#!/bin/bash
set -xeu

CYAN="\033[0;36m"
NORMAL="\033[0m"

# CHANGE THESE
ROOT_PART="/dev/sda2"
EFI_PART="/dev/sda1"
HOME_PART="/dev/sdb1"

TARGET_HOSTNAME="artixBTW"
USERNAME="theAtlas"

DISKFORMATTYPE="ext4"
DISKLABELTYPE="gpt"

EFI_FORMAT="FAT32"

PRE_PACMAN_PKGS="base base-devel openrc elogind-openrc"

PACMAN_PKGS="
	dbus-openrc
	linux-lts linux-firmware
	sudo vim git jj curl
	grub efibootmgr
	networkmanager-openrc
	i2pd neofetch
	xinit xsetroot xrandr xinput
	libx11 libxft libxinerama
	hsetroot dmenu
	awesome
	ghostty
"

NIX_PKGS="
	nixpkgs.ghostty
"

PKGIT="fastcompmgr"

title(){

	clear
	echo -ne "${CYAN}
################################################################################
#                                                                              #
#  						Welcome to Artix						    #
#                                                                              #
#                                    By								    #
#                                        							    #
#                                 1negroup                                     #
#                               			                                  #
#                    Modified Script Made By Hein Oke Soe                      #
################################################################################
${NORMAL}
"
}

show_disks(){

	echo
	echo "Current disks and partitions:"
	echo

	lsblk -o NAME,SIZE,FSTYPE,LABEL,UUID,MOUNTPOINTS

	echo
}

start_install(){

	show_disks
	read -rp "Look at the disk list above, then press Enter to continue..."

	read -rp "Enter hostname [${TARGET_HOSTNAME}]: " TARGET_HOSTNAME
	TARGET_HOSTNAME="${TARGET_HOSTNAME:-artixBTW}"
	export TARGET_HOSTNAME

	show_disks
	read -rp "Look at the disk list above, then press Enter to continue..."

	read -rp "Enter disk format type [${DISKFORMATTYPE}]: " input_format
	DISKFORMATTYPE="${input_format:-$DISKFORMATTYPE}"
	export DISKFORMATTYPE

	show_disks
	read -rp "Enter root partition [$ROOT_PART]: " input_root
	ROOT_PART="${input_root:-$ROOT_PART}"
	export ROOT_PART

	show_disks
	read -rp "Enter EFI partition [$EFI_PART]: " input_efi
	EFI_PART="${input_efi:-$EFI_PART}"
	export EFI_PART

	show_disks
	read -rp "Enter external home partition [$HOME_PART]: " input_home
	HOME_PART="${input_home:-$HOME_PART}"
	export HOME_PART
}

confirm_format(){

	echo
	echo "WARNING: These partitions will be formatted:"
	echo
	echo "  EFI:  $EFI_PART  -> fat32"
	echo "  ROOT: $ROOT_PART -> $DISKFORMATTYPE"
	echo "  HOME: $HOME_PART -> $DISKFORMATTYPE"
	echo
	echo "THIS WILL ERASE DATA ON THOSE PARTITIONS."
	echo

	read -rp "Type FORMAT to continue: " answer

	if [ "$answer" != "FORMAT" ]
	then
		echo "Formatting cancelled."
		exit 1
	fi
}

format_base(){

	umount -R /mnt 2>/dev/null || true
	swapoff -a 2>/dev/null || true

	mkfs.fat -F32 -n EFI "$EFI_PART"

	case "$DISKFORMATTYPE" in
		ext4)
			mkfs.ext4 -F -L ARTIX "$ROOT_PART"
			mkfs.ext4 -F -L HOME "$HOME_PART"
			;;
		btrfs)
			mkfs.btrfs -f -L ARTIX "$ROOT_PART"
			mkfs.btrfs -f -L HOME "$HOME_PART"
			;;
		*)
			echo "Unsupported disk format type: $DISKFORMATTYPE"
			echo "Supported types: ext4, btrfs"
			exit 1
			;;
	esac
}

mount_base(){

	mount "$ROOT_PART" /mnt

	mkdir -p /mnt/boot/efi
	mount "$EFI_PART" /mnt/boot/efi

	mkdir -p /mnt/home
	mount "$HOME_PART" /mnt/home


}

pkgitInstall()
{
	git clone https://git.symlinx.net/pkgit /tmp/pkgit

	cd /tmp/pkgit

	make

	make install PREFIX="/home/$USERNAME/Applications"

	read -rp "Enter pkgit PATH location: " PKGIT_PATH

	export PATH="$PKGIT_PATH:$PATH"

	cat > "/home/$USERNAME/.profile" <<EOF
		export PATH="$PKGIT_PATH:\$PATH"
EOF

	chown "$USERNAME:$USERNAME" "/home/$USERNAME/.profile"
	chown -R "$USERNAME:$USERNAME" "/home/$USERNAME/Applications"

	pkgit -i fastcompmgr
}

install_base(){

	basestrap /mnt $PRE_PACMAN_PKGS

	basestrap /mnt $PACMAN_PKGS

	pkgitInstall

	fstabgen -U /mnt >> /mnt/etc/fstab
}

chroot_part(){

	artix-chroot /mnt /bin/bash -c "$(declare -f remaining_commands); remaining_commands"
	
}

remaining_commands(){

	ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime

	hwclock --systohc

	read -rp "Remove # next to your TimeZone Then Type :wq" 

	vim /etc/locale.gen

	locale-gen

	echo 'LANG=en_US.UTF-8' > /etc/locale.conf

	read -rp "Type In ${TARGET_HOSTNAME} Then Type :wq" 

	vim /etc/conf.d/hostname

	echo $TARGET_HOSTNAME >> /etc/hostname

	read -rp "Type In \
	
	127.0.0.1        localhost
 	::1              localhost
 	127.0.1.1        ${TARGET_HOSTNAME}.localdomain  ${TARGET_HOSTNAME}
	
	Then Type :wq" 

	vim /etc/hosts

	passwd

	passwd $USERNAME

	read -rp "Comment out %wheel ALL=(ALL:ALL) ALL then type :wq"

	EDITOR=vim visudo

	grub-mkconfig -o /boot/grub/grub.cfg

	rc-update add NetworkManager default

	rc-update add elogind default


}



case "${1:-live}" in
	live)
		title
		start_install
		confirm_format
		format_base
		mount_base
		install_base
		chroot_part
		;;
esac