# - bof - #

#!/bin/bash

Opt_=$1;



<<'comment'
rm -rf ~/DTs_Arch.sh; vim ~/DTs_Arch.sh;
copy & paste this content
chmod +x ~/DTs_Arch.sh; ~/./DTs_Arch.sh;

ssh-keygen -R 192.168.122.1
#cd~/; curl -O http://192.168.1.200/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/DTs_Arch.Ins/DTs_Arch.sh;
cd~/; curl -O http://192.168.122.100/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/DTs_Arch.Ins/DTs_Arch.sh;
chmod +x ~/DTs_Arch.sh; ~/./DTs_Arch.sh;
~/./DTs_Arch.sh;



# -  - #
# - When this error happen on ssh command - #
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# -  - #
# - Fixing this error - #
ssh-keygen -R "you server hostname or ip"
ssh-keygen -R 192.168.1.100;
ssh-keygen -R 192.168.1.200;
ssh-keygen -R 192.168.122.100;
ssh-keygen -R 192.168.122.200;
ssh-keygen -R 192.168.122.1
comment



currentD_=($PWD);
uServerIP_='192.168.122.200';
gateway4_='192.168.122.1';
Addresses_='8.8.8.8, 8.8.4.4';
rootPasswd_='Administrator2';
hostName_='Arch';
swapG_='4G';
swapName_='swap.img';

<<'comment'
 adminUser_='is_derayo';
 adminName_="Djalmar Enrique Rayo";
comment

adminUser_='luigi';
adminName_="Luigi Guarnieri";
rootPasswd_='dispata06041940';

adminPasswd_=$rootPasswd_;
#system_='BIOS';
 system_='UEFI';
#createDiskConfirmation_='Yes';
createDiskConfirmation_='No';
# ( BIOS diskType_=1, UEFI diskType_=2 )
if [ "$system_" == "BIOS" ]; then diskType_=1; else diskType_=2; fi



# -  - #
# - createDisk_Bios_ - #
function createDisk_Bios_(){
  local Disk_='' Disk1_='' Disk2_='' \
        d1_='sda' d2_='vda' d3_='';        

  d3_=$(lsblk | grep -oF "$d1_"" ");   # look for sda
  # - Is d3_ empty ? - #
  if [ "$d3_" == "" ]; then
    d3_=$(lsblk | grep -oF "$d2_"" ");
    if [ "$d3_" == "" ]; then
      echo 'There is not disk present to format...';
      return;
    else
      Disk_=$d2_;
    fi
  else
    Disk_=$d1_;
  fi

  Disk1_=$Disk_'1';
  Disk2_=$Disk_'2';
  
<<'comment'
  # -  - #
  # - Format HDD - #

  fdisk /dev/vda;
  o  # DOS Partition Table
  n  # New
  p  # Primary
  1  # Default
  83 # Linux
  w  # Write changes to disk

  fdisk -l /dev/vda;
  Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors

  Device     Boot    Start       End   Sectors Size Id Type
  /dev/vda1           2048  62916607  62914560  30G 83 Linux
  /dev/vda2       62916608 209715199 146798592  70G 83 Linux

  lsblk -f
  NAME   FSTYPE  FSVER            LABEL       UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
  sr0    iso9660 Joliet Extension ARCH_202202 2022-02-01-17-06-09-00                              
  vda                                                                                             
  ├─vda1 ext4    1.0              Arch        6ff0e382-26a7-42c4-b27d-d1c8866f63f3   15.6G    42% /
  └─vda2 ext4    1.0              Data        ea1d7285-8570-48a9-839f-19bb0017b55c  
comment

#sudo wipefs -a -f /dev/$Disk_
#sudo wipefs -o 0x1fe /dev/$Disk_;

(
echo o    # New empty DOS partition table
echo n    # New partition
echo p    # Primary partition
echo 1    # Partition number
echo      # First sector (default: 1)
echo +30G # Arch partition
echo n    # Add a new partition
echo p    # Primary partition
echo 2    # Partition number
echo      # First sector (default: 1)
echo      # Last sector (default, rest of partition Data) 
echo w    # Write changes
) | sudo fdisk /dev/$Disk_;

(echo Y) | sudo mkfs.ext4 -L Arch /dev/$Disk1_;
(echo Y) | sudo mkfs.ext4 -L Data /dev/$Disk2_;

echo 'BIOS partition table created,';
echo 'Disk was formated...';

}



# -  - #
# - createDisk_UEFI_ - #
function createDisk_UEFI_(){
  local Disk_='' Disk1_='' Disk2_='' Disk3_='' \
        d1_='sda' d2_='vda' d3_='';        

  d3_=$(lsblk | grep -oF "$d1_"" ");   # look for sda
  # - Is d3_ empty ? - #
  if [ "$d3_" == "" ]; then
    d3_=$(lsblk | grep -oF "$d2_"" ");
    if [ "$d3_" == "" ]; then
      echo 'There is not disk present to format...';
      return;
    else
      Disk_=$d2_;
    fi
  else
    Disk_=$d1_;
  fi

  Disk1_=$Disk_'1';
  Disk2_=$Disk_'2';
  Disk3_=$Disk_'3';

<<'comment'
  # -  - #
  # - Format HDD - #

  gdisk -l /dev/vda;

  gdisk /dev/vda;
  o                 # GPT Partition Table
  n                 # New
  Y                 # Yes
  t                 # Type
  ef00              # EFI system partition +640M
  8300              # Linux filesystem       30G
  8300              # Linux filesystem      rest

  w                 # Write changes to disk

  gdisk -l /dev/vda;

  GPT fdisk (gdisk) version 1.0.8

  Partition table scan:
    MBR: protective
    BSD: not present
    APM: not present
    GPT: present

  Found valid GPT with protective MBR; using GPT.
  Disk /dev/vda: 209715200 sectors, 100.0 GiB

  Number  Start (sector)    End (sector)  Size       Code  Name
     1            2048         1312767   640.0 MiB   EF00  EFI system partition
     2         1312768        64227327   30.0 GiB    8300  Linux filesystem
     3        64227328       209715166   69.4 GiB    8300  Linux filesystem

  lsblk -f
  NAME   FSTYPE  FSVER            LABEL       UUID                                 
  sr0    iso9660 Joliet Extension ARCH_202202 2022-02-01-17-06-09-00              
  vda  
  ├─vda1 vfat    FAT32            EFI BOOT    70E-7DA8
  ├─vda2 ext4    1.0              Arch        6ff0e382-26a7-42c4-b27d-d1c8866f63f3
  └─vda3 ext4    1.0              Data        ea1d7285-8570-48a9-839f-19bb0017b55c
comment


<<'comment'
(
echo d     # Delete partition
echo 3     # Number
echo Y     # Yes
echo w     # Write changes
echo Y     # Yes
) | sudo gdisk /dev/vda;

(
echo d     # Delete partition
echo 2     # Number
echo Y     # Yes
echo w     # Write changes
echo Y     # Yes
) | sudo gdisk /dev/vda;

(
echo d     # Delete partition
echo 1     # Number
echo Y     # Yes
echo w     # Write changes
echo Y     # Yes
) | sudo gdisk /dev/vda;


sudo wipefs -a -f /dev/$Disk_
#sudo wipefs -o 0x1fe /dev/$Disk_;
comment



(
echo o     # New empty GPT partition table
echo Y     # Yes
echo n     # New partition
echo 1     # Partition number
echo       # First sector (default: 1)
echo +640M # Arch partition
echo ef00  # EFI system partition
echo n     # New partition
echo 2     # Partition number
echo       # First sector (default: 1)
echo +30G  # Arch
echo 8300  # Linux filesystem
echo n     # Add a new partition
echo 3     # Data
echo       # First sector (default: 1)
echo       # Last sector (default, rest of partition Data) 
echo 8300  # Linux filesystem
echo w     # Write changes
echo Y     # Write changes
) | sudo gdisk /dev/$Disk_;

sudo mkfs.vfat -n BOOT /dev/$Disk1_;
(echo Y) | sudo mkfs.ext4 -L Arch /dev/$Disk2_;
(echo Y) | sudo mkfs.ext4 -L Data /dev/$Disk3_;

<<'comment'
  mkswap -L Swap /dev/vda2;
  swapon /dev/vda2;
comment

  echo 'UEFI partition table created,';
  echo 'Disk was formated...';

}


<<'comment'

(
echo o     # New empty GPT partition table
echo Y     # Yes
echo w     # Write changes
echo Y     # Yes
) | sudo gdisk /dev/sda;
sudo reboot now;

(
echo o     # New empty GPT partition table
echo Y     # Yes
echo w     # Write changes
echo Y     # Yes
) | sudo gdisk /dev/vda;
sudo reboot now;

comment

# -  - #
# - diskLabels_ 1 $Disk1_ $Disk2_ $Disk3_ - #
function diskLabels_(){
  local systemType_=$1 $Disk1_=$2 $Disk2_=$3 $Disk3_=$4;
  case $systemType_ in
    ''|*[!0-9]*)
      echo '';
      echo 'Its not a number...';
      ;;
    1)
      sudo e2label /dev/$Disk1_ Arch;
      sudo e2label /dev/$Disk2_ Data;
      ;;
    2)
      sudo dosfslabel /dev/$Disk1_ BOOT;
      sudo e2label /dev/$Disk2_ Arch;
      sudo e2label /dev/$Disk3_ Data;
      ;;
    *)
      echo "Option no valid: " $Opt_;
      ;;
  esac
}



# -  - #
# - createDisk_ 1 - #
function createDisk_(){
  local Disk_=$1;
  case $Disk_ in
    ''|*[!0-9]*)
      echo '';
      echo 'Its not a number...';
      ;;
    1)
      createDisk_Bios_;
      ;;
    2)
      createDisk_UEFI_;
      ;;
    *)
      echo "Option no valid: " $Opt_;
      ;;
  esac
  
  echo '';
  lsblk -f;
  echo '';
  read -t 10 -p '10 seconds pause (disk structure)...';
  
}



# -  - #
# - ArchBase1_ - #
function ArchBase1_(){
local Disk_='' \
      Disk1_='' Disk2_='' Disk3_='' \
      d1_='sda' d2_='vda' d3_='';        

  d3_=$(lsblk | grep -oF "$d1_"" ");   # sda "lsblk -f" line
  if [ "$d3_" == "" ]; then
    d3_=$(lsblk | grep -oF "$d2_"" ");
    if [ "$d3_" == "" ]; then
      echo 'There is not disk present for this installation...';
      exit;
    else
      Disk_=$d2_;
    fi
  else
    Disk_=$d1_;
  fi

  Disk1_=$Disk_'1';
  Disk2_=$Disk_'2';
  Disk3_=$Disk_'3';
  


  # -  - #
  # - Timezone - #
  timedatectl set-ntp true;
  timedatectl set-timezone America/New_York;
  


  # -  - #
  # - Mount sda/vda - #
  if [ "$system_" == "BIOS" ]; then
    diskLabels_ 1 $Disk1_ $Disk2_;
    mount /dev/$Disk1_ /mnt;
  elif [ "$system_" == "UEFI" ]; then
    diskLabels_ 2 $Disk1_ $Disk2_ $Disk3_;
    mount /dev/$Disk2_ /mnt;
    mkdir -p /mnt/boot/efi;
    mount /dev/$Disk1_ /mnt/boot/efi;
    #echo $Disk1_ $Disk2_ $Disk3_;
  fi



  # -  - #
  # - Create & swapon file - #
  fallocate -l $swapG_ /mnt/$swapName_;
  chmod 600 /mnt/$swapName_;
  mkswap -L Swap /mnt/$swapName_;
  swapon /mnt/$swapName_;



  # -  - #
  # - Install linux base - #
  pacstrap /mnt base base-devel dhcpcd dialog gedit git grub linux linux-headers linux-firmware \
                mesa nano net-tools netctl networkmanager network-manager-applet openssh samba sudo \
                sshfs tilix vim wpa_supplicant;



  # -  - #
  # - Generate fstab file - #
  genfstab -U /mnt >> /mnt/etc/fstab;

  # -  - #
  # - Copy script /mnt - #
  cp DTs_Arch.sh /mnt;
  arch-chroot /mnt ./DTs_Arch.sh 2 $Disk_;

}



# -  - #
# - ArchBase2_ - #
function ArchBase2_(){
  local Disk_=$2 \
        hostnameF_=/etc/hostname \
        hostsF_=/etc/hosts;

  # -  - #
  # - Set localtime - #
  ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime;

  # -  - #
  # - Set hardware clock - #
  hwclock --systohc;

  # -  - #
  # - generate locate - #
  sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen;
  locale-gen;

  # -  - #
  # - Set language - #
  echo "LANG=en_US.UTF-8" >> /etc/locale.conf;

  # -  - #
  # - Set keyboard layout - #
  echo "KEYMAP=en" >> /etc/vconsole.conf;

  # -  - #
  # - Set hostname - #
  if [ "$Disk_" == "vda" ]; then hostName_='ArchVManager'; fi;
  rm -rf $hostnameF_;
  echo $hostName_ >> $hostnameF_;
  hostnamectl set-hostname $hostName_;

  # -  - #
  # - Set local ip address - #
  rm -rf $hostsF_;
  echo "127.0.0.1 localhost" >> $hostsF_;
  echo "::1       localhost" >> $hostsF_;
  echo '127.0.1.1 '$hostName_'.localdomain '$hostName_ >> $hostsF_;



  # -  - #
  # - Hardware software - #

  # -  - #
  # - All video cards (generics) - #
  pacman -S xf86-video-vesa xf86-video-ati xf86-video-intel xf86-video-amdgpu xf86-video-nouveau --noconfirm;

  # -  - #
  # - Audio - #
  pacman -S pulseaudio pulseaudio-alsa pulseaudio-bluetooth --noconfirm;

  # -  - #
  # - Intel, AMD & nvidia - #
  pacman -S intel-ucode amd-ucode nvidia nvidia-utils --noconfirm;

  # -  - #
  # - Utilitary software - #
  pacman -S acpi acpi_call alsa-utils dnsutils inetutils powertop reflector \
            tlp xdg-utils xdg-user-dirs --noconfirm; 
  pacman -S bluez bluez-utils wireless_tools --noconfirm; 

  # -  - #
  if [ "$system_" == "UEFI" ]; then 
    pacman -S efibootmgr dosfstools mtools --noconfirm;
  fi



  # -  - #
  # - Double boot activation - #
  pacman -S os-prober --noconfirm;
  os-prober;  
  sed -i 's/#GRUB_DISABLE_OS_PROBER=false/GRUB_DISABLE_OS_PROBER=false/' /etc/default/grub;

  # -  - #
  # - Install grub - #
  if [ "$system_" == "BIOS" ]; then
    grub-install /dev/$Disk_;
  elif [ "$system_" == "UEFI" ]; then
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB --recheck;
  fi
  read -t 10 -p '10 seconds pause (grub-install)...';

  # -  - #
  # - Create grub.cfg file - #
  grub-mkconfig -o /boot/grub/grub.cfg;
  read -t 10 -p '10 seconds pause (grub-mkconfig)...';



  # -  - #
  # - Root & admin user - #
  echo 'root:'$rootPasswd_ | sudo chpasswd;
  # -  - #
  useradd -m $adminUser_;
  usermod -aG wheel,audio,video,optical,storage $adminUser_;
  usermod -c "$adminName_" $adminUser_; 
  echo $adminUser_':'$adminPasswd_ | sudo chpasswd;



  # -  - #
  # - Authorize wheel to sudo - #
  sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers;



  # -  - #
  # - Printer - #
  sudo pacman -S cups cups-pdf hplip libcups system-config-printer --noconfirm;
  sudo systemctl enable cups;



  # -  - #
  # - Additional software - #

  # -  - #
  # - Programs - #
  sudo pacman -S audacious bash-completion bleachbit bpytop curl dolphin evince filelight file-roller \
                 filezilla firefox gcc geeqie gimp gnome-disk-utility gnome-calculator gnome-multi-writer \
                 gnome-packagekit gnome-terminal gnome-system-monitor gpart gparted gpicview grub-customizer \
                 gvfs htop kpat libreoffice man mysql-clients neofetch nmap ntfs-3g pavucontrol pinta plank \
                 poppler-glib postgresql pv python p7zip reflector rsync screenfetch simple-scan smplayer \
                 spectacle testdisk thunar transmission-gtk tree unrar unzip viewnior vlc whois wget \
                 yajl --noconfirm;                 

  # -  - #
  # - Picture viewer programs - #
  sudo pacman -S gpicview viewnior --noconfirm;

  # -  - #
  # - Media utilities - #
  sudo pacman -S brasero clementine xfburn --noconfirm;

  # -  - #
  # - To install yay & yaourt - #
  sudo pacman -S binutils fakeroot make --noconfirm;

  # -  - #
  # - Arch themes - #
  sudo pacman -S adapta-gtk-theme arc-gtk-theme arc-icon-theme archlinux-wallpaper gtkmm3 \
                 papirus-icon-theme ttf-freefont --noconfirm;



  # -  - #
  # - Enable ssh & NetworkManager - #
  systemctl enable sshd;
  systemctl enable NetworkManager;
  
  
  
  # -  - #
  # - Exit & reboot - #
  echo '';
  printf "\e[1;32mDone! Type umount -a and shutdown now.\e[0m"
  echo '';

  rm -rf /DTs_Arch.sh;

}



<<'comment'
  case $MAX in
    ''|*[!0-9]*)
      echo "The value $MAX is not a number !"
      exit 1
      ;;
    *)
      if [ $MAX -lt 50 ] || [ $MAX -gt 100 ] ;then
        echo "The value $MAX is not between 50-100"
        exit 1
      fi
      echo "Looks like we are good !"
      ;;
comment



case $Opt_ in
  ''|*[!0-9]*)
    if [ "$createDiskConfirmation_" == "Yes" ]; then
       createDisk_ $diskType_; fi 
    ArchBase1_;
    umount -a;
    echo '';
    echo 'Arch installation is done...';
    read -t 5 -p 'System will shutdown now.';
    echo '';
    shutdown now;
    ;;
  2)
    ArchBase2_ $1 $2;
    ;;
  *)
    echo "Option no valid: " $Opt_;
    ;;
esac



<<'comment'
Very important:
Make the boot BIOS for a BIOS installation.
Make the boot UEFI for a UEFI installation.


ssh-keygen -R 192.168.122.1
#cd~/; curl -O http://192.168.1.200/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/DTs_Arch.Ins/DTs_Arch.sh;
cd~/; curl -O http://192.168.122.100/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/DTs_Arch.Ins/DTs_Arch.sh;
chmod +x ~/DTs_Arch.sh; ~/./DTs_Arch.sh;
~/./DTs_Arch.sh;
comment



# - eof - #
