# - bof - #
#!/bin/bash



Opt_=$1;



<<'comment'
# -  - #
# - File name - #
Ermanno_Btrfs-Arch.sh

cd ~/; rm -rf ~/Ermanno_Btrfs-Arch.sh; vim ~/Ermanno_Btrfs-Arch.sh;
copy & paste this content
chmod +x ~/Ermanno_Btrfs-Arch.sh; ~/./Ermanno_Btrfs-Arch.sh;

# - Configured - #

rm -rf ~/.bashrc;
vim ~/.bashrc;

rm -rf ~/.zshrc;
vim ~/.zshrc;

ssh-keygen -R 192.168.1.1

# - On client - #
cd ~/.ssh;
ssh-keygen -t rsa;

cat ~/.ssh/id_rsa.pub;

# - On server - #
vim ~/.ssh/authorized_keys;

sudo apt install curl git htop inxi net-tools ntp neovim screenfetch tree -y;

# -  - #
# - Documentation - #
comment



currentD_=($PWD);


iMac_='Yes';
iMac_='No';

HPLaptop_='Yes';
HPLaptop_='No';

HPDesktop_='Yes';
HPDesktop_='No';

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

uServerIP_uSVManager='192.168.1.101';
gateway4_uSVManager='192.168.1.1';

uServerIP_uSProxmox='192.168.1.100';
gateway4_uSProxmox='192.168.1.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;


 hostName_='ArchBtrfs';
#hostName_='HPDesktopArch';

Addresses_='8.8.8.8, 8.8.4.4';



 zRam_='Yes';
#zRam_='No';

 swapPartition_='Yes';
 swapPartition_='No';

 swapFile_='Yes';
 swapFile_='No';

sG_="4G";
sF_=/swap.img;
fS_=/etc/fstab;



if [ "$zRam_" == "Yes" ]; then
  swapPartition_='No';
  swapFile_='No';
elif [ "$swapPartition_" == "Yes" ]; then
  zRam_='No';
  swapFile_='No';
elif [ "$swapFile_" == "Yes" ]; then
  zRam_='No';
  swapPartition_='No';
fi;



 Btrfs_='Yes';
#Btrfs_='No';



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

if [ "$hostName_" == "HPLaptopArch" ] || [ "$hostName_" == "HPDesktopArch" ]; then
   createDiskConfirmation_="No"; fi;


 system_='BIOS';
#system_='UEFI';


# ( BIOS diskType_=1, UEFI diskType_=2 )
if [ "$system_" == "BIOS" ]; then diskType_=1; else diskType_=2; fi;

 OS_Prober_='Yes';
#OS_Prober_='No';



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

  if [ "$iMac_" == "Yes" ]; then d1_='sdb'; fi;
  if [ "$HPLaptop_" == "Yes" ]; then d1_='nvme0n1'; fi;
  
  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;

  if [ "$HPLaptop_" == "Yes" ]; then
    Disk1_=$Disk_'p1';
    Disk2_=$Disk_'p2';
    Disk3_=$Disk_'p3';
  else
    Disk1_=$Disk_'1';
    Disk2_=$Disk_'2';
    Disk3_=$Disk_'3';
  fi;

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



<<'comment'
sudo wipefs -a -f /dev/sda;
sudo wipefs -o 0x1fe /dev/sda;
comment

echo "Disk :" $Disk_;
echo "Disk1:" $Disk1_;
echo "Disk2:" $Disk2_;
echo "Disk3:" $Disk3_;
echo "";
echo "5 Seconds pause ...";
sleep 5;
#comment



if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
(
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 +35G  # 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.btrfs -L Arch /dev/$Disk1_;
(echo Y) | sudo mkfs.btrfs -L Data /dev/$Disk2_;

else

(
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 +4G   # Swap
echo t     # Type
echo 8200  # Linux swap
echo n     # New partition
echo p     # Primary partition
echo 2     # Partition number
echo       # First sector (default: 1)
echo +35G  # Arch partition
echo n     # Add a new partition
echo p     # Primary partition
echo 3     # Partition number
echo       # First sector (default: 1)
echo       # Last sector (default, rest of partition Data) 
echo w     # Write changes
) | sudo fdisk /dev/$Disk_;

           sudo mkswap -L Swap /dev/$Disk1_;
(echo Y) | sudo mkfs.btrfs -L Arch /dev/$Disk2_;
(echo Y) | sudo mkfs.btrfs -L Data /dev/$Disk3_;

  swapOnPartition_ $Disk1_;
fi;

  echo "";
  echo 'BIOS partition table created,';
  echo 'Disk was formated...';
  echo "";
  lsblk -f;
  echo "";
  sleep 10;
}



# -  - #
# - swapOnPartition_ - #
function swapOnPartition_(){
  local swapDisk_=$1;
  swapon /dev/$swapDisk_;
  #mkswap -L Swap /dev/$Disk1_;
  #swapoff /dev/$Disk1_;
}



# -  - #
# - swapOnFile_ - #
function swapOnFile_(){
  if [ "$Btrfs_" == "Yes" ]; then
    btrfs_Swap_img_;
  else
    ext4_Swap_img_;
  fi;
  mountSwap_;
}



# -  - #
# - ext4_Swap_img_ - #
function ext4_Swap_img_(){
  echo "";
  echo "Creating swap file for ext4 root partition...";
  sudo fallocate -l $sG_ $sF_;
  sudo chmod 600 $sF_;
  sudo mkswap -L Swap $sF_;
  sudo swapon $sF_;
  echo "swap.img file was created successfully...";
  echo "";
}



# -  - #
# - btrfs_Swap_img_ - #
function btrfs_Swap_img_(){
  echo "";
  echo "Creating swap file for btrfs root partition...";
  sudo truncate -s 0 $sF_;
  sudo chattr +C $sF_;
  sudo fallocate -l $sG_ $sF_;
  sudo chmod 600 $sF_;
  sudo mkswap -L Swap $sF_;
  sudo swapon $sF_;
  echo "swap.img file was created successfully...";
  echo "";
}



# -  - #
# - mountSwap_ - #
function mountSwap_(){
  local sD_=~/Documents \
        sL_='';
  local tFs_=$sD_/fstab.tmp;

  echo "";
  echo "Mounting swap.img to fstab file ...";
  mkdir -p $sD_;
  rm -rf $tFs_;
  cat $fS_ >> $tFs_;
  sL_=$(echo "$sF_                                   none        swap    defaults                0       0")
  sed -i '5i '"$sL_" $tFs_;
  sudo mv $tFs_ $fS_;
  sudo mount -av;
  echo "swap.img file was mounted successfully...";
  echo "";
  sleep 5;

<<'comment'
  sudo swapoff -v /swap.img;
  sudo rm -rf /swap.img;
comment
}



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

  if [ "$iMac_" == "Yes" ]; then d1_='sdb'; fi;
  if [ "$HPLaptop_" == "Yes" ]; then d1_='nvme0n1'; fi;

  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;

  if [ "$HPLaptop_" == "Yes" ]; then
    Disk1_=$Disk_'p1';
    Disk2_=$Disk_'p2';
    Disk3_=$Disk_'p3';
    Disk4_=$Disk_'p4';
  else
    Disk1_=$Disk_'1';
    Disk2_=$Disk_'2';
    Disk3_=$Disk_'3';
    Disk4_=$Disk_'4';
  fi;

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

  if [ "$d3_" != "" ]; then Disk_=$d2_; fi;
  if [ $(lsblk -f | grep -oF  "$Disk1_") = $Disk1_ ] && \
     [ $(lsblk -f | grep -oF  "$Disk2_") = $Disk2_ ] && \
     [ $(lsblk -f | grep -oF  "$Disk3_") = $Disk3_ ]; then echo $Disk1_" "$Disk2_" "$Disk3_; fi;

  gdisk -l /dev/vda;

  vda
  ├─vda1 vfat     FAT32            EFI BOOT    0548-C74E
  ├─vda2 swap     1                Swap         c7335240-6db1-4a89-89ce-428b77ce5c0b
  ├─vda3 btrfs                     Arch        a4011d58-cede-4059-afa0-00aaf0891d55
  └─vda4 btrfs                     Data        2f14e979-a386-44ab-93fb-78ac932fd8a0


  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


(
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/vda;
sudo wipefs -a -f /dev/$Disk_;
sudo wipefs -o 0x1fe /dev/$Disk_;
comment

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

if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
(
echo o     # New empty GPT partition table
echo Y     # Yes
echo n     # New partition
echo p     # Primary partition
echo 1     # Partition number
echo       # First sector (default: 1)
echo +640M # Arch partition
echo ef00  # EFI system partition
echo n     # New partition
echo p     # Primary partition
echo 2     # Partition number
echo       # First sector (default: 1)
echo +35G  # Arch
echo 8300  # Linux filesystem
echo n     # Add a new partition
echo p     # Primary 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.btrfs -L Arch /dev/$Disk2_;
(echo Y) | sudo mkfs.btrfs -L Data /dev/$Disk3_;

<<'comment'
  if [ "$swapFile_" == "Yes" ]; then
    swapOnFile_; fi;
comment

else
(
echo o     # New empty GPT partition table
echo Y     # Yes
echo n     # New partition
echo p     # Primary partition
echo 1     # Partition number
echo       # First sector (default: 1)
echo +640M # Arch partition
echo ef00  # EFI system partition
echo n     # New partition
echo p     # Primary partition
echo 2     # Partition number
echo       # First sector (default: 1)
echo +4G   # Swap
echo 8200  # Linux swap
echo n     # New partition
echo p     # Primary partition
echo 3     # Partition number
echo       # First sector (default: 1)
echo +35G  # Arch
echo 8300  # Linux filesystem
echo n     # Add a new partition
echo p     # Primary partition
echo 4     # 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 mkswap -L Swap /dev/$Disk2_;
(echo Y) | sudo mkfs.btrfs -L Arch /dev/$Disk3_;
(echo Y) | sudo mkfs.btrfs -L Data /dev/$Disk4_;

  swapOnPartition_ $Disk2_;
fi;

  echo "";
  echo 'UEFI partition table created,';
  echo 'Disk was formated...';
  echo "";
  echo "";
  sleep 10;
}



# -  - #
# - diskLabels_ 1 $Disk1_ $Disk2_ $Disk3_ $Disk4_ - #
function diskLabels_(){
  local systemType_=$1 $Disk1_=$2 $Disk2_=$3 $Disk3_=$4 $Disk4_=$5;

  case $systemType_ in
    ''|*[!0-9]*)
      echo '';
      echo 'Its not a number...';
      ;;
    1)
      if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
        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;
      else
        if [ $(lsblk -f | grep -oF  "$Disk1_") = $Disk1_ ] && \
           [ $(lsblk -f | grep -oF  "$Disk2_") = $Disk2_ ] && \
           [ $(lsblk -f | grep -oF  "$Disk3_") = $Disk3_ ]; then return; fi;
        sudo swaplabel --label Swap /dev/$Disk1_;
        sudo e2label /dev/$Disk2_ Arch;
        sudo e2label /dev/$Disk3_ Data;
      fi;
      ;;
    2)
      if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
        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;
      else
        if [ $(lsblk -f | grep -oF  "$Disk1_") = $Disk1_ ] && \
           [ $(lsblk -f | grep -oF  "$Disk2_") = $Disk2_ ] && \
           [ $(lsblk -f | grep -oF  "$Disk3_") = $Disk3_ ] && \
           [ $(lsblk -f | grep -oF  "$Disk4_") = $Disk4_ ]; then return; fi;
        sudo dosfslabel /dev/$Disk1_ BOOT;
        sudo swaplabel --label Swap /dev/$Disk2_;
        sudo e2label /dev/$Disk3_ Arch;
        sudo e2label /dev/$Disk4_ Data;
      fi;
      ;;
    *)
      echo "Option no valid: " $systemType_;
      ;;
  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 "";
  echo "";
  echo "Mounts as shown...";
  lsblk -f;
  echo "";
  echo "10 seconds pause (disk structure)...";
  sleep 10;
}



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

  if [ "$iMac_" == "Yes" ]; then d1_='sdb'; fi;
  if [ "$HPLaptop_" == "Yes" ]; then d1_='nvme0n1'; fi;

  d3_=$(lsblk | grep -oF "$d1_"" ");   # vda/sda/sdb/nvme0n1p "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;

  if [ "$HPLaptop_" == "Yes" ]; then
    Disk1_=$Disk_'p1';
    Disk2_=$Disk_'p2';
    Disk3_=$Disk_'p3';
    Disk4_=$Disk_'p4';
  else
    Disk1_=$Disk_'1';
    Disk2_=$Disk_'2';
    Disk3_=$Disk_'3';
    Disk4_=$Disk_'4';
  fi;


  # -  - #
  # - Loadkeys - #
  loadkeys us;

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


<<'comment'
  # -  - #
  # - BTRFS Requirements - #
  vda
  ├─vda1 vfat     FAT32            EFI BOOT    0548-C74E
  ├─vda2 swap     1                Swap        c7335240-6db1-4a89-89ce-428b77ce5c0b
  ├─vda3 btrfs                     Arch        a4011d58-cede-4059-afa0-00aaf0891d55
  └─vda4 btrfs                     Data        2f14e979-a386-44ab-93fb-78ac932fd8a0
comment



  # -  - #
  # - Mount partitions & Create subvolumes - #


  # -  - #
  # - Mount sda/vda - #
  if [ "$system_" == "BIOS" ]; then
    diskLabels_ 1 $Disk1_ $Disk2_ $Disk3_;
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount /dev/$Disk1_ /mnt;
    else
      #swapon /dev/$Disk1_;
      mount /dev/$Disk2_ /mnt;
    fi;
  elif [ "$system_" == "UEFI" ]; then
    diskLabels_ 2 $Disk1_ $Disk2_ $Disk3_ $Disk4_;
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount /dev/$Disk2_ /mnt;
    else
      #swapon /dev/$Disk2_;
      mount /dev/$Disk3_ /mnt;
    fi;
  fi;

  echo "";
  if [ "$createDiskConfirmation_" == "No" ]; then
     echo "";
     echo "";
     echo "Mounts as shown...";
     lsblk -f;
     echo "";
     echo "";
     sleep 15;
  fi;


  echo "";
  echo "Swap memory mounted...";
  free -h;
  echo "";
  sleep 15;


  # -  - #
  # - Create subvolumes - #
  btrfs subvolume create /mnt/@;
  btrfs subvolume create /mnt/@.snapshots;
  btrfs subvolume create /mnt/@cache
  btrfs subvolume create /mnt/@home;
  btrfs subvolume create /mnt/@log
  btrfs subvolume create /mnt/@var;
  #umount /mnt/boot/efi;
  umount /mnt;



  # -  - #
  # - Mount subvolumes - #



  if [ "$system_" == "BIOS" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@ /dev/$Disk1_ /mnt;
      mkdir -p /mnt/{boot/efi,home,.snapshots,var};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@home /dev/$Disk1_ /mnt/home;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@.snapshots /dev/$Disk1_ /mnt/.snapshots;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@var /dev/$Disk1_ /mnt/var;
      mkdir -p /mnt/var/{cache,log};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@cache /dev/$Disk1_ /mnt/var/cache;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@log /dev/$Disk1_ /mnt/var/log;
    else
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@ /dev/$Disk2_ /mnt;
      mkdir -p /mnt/{boot/efi,home,.snapshots,var};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@home /dev/$Disk2_ /mnt/home;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@.snapshots /dev/$Disk2_ /mnt/.snapshots;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@var /dev/$Disk2_ /mnt/var;
      mkdir -p /mnt/var/{cache,log};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@cache /dev/$Disk2_ /mnt/var/cache;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@log /dev/$Disk2_ /mnt/var/log;
    fi;
  elif [ "$system_" == "UEFI" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@ /dev/$Disk2_ /mnt;
      mkdir -p /mnt/{boot/efi,home,.snapshots,var};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@home /dev/$Disk2_ /mnt/home;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@.snapshots /dev/$Disk2_ /mnt/.snapshots;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@var /dev/$Disk2_ /mnt/var;
      mkdir -p /mnt/var/{cache,log};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@cache /dev/$Disk2_ /mnt/var/cache;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@log /dev/$Disk2_ /mnt/var/log;
    else
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@ /dev/$Disk3_ /mnt;
      mkdir -p /mnt/{boot/efi,home,.snapshots,var};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@home /dev/$Disk3_ /mnt/home;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@.snapshots /dev/$Disk3_ /mnt/.snapshots;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@var /dev/$Disk3_ /mnt/var;
      mkdir -p /mnt/var/{cache,log};
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@cache /dev/$Disk3_ /mnt/var/cache;
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@log /dev/$Disk3_ /mnt/var/log;
    fi;
  fi;



  # -  - #
  # - Create Data subvolume - #
  mkdir -p /mnt/Data;

  if [ "$system_" == "BIOS" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount /dev/$Disk2_ /mnt/Data;
    else
      mount /dev/$Disk3_ /mnt/Data;
    fi;
  elif [ "$system_" == "UEFI" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount /dev/$Disk3_ /mnt/Data;
    else
      mount /dev/$Disk4_ /mnt/Data;
    fi;
  fi;

  btrfs subvolume create /mnt/Data/@Data;
  umount /mnt/Data;



  # -  - #
  # - Mount Data subvolume - #

  if [ "$system_" == "BIOS" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@Data /dev/$Disk2_ /mnt/Data;
    else
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@Data /dev/$Disk3_ /mnt/Data;
    fi;
  elif [ "$system_" == "UEFI" ]; then
    if [ "$swapFile_" == "Yes" ] || [ "$zRam_" == "Yes" ]; then
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@Data /dev/$Disk3_ /mnt/Data;
    else
      mount -o ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=@Data /dev/$Disk4_ /mnt/Data;
    fi;
    # -  - #
    # - Efi mount (Arch alone install) - #
    mount /dev/$Disk1_ /mnt/boot/efi;
  fi;

  echo "";
  echo "lsblk -f as is right now...";
  echo "";
  lsblk -f;
  sleep 10;



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

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

  # -  - #
  # - Copy script /mnt - #
  cp Ermanno_Btrfs-Arch.sh /mnt;
  arch-chroot /mnt ./Ermanno_Btrfs-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=us" >> /etc/vconsole.conf;

  # -  - #
  # - Set hostname - #
  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) - #
  if [ "$HPLaptop_" == "No" ]; then
     pacman -S xf86-video-vesa xf86-video-ati xf86-video-intel xf86-video-amdgpu \
               xf86-video-nouveau --noconfirm --needed; fi;

  # -  - #
  # - Intel, AMD & nvidia - #
  if [ "$iMac_" == "No" ] && [ "$HPLaptop_" == "No" ]; then
     pacman -S intel-ucode amd-ucode nvidia nvidia-utils --noconfirm --needed; fi;

  # - Intel & nVidia- #
  if [ "$HPLaptop_" == "Yes" ]; then
     pacman -S intel-ucode nvidia nvidia-utils --noconfirm --needed; fi;
     # amd-ucode

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



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

  # -  - #
  # - Double boot activation - #
  if [ "$OS_Prober_" == "Yes" ]; then
     pacman -S os-prober --noconfirm --needed;
     os-prober;
     sed -i 's/#GRUB_DISABLE_OS_PROBER=false/GRUB_DISABLE_OS_PROBER=false/' /etc/default/grub;
  fi;
 


  # -  - #
  # - Install grub - #

  # - Prepare grub Btrfs configuration - #
  sed -i 's/MODULES=()/MODULES=(btrfs)/' /etc/mkinitcpio.conf;
  mkinitcpio -p linux;

  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;
  echo "";
  echo "15 seconds pause (grub-installed)...";
  echo "";
  echo "";
  sleep 15;

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



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

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

  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_;
  sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' $sshdConf_;
  sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' $sshdConf_;
  sleep 5;

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

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

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


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


  # -  - #
  # - Additional software - #

  # -  - #
  # - Programs - #
  sudo pacman -S audacious bash-completion bleachbit bpytop curl dolphin evince fastfetch 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 mate-icon-theme-faenza neofetch nmap ntfs-3g pavucontrol \
                 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;

  # sudo pacman -S pinta --noconfirm --needed;
  sudo pacman -S mariadb-clients --noconfirm --needed;

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

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

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

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


 
  # -  - #
  # - Enable ssh & NetworkManager - #
  systemctl enable sshd;
  systemctl enable NetworkManager;
  #systemctl enable systemd-resolved.service;

  #systemctl enable systemd-networkd;
  #systemctl start systemd-networkd;


  if [ "$zRam_" == "Yes" ]; then
    createzRam_; fi;

  if [ "$swapFile_" == "Yes" ]; then
    swapOnFile_; fi;


  if [ "$HPLaptop_" == "Yes" ]; then 
    NetworkFile_;
    NetworkConfiguration_;
  fi;


  rm -rf /Ermanno_Btrfs-Arch.sh;

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

}



# -  - #
# - create-zRam_ - #
function createzRam_(){
  local zRamCnf_=/etc/systemd/zram-generator.conf \
        tzRamCnf_=~/tzRamCnf_.tmp;
  echo "";
  echo "";
  echo "Install zram...";
  pacman -S zram-generator --noconfirm --needed;

  if [ -f "$zRamCnf_" ]; then
    echo "$zRamCnf_ already exists!"
  else
	  touch $tzRamCnf_;
	  echo "[zram0]" > $tzRamCnf_;
	  echo "zram-size = ram / 2" >> $tzRamCnf_;
	  #echo "compression-algorithm = zstd" >> $tzRamCnf_;
	  #echo "swap-priority = 100" >> $tzRamCnf_;
	  #echo "fs-type = swap" >> $tzRamCnf_;

    sudo mv $tzRamCnf_ $zRamCnf_;

    echo "";
    echo "$zRamCnf_ details...";
    echo "";
    cat $zRamCnf_;

    sudo systemctl daemon-reload;
    sudo systemctl start /dev/zram0;
  fi;
  echo "DONE! Reboot now and check with free -h the ZRAM installation.";
  echo "";
}



<<'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_" == "HPLaptopArch" ] || [ "$hostName_" == "HPDesktopArch" ]; then
     echo "";
     echo "This process cannot be executed on $hostName_...";
     spd-say -w "This process cannot be executed on $hostName_";
     rm -rf ~/Ermanno_Btrfs-Arch.sh;
     echo "";
     exit;
  fi;
  echo "";
}



# -  - #
# - Network file - #
function NetworkFile_(){
	local networkF_=/home/is_derayo/20-wired.network;

cat <<EOT>> $networkF_;
# -  - #
# - Network file - #
# - sudo vim /etc/systemd/network/20-wired.network; - #

# - Documentation - #
# https://www.youtube.com/watch?v=iedzVZlAbqI
# -  - #

[Match]
Name=enp0s20f0u1c2

[Network]
DHCP=yes

EOT
}



# -  - #
# - Network configuration - #
function NetworkConfiguration_(){
	local configF_=/home/is_derayo/NetworkConfiguration.sh \
	      networkF_1=/home/is_derayo/20-wired.network \
	      networkF_2=/etc/systemd/network/20-wired.network;

# -  - #
# - Configuration file - #

cat <<EOT>> $configF_;
#!/bin/bash

# -  - #
# - Configured - #
# - vim ~/NetworkConfiguration.sh - #

# - Documentation - #
# https://www.youtube.com/watch?v=iedzVZlAbqI
# -  - #

clear;

#sudo ip link set dev wlo1 up;
sudo ip link set dev enp0s20f0u1c2 up;

#sudo wpa_passphrase IS_Network_5G-1 Administrator22@ > /etc/wifi.conf;
sudo cat /etc/wifi.conf;

sudo wpa_supplicant -B -i wlo1 -D wext -c /etc/wifi.conf;

sudo systemctl enable systemd-networkd;
sudo systemctl start systemd-networkd;

sudo systemctl enable systemd-resolved;
sudo systemctl start systemd-resolved;



sudo ip link set dev wlo1 up;
sudo ip link set dev enp0s20f0u1c2 up;

sudo dhcpcd enp0s20f0u1c2;

sudo systemctl restart systemd-networkd;
sudo systemctl restart systemd-resolved;

sudo nmcli dev wifi connect IS_Network_5G-1 password "Administrator22@"

sudo shutdown now;

EOT



  chown is_derayo:is_derayo $configF_;
  chmod 775 $configF_;



  # -  - #
  # - Set wlan0 up - #
  ip link set dev wlo1 up;
  ip link set dev enp0s20f0u1c2 up;
  
  # -  - #
  # - Generate configuration of wireless connection - #
  wpa_passphrase IS_Network_5G-1 Administrator22@ > /etc/wifi.conf;

  # -  - #
  # - Connect to the wifi network - #
  wpa_supplicant -B -i wlo1 -D wext -c /etc/wifi.conf;

  systemctl enable systemd-networkd;
  systemctl start systemd-networkd;
  
  systemctl enable systemd-resolved;
  systemctl start systemd-resolved;

  mv $networkF_1 $networkF_2;
  sudo dhcpcd enp0s20f0u1c2;

  systemctl restart systemd-networkd;
  systemctl restart systemd-resolved;
  
  sleep 5
}



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

    checkComputer_;
    
    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.

cd ~/; rm -rf ~/Ermanno_Btrfs-Arch.sh; vim ~/Ermanno_Btrfs-Arch.sh;
copy & paste this content
chmod +x ~/Ermanno_Btrfs-Arch.sh; ~/./Ermanno_Btrfs-Arch.sh;

ssh-keygen -R 192.168.1.1
~/./Ermanno_Btrfs-Arch.sh;
comment



# - eof - #