#!/usr/bin/bash -e
#
# Author: Casey DeLorme
#
# Description: A fully automated installation script wired for testing with
# packer, but fully functional for a standard installation.
#
# Make sure you have a network connection, install git, clone this repository,
# and finally run this script to automate the entire installation.
#
#	pacman -Sy git
#	git clone https://git.caseydelorme.com/cdelorme/arch-desktop.git
#	cd arch-desktop
#	./setup/install.sh

# check that efivars is mounted or notify the user that it could create a problem
[ ! -d /sys/firmware/efi/efivars ] && echo "efivars not mounted; this install may leave you with a derelict system..." && sleep 30s

# verify required variables
while [[ ! "$enable_hibernation" =~ ^[yn]$ ]]; do read -p "would you like to enable hibernation (yn)? " -n1 enable_hibernation && echo ""; done
while [ -z "$root_password" ]; do read -p "please enter a root password: " -s root_password && echo ""; done
while [ -z "$username" ]; do read -p "please enter your username: " username; done
while [ -z "$password" ]; do read -p "please enter your password: " -s password && echo ""; done

# optionally print all commands
[ -n "$DEBUG" ] && set -x

# test network connection
ping -c 3 archlinux.org

# synchronize time (print for debug)
timedatectl set-ntp true
timedatectl status

# if more than one disk exists ask which to use:
export disk=$(lsblk -l | grep disk | awk '{print $1}')
if [ $(echo "$disk" | wc -l) -gt 1 ]; then
	echo -e "multiple disks found:\n$disk"
	read -p "please enter the name of the disk you want to use: " disk

	# make sure the disk exists or terminate
	if [ ! -b "/dev/${disk}" ]; then
		echo "unable to locate disk: /dev/${disk}"
		exit 1
	fi
fi

# assume sane default for swap, or intelligently size for hibernation
export swap_size=2048
if [ "$enable_hibernation" = "y" ]; then
	export ram_size=$(numfmt --to-unit=Mi --from-unit=K $(cat /proc/meminfo | grep MemTotal | awk '{print $2}'))
	if [ $ram_size -le $(numfmt --to-unit=Mi --from=iec 4G) ]; then
		export swap_size=$(echo $(($ram_size * 3 / 2)))
	else
		swap_size=$(printf %.0f $(echo "sqrt($ram_size) + $ram_size" | bc))
	fi
fi

# partition the hard drive
parted "/dev/${disk}" -s 'mklabel gpt'
parted "/dev/${disk}" -s 'mkpart primary fat32 1MiB 1024MiB'
parted "/dev/${disk}" -s 'set 1 esp on'
parted "/dev/${disk}" -s "mkpart primary linux-swap 1024MiB $((swap_size + 1024))MiB"
parted "/dev/${disk}" -s "mkpart primary btrfs $((swap_size + 1024))MiB 100%"

# acquire partitions as an array
export partitions=( $(lsblk -l -xNAME "/dev/${disk}" | grep part | awk '{print $1}') )

# format the partitions
mkfs.fat -F32 -nEFI "/dev/${partitions[0]}"
mkswap -Larch "/dev/${partitions[1]}"
mkfs.btrfs -fLarch "/dev/${partitions[2]}"

# enable and mount partitions with appropriate settings
swapon "/dev/${partitions[1]}"
mount -o "noatime,compress=lzo,space_cache,autodefrag,ssd" "/dev/${partitions[2]}" /mnt
mkdir /mnt/boot
mount "/dev/${partitions[0]}" /mnt/boot

# install base and base-devel package sets, and generate the fstab
pacstrap /mnt base base-devel
genfstab -Up /mnt > /mnt/etc/fstab

# this assumes that arch.sh and install/ exist and will copy them to continue
# @note: would prefer to copy to `/tmp` but `/mnt/tmp` from iso is a separate tmpfs
mkdir -p /mnt/srv/arch-desktop
cp -r install /mnt/srv/arch-desktop/
cp arch.sh /mnt/srv/arch-desktop/

# proceed to automate arch-chroot installation then umount and reboot with an async delay
arch-chroot /mnt /srv/arch-desktop/arch.sh
if [ -n "$PACKER" ]; then
	sed -i "/^#\?PermitRootLogin/d" /mnt/etc/ssh/sshd_config
	sed -i "/^#\?PasswordAuthentication/d" /mnt/etc/ssh/sshd_config
	echo "PasswordAuthentication yes" >> /mnt/etc/ssh/sshd_config
	echo "PermitRootLogin yes" >> /mnt/etc/ssh/sshd_config
fi
umount -R /mnt
(sleep 5 && systemctl reboot) &
