# - bof - #

#!/bin/bash

Opt_=$1;



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

ssh-keygen -R 10.0.0.1
ssh-keygen -R 192.168.122.1
cd ~/; curl -O http://192.168.122.100/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/KarlaS_Arch.Ins/KarlaS_Arch.sh;

chmod +x ~/KarlaS_Arch.sh; ~/./KarlaS_Arch.sh;
~/./KarlaS_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 10.0.0.1
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;
comment



currentD_=($PWD);



uServerIP_isdevelopmentUS_='www.isdevelopment.us';
gateway4_isdevelopmentUS_='192.168.1.1';

uServerIP_uSVManager='10.0.0.100';
gateway4_uSVManager='10.0.0.1';

uServerIP_uSProxmox='10.0.0.200';
gateway4_uSProxmox='10.0.0.1';


ViManager_='Yes';
VEProxmox_='No';
RealMetal_='No';
WebServer_='No';

if [ "$ViManager_" == "Yes" ]; then
   uServerIP_=$uServerIP_uSVManager;
   gateway4_=$gateway4_uSVManager;
elif [ "$VEProxmox_" == "Yes" ]; then
   uServerIP_=$uServerIP_uSProxmox;
   gateway4_=$gateway4_uSProxmox;
elif [ "$WebServer_" == "Yes" ]; then
   uServerIP_=$uServerIP_isdevelopmentUS_;
   gateway4_=$gateway4_isdevelopmentUS_;
elif [ "$RealMetal_" == "Yes" ]; then
   uServerIP_=$uServerIP_isdevelopmentUS_;
   gateway4_=$gateway4_isdevelopmentUS_;
fi;



Addresses_='8.8.8.8, 8.8.4.4';
hostName_='Arch';
swapG_='2G';
swapName_='swap.img';

User_='luigi';
User_='is_derayo';

adminUser_=$User_;

if [ "$User_" == "is_derayo" ]; then
   adminName_="Djalmar Enrique Rayo";
   rootPasswd_='Administrator2';
elif [ "$User_" == "luigi" ]; then
   adminName_='Luigi Guarnieri';
   rootPasswd_='dispata06041940';
fi;

adminPasswd_=$rootPasswd_;

createDiskConfirmation_='No';
createDiskConfirmation_='Yes';

GuestUser_='guest';
GuestPasswd_=$rootPasswd_;
GuestName_='Guest user';



# -  - #
# - 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

(
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 "";
  echo 'BIOS partition table created,';
  echo 'Disk was formated...';
  sleep 10;
}



# -  - #
# - 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)
      if [ $(lsblk -f | grep -oF  "$Disk1_") = $Disk1_ ] && \
         [ $(lsblk -f | grep -oF  "$Disk2_") = $Disk2_ ]; then return; fi;

      sudo e2label /dev/$Disk1_ Arch;
      sudo e2label /dev/$Disk2_ Data;
      ;;
    2)
      if [ $(lsblk -f | grep -oF  "$Disk1_") = $Disk1_ ] && \
         [ $(lsblk -f | grep -oF  "$Disk2_") = $Disk2_ ] && \
         [ $(lsblk -f | grep -oF  "$Disk3_") = $Disk3_ ]; then return; fi;

      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: " $Disk_;
      ;;
  esac

  echo "";
  lsblk -f;
  echo "";
  echo "10 seconds pause (disk structure)...";
  sleep 10;
}



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

  d3_=$(lsblk | grep -oF "$d1_"" ");   # vda/sda "lsblk -f" line
  # - Is d3_ empty ? - #
  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';



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

  # -  - #
  # - Mount sda/vda - #
  #e2label /dev/$Disk1_ Arch;
  #mount /dev/$Disk1_ /mnt;
  diskLabels_ 1 $Disk1_ $Disk2_;
  mount /dev/$Disk1_ /mnt;



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


  echo "";
  echo "Swap memory created...";
  free -h;
  echo "";
  sleep 5;


  # -  - #
  # - 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 /mn - #
  cp KarlaS_Arch.sh /mnt;
  arch-chroot /mnt ./KarlaS_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 --needed;

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

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

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

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



  # -  - #
  # - Install grub - #
  grub-install /dev/$Disk_;

  # -  - #
  # - Create grub.cfg file - #
  grub-mkconfig -o /boot/grub/grub.cfg;
  echo "";
  echo "10 seconds pause (grub-mkconfig configured)...";
  sleep 10;


  # -  - #
  # - Root password & create new user & password - #

  # -  - #
  echo "";
  echo "Creating root password...";
  echo 'root:'$rootPasswd_ | sudo chpasswd;
  sleep 10;


  echo "";
  echo "Authorizing root user full ssh access...";
  sshdConf_=/etc/ssh/sshd_config;  
  sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' $sshdConf_;
  sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' $sshdConf_;
  sleep 10;



  # -  - #
  echo "";
  echo "Creating admin user...";
  useradd -m $adminUser_;
  usermod -aG wheel,audio,video,optical,storage $adminUser_;
  usermod -c "$adminName_" $adminUser_; 
  echo $adminUser_':'$adminPasswd_ | sudo chpasswd;
  sleep 10;



  # -  - #
  echo "";
  echo "Creating guest user...";
  useradd -m $GuestUser_;
  usermod -aG wheel,audio,video,optical,storage $GuestUser_;
  usermod -c "$GuestName_" $GuestUser_; 
  echo $GuestUser_':'$GuestPasswd_ | sudo chpasswd;
  sleep 10;



  # -  - #
  # - 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 --needed;
  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 pydf python p7zip reflector rsync screenfetch simple-scan smplayer \
                 spectacle testdisk thunar transmission-gtk tree unrar unzip viewnior vlc whois wget \
                 yajl --noconfirm --needed;

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

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

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

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



  # -  - #
  # - Enable ssh & NetworkManager - #
  systemctl enable sshd;
  systemctl enable NetworkManager;



  rm -rf /KarlaS_Arch.sh;
  
  # -  - #
  # - Exit & reboot - #
  echo '';
  printf "\e[1;32mDone! Type umount -a and shutdown now.\e[0m"
  echo '';

}





# -  - #
# - SetingIPAddressOnLogin_ - #
function SetingIPAddressOnLogin_(){
  local rcLocal_=/etc/rc.local \
        rcLocaluServer_='/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/KarlaS_Arch.Ins/IPAddressAtLogin/rc.local' \
        rcLocalService_=/etc/systemd/system/rc-local.service \
        rcLocalServiceuServer_='/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/KarlaS_Arch.Ins/IPAddressAtLogin/rc-local.service' \
        cUser_=/home/is_derayo \
        Dir_=($PWD);
  local tFile_='';
  
  echo '';
  echo 'Seting IP Address on login...';

  tFile_=$cUser_/rc.local;
  rm -rf $tFile_;
  cd $cUser_;

  # -  - #
  # - Processing rc.local - #
  curl -O http://$uServerIP_$rcLocaluServer_;
  sudo rm -r $rcLocal_;
  sudo mv $tFile_ $rcLocal_;
  sudo chmod +x $rcLocal_;


  tFile_=$cUser_/rc-local.service;
  rm -rf $tFile_;

  # -  - #
  # - Processing rc-local.service - #
  curl -O http://$uServerIP_$rcLocalServiceuServer_;
  
  sudo rm -r $rcLocalService_;
  sudo mv $tFile_ $rcLocalService_;

  sudo systemctl daemon-reload;
  sudo systemctl start rc-local.service;
  sudo systemctl status rc-local.service;
  
  cd $Dir_;
  echo 'Ip address stablished...';
}



<<'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



# -  - #
# - checkComputer_ - #
function checkComputer_(){
  clear;
  echo '';
  echo 'Checking computer...';
  if [ "$HOSTNAME" == "iMacArch" ]; then
     echo '';
     echo "This process cannot be executed on $HOSTNAME...";
     spd-say -w "This process cannot be executed on $HOSTNAME";
     rm -rf ~/KarlaS_Arch.sh;
     echo '';
     exit;
  fi;
  echo '';
}



case $Opt_ in
  ''|*[!0-9]*)

    checkComputer_;
    
    if [ "$createDiskConfirmation_" == "Yes" ]; then
       createDisk_ 1; 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;
    #SetingIPAddressOnLogin_;
    ;;
  *)
    echo "Option no valid: " $Opt_;
    ;;
esac



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

ssh-keygen -R 192.168.122.1
cd ~/; curl -O http://$uServerIP_/Images/Programs/Linux.Img/Linux_Arch.Img/Arch_Install/KarlaS_Arch.Ins/KarlaS_Arch.sh;
chmod +x ~/KarlaS_Arch.sh; ~/./KarlaS_Arch.sh;
~/./KarlaS_Arch.sh;
comment



# - eof - #
