#!/bin/bash

read -p "Enter hostname: " hostname
read -p "Enter username: " user

x=1
IFS=$'\n'
for line in $(fdisk -l 2> /dev/null | grep "Disk /dev/"); do
        devices[$x]=$(echo $line | cut -d' ' -f2 | sed 's/://g')
        sizes[$x]=$(echo $line | cut -d' ' -f5)
        echo "$x $line" | cut -d',' -f1
        x=$((x+1))
done
read -p "Which device? " devnum
choice=${devices[$devnum]}
echo "${choice} selected"

# Partitionnement *avec partition swap* : 1. Boot  2. Swap  3. Root
#sgdisk -o --new=1:0:+300M --typecode=1:ef00 --new=2:0:+4G --typecode=2:8200 --new=3:0:0 --typecode=3:8300 ${choice}

# Partitionnement *sans partition swap* : 1. Boot  2. Root
sgdisk -o --new=1:0:+300M --typecode=1:ef00 --new=3:0:0 --typecode=3:8300 ${choice}

# Formatages (version *sans swap*) :
mkfs.fat -F32 ${choice}1
mkfs.ext4 ${choice}2

# *Avec swap* :
#mkfs.ext4 ${choice}3
#mkswap ${choice}2

# Montages *sans swap* :
mount ${choice}2 /mnt
mkdir -p /mnt/boot/efi
mount ${choice}1 /mnt/boot/efi

# *Avec swap* :
#swapon ${choice}2
#mount ${choice}3 /mnt

# Serveurs miroirs pour des téléchargements plus rapides
echo 'Server = http://mirror.csclub.uwaterloo.ca/archlinux/$repo/os/$arch' >> /etc/pacman.d/mirrorlist
echo 'Server = https://mirror.csclub.uwaterloo.ca/archlinux/$repo/os/$arch' >> /etc/pacman.d/mirrorlist
echo 'Server = rsync://mirror.csclub.uwaterloo.ca/archlinux/$repo/os/$arch' >> /etc/pacman.d/mirrorlist

# Connexion internet :
# - Idéalement par câble.
# - Si l'installation se fait dans une machine virtuelle, normalement la connexion fonctionne déjà.
# - Sinon :
# 	- Taper "ip a" pour voir la liste des interfaces réseaux.
# 	- Supposons que l'interface réseau Wifi est "wlan0"
# 	- Alors : "station wlan0 connect <nom_du_réseau_wifi>" pour se connecter au Wifi.

# Installation du système de base :
pacstrap /mnt base base-devel linux linux-firmware vim wget git

cp /etc/pacman.d/mirrorlist /mnt/etc/pacman.d/mirrorlist

# Génération de la table des partitions :
genfstab -U /mnt > /mnt/etc/fstab
sed -i 's/relatime/noatime,nodiratime,discard/g' /mnt/etc/fstab
#echo "tmpfs /tmp tmpfs nodev,nosuid,size=8G 0 0" >> /mnt/etc/fstab

#uuid=$(blkid | grep ${choice}3 | grep -Po '(?<= UUID=").*?(?=")')

arch-chroot /mnt

### Si on veut "swap to file" au lieu de "swap to disk" ###
#fallocate -l 4GB /swapfile
#chmod 600 /swapfile
#mkswap /swapfile
#swapon /swapfile
# Dans /etc/fstab, ajouter la ligne :
# /swapfile none swap sw 0 0

#arch-chroot /mnt pacman -Syu intel-ucode ntp openssh sshfs networkmanager net-tools xorg xorg-drivers xorg-apps wpa_supplicant dialog libnl gdm git --noconfirm
arch-chroot /mnt pacman -Syu linux linux-headers mkinitcpio intel-ucode amd-ucode ntp openssh sshfs networkmanager network-manager-applet net-tools xorg xorg-drivers xorg-apps xorg-xinit wpa_supplicant dialog libnl sddm git --noconfirm
arch-chroot /mnt systemctl enable ntpd sshd NetworkManager sddm

# BOOT SYSLINUX
#pacstrap /mnt syslinux
#arch-chroot /mnt extlinux --install /boot/syslinux/
#sed -i "s/^DEFAULT arch$/DEFAULT archfallback/g" /mnt/boot/syslinux/syslinux.cfg
#sed -i "s/APPEND.*$/APPEND root=UUID=$uuid rw/g" /mnt/boot/syslinux/syslinux.cfg
#sed -i "s%INITRD ../initramfs-linux.img$%INITRD ../intel-ucode.img,../initramfs-linux.img%g" /mnt/boot/syslinux/syslinux.cfg
#sed -i "s%INITRD ../initramfs-linux-fallback.img$%INITRD ../intel-ucode.img,../initramfs-linux-fallback.img%g" /mnt/boot/syslinux/syslinux.cfg
#cp /mnt/usr/lib/syslinux/bios/*.c32 /mnt/boot/syslinux/
#dd conv=notrunc bs=440 count=1 if=/mnt/usr/lib/syslinux/bios/gptmbr.bin of=${choice}
#sgdisk ${choice} --attributes=1:set:2

# BOOT EFI
#arch-chroot /mnt bootctl --path=/boot install
#echo "default archfallback" > /mnt/boot/loader/loader.conf
#echo "timeout 5" >> /mnt/boot/loader/loader.conf
#
#echo "title Arch Linux" > /mnt/boot/loader/entries/arch.conf
#echo "linux /vmlinuz-linux" >> /mnt/boot/loader/entries/arch.conf
#echo "initrd /intel-ucode.img" >> /mnt/boot/loader/entries/arch.conf
#echo "initrd /initramfs-linux.img" >> /mnt/boot/loader/entries/arch.conf
#echo "options root=UUID=$uuid rw" >> /mnt/boot/loader/entries/arch.conf
#
#echo "title Arch Linux Fallback" > /mnt/boot/loader/entries/archfallback.conf
#echo "linux /vmlinuz-linux" >> /mnt/boot/loader/entries/archfallback.conf
#echo "initrd /intel-ucode.img" >> /mnt/boot/loader/entries/archfallback.conf
#echo "initrd /initramfs-linux-fallback.img" >> /mnt/boot/loader/entries/archfallback.conf
#echo "options root=UUID=$uuid rw" >> /mnt/boot/loader/entries/archfallback.conf

# BOOT GRUB EFI
arch-chroot /mnt pacman -S grub efibootmgr xdg-utils xdg-user-dirs bluez bluez-utils cups pulseaudio alsa-utils pavucontrol terminus-font
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ARCH
grub-mkconfig -o /boot/grub/grub.cfg

sed -i 's/#en_CA/en_CA/g' /mnt/etc/locale.gen
sed -i 's/#fr_CA/fr_CA/g' /mnt/etc/locale.gen
sed -i 's/#en_US/en_US/g' /mnt/etc/locale.gen
arch-chroot /mnt locale-gen
echo "LANG=en_CA.UTF-8" > /mnt/etc/locale.conf

# Région géographique pour l'heure.
# "ls /usr/share/zoneinfo/" pour chercher d'autres régions
arch-chroot /mnt ln -sf /usr/share/zoneinfo/Canada/Atlantic /etc/localtime

echo "$hostname" > /mnt/etc/hostname
echo "127.0.0.1		localhost" >> /mnt/etc/hosts
echo "::1		localhost" >> /mnt/etc/hosts
echo "127.0.1.1		$hostname.localdomain	$hostname" >> /mnt/etc/hosts

sed -i 's/MODULES=()/MODULES=(overlay usbcore xhci-hcd nvme)/g' /mnt/etc/mkinitcpio.conf
sed -i 's/HOOKS.*/HOOKS=(base udev autodetect modconf block filesystems keyboard usr fsck shutdown)/g' /mnt/etc/mkinitcpio.conf

arch-chroot /mnt mkinitcpio -p linux

arch-chroot /mnt useradd -m -g users $user

#echo "$user ALL=(ALL) ALL" >> /mnt/etc/sudoers
echo "$user ALL=(ALL) NOPASSWD: ALL" >> /mnt/etc/sudoers

echo "[multilib]" >> /etc/pacman.conf
echo "Include = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf

# Gestionnaire de paquets AUR
echo "sudo pacman -Syu git --noconfirm" > /mnt/home/$user/yay.sh
echo "cd" >> /mnt/home/$user/yay.sh
echo "git clone https://aur.archlinux.org/yay.git" >> /mnt/home/$user/yay.sh
echo "cd yay" >> /mnt/home/$user/yay.sh
echo "makepkg -sri --noconfirm" >> /mnt/home/$user/yay.sh
echo "cd .." >> /mnt/home/$user/yay.sh
echo "rm -rf yay yay.sh" >> /mnt/home/$user/yay.sh
arch-chroot /mnt chown $user:users /home/$user/yay.sh
arch-chroot /mnt chmod +x /home/$user/yay.sh

sed -i 's/#Color/Color/g' /mnt/etc/pacman.conf
sed -i 's/#VerbosePkgLists/VerbosePkgLists/g' /mnt/etc/pacman.conf

#reboot
