#!/bin/bash

#Create the luks partition
script_luks_create() {
	DRIVE="$1"
	LUKS_DEVICE="$2"
	LUKS_KEY="$3"
	LUKS_NAME="$4"
	if [ -d "/sys/firmware/efi/efivars" ]; then
		echo -en "$LUKS_KEY\n" | cryptsetup -q -v luksFormat --type luks2 "/dev/$LUKS_DEVICE" #Create encrypted partition
	else
		echo -en "$LUKS_KEY\n" | cryptsetup -q -v luksFormat --type luks1 "/dev/$LUKS_DEVICE" #Create encrypted partition
	fi

	if [[ "$(cat "/sys/block/$DRIVE/queue/rotational")" == "0" ]]; then
		echo -en "$LUKS_KEY\n" | cryptsetup --allow-discards --perf-no_read_workqueue --perf-no_write_workqueue --persistent luksOpen "/dev/$LUKS_DEVICE" "$LUKS_NAME" #Open encrypted partition
	else
		echo -en "$LUKS_KEY\n" | cryptsetup --perf-no_read_workqueue --perf-no_write_workqueue --persistent luksOpen "/dev/$LUKS_DEVICE" "$LUKS_NAME" #Open encrypted partition
	fi
}

#Create logical volumes with lvm
script_luks_lvm() {
	LUKS_DEVICE="$1"
	SWAP="$2"
	pvcreate "/dev/$LUKS_DEVICE" #Create lvm partition
	vgcreate cryptvg_luks0 "/dev/$LUKS_DEVICE" #Create lvm group
	if [ ! -d "/sys/firmware/efi/efivars" ]; then
		lvcreate -L 1G cryptvg_luks0 -n boot-arch #Create lvm logical volume with 1GB
	fi
	if [[ "$SWAP" != "disabled" ]]; then
		lvcreate -L"${SWAP}"G cryptvg_luks0 -n swap-arch #Create lvm logical volume
	fi
	lvcreate -l 50%FREE cryptvg_luks0 -n root-arch #Create lvm logical volume with half of the free space available
	lvcreate -l 100%FREE cryptvg_luks0 -n home-arch #Create lvm logical volume with all free space available
}

#-------------------------------------------------------------------------

#Prepare the btrfs filesystem for UEFI
script_btrfs() {
	DRIVE="$1"
	BOOT="$2"
	ROOT="$3"
	SWAP="$4"
	#Format the partitions
	if [ -d "/sys/firmware/efi/efivars" ]; then
		mkfs.fat -F32 "/dev/$BOOT"
	fi
	mkfs.btrfs -f -L "Root" "/dev/$ROOT"

	mount "/dev/$ROOT" /mnt
	cd /mnt || exit

	#Create subvolumes root-arch and home-arch
	btrfs subvolume create _active
	btrfs subvolume create _active/root-arch
	btrfs subvolume create _active/home-arch
	btrfs subvolume create _snapshots

	if [[ "$SWAP" != "disabled" ]]; then
		btrfs subvolume create _swap
	fi

	#Unmount
	cd / || exit
	umount /mnt

	#Mount file systems for install
	if [[ "$(cat "/sys/block/$DRIVE/queue/rotational")" == "0" ]]; then
		mount -o rw,relatime,ssd,discard=async,space_cache=v2,subvol=/_active/root-arch "/dev/$ROOT" /mnt
		mkdir /mnt/{boot,efi,home,tmp}
		mount -o rw,relatime,ssd,discard=async,space_cache=v2,subvol=/_active/home-arch "/dev/$ROOT" /mnt/home
	else
		mount -o rw,relatime,autodefrag,space_cache=v2,subvol=/_active/root-arch "/dev/$ROOT" /mnt
		mkdir /mnt/{boot,efi,home,tmp}
		mount -o rw,relatime,autodefrag,space_cache=v2,subvol=/_active/home-arch "/dev/$ROOT" /mnt/home
	fi

	#Prepare swap
	if [[ "$SWAP" != "disabled" ]]; then
		mkdir /mnt/swap
		mount -o subvol=_swap "/dev/$ROOT" /mnt/swap
		if [[ "$(cat "/sys/block/$DRIVE/queue/rotational")" == "0" ]]; then
			mount -o rw,noatime,ssd,discard=async,space_cache=v2,subvol=/_swap "/dev/$ROOT" /mnt
		else
			mount -o rw,noatime,autodefrag,space_cache=v2,subvol=/_swap "/dev/$ROOT" /mnt
		fi

		btrfs filesystem mkswapfile --size "${SWAP}"g --uuid clear /mnt/swap/swapfile
		swapon /mnt/swap/swapfile
	fi

	if [ -d "/sys/firmware/efi/efivars" ]; then
		mount -o rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro "/dev/$BOOT" /mnt/efi
	fi
}

#-------------------------------------------------------------------------

#Prepare the ext4 filesystem for UEFI
script_ext4() {
	BOOT="$1"
	ROOT="$2"
	HOME="$3"
	SWAP="$4"
	SWAP_SIZE="$5"
	#Format the partitions
	if [ -d "/sys/firmware/efi/efivars" ]; then
		mkfs.fat -F32 "/dev/$BOOT"
	fi
	mkfs.ext4 -F "/dev/$ROOT"
	mkfs.ext4 -F "/dev/$HOME"

	#Prepare swap
	if [[ "$SWAP_SIZE" != "disabled" ]]; then
		mkswap "/dev/$SWAP"
		swapon "/dev/$SWAP"
	fi

	#Mount file systems for install
	mount "/dev/$ROOT" /mnt
	mkdir /mnt/{boot,efi,home,tmp}
	if [ -d "/sys/firmware/efi/efivars" ]; then
		mount "/dev/$BOOT" /mnt/efi
	fi
	mount "/dev/$HOME" /mnt/home
}

#-------------------------------------------------------------------------

#Install systemd-boot
script_systemd-boot() {
	ENCRYPTION="$1"
	FILESYSTEM="$2"
	ROOT_PARTITION="$3"
	SWAP_ENABLED="$4"
	SECURE_BOOT="$5"

	if [[ $(cat /proc/cpuinfo | grep vendor | uniq | awk '{print $3}') == *"Intel"* ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm intel-ucode
		EOF
	else
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm amd-ucode
		EOF
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	bootctl --esp-path=/efi install
	EOF

	mkdir -p /mnt/efi/loader/entries
	mkdir -p /mnt/efi/uki/arch-linux

	cat > /mnt/efi/loader/loader.conf <<- EOF
	default arch-linux.conf
	timeout 4
	console-mode max
	editor no
	EOF

	cat > /mnt/efi/loader/entries/arch-linux.conf <<- EOF
	title Arch Linux
	sort-key archlinux-10
	efi /uki/arch-linux/arch-linux.efi
	EOF

	cat > /mnt/efi/loader/entries/arch-linux-fallback.conf <<- EOF
	title Arch Linux Fallback
	sort-key archlinux-10
	efi /uki/arch-linux/arch-linux-fallback.efi
	EOF

	cat > /mnt/efi/loader/entries/arch-linux-lts.conf <<- EOF
	title Arch Linux LTS
	sort-key archlinux-20
	efi /uki/arch-linux/arch-linux-lts.efi
	EOF

	cat > /mnt/efi/loader/entries/arch-linux-lts-fallback.conf <<- EOF
	title Arch Linux LTS Fallback
	sort-key archlinux-20
	efi /uki/arch-linux/arch-linux-lts-fallback.efi
	EOF

	if [[ "$ENCRYPTION" == "YES" ]]; then
		ROOT_UUID=$(blkid -s UUID -o value /dev/mapper/crypt_luks_root0)
	else
		ROOT_UUID=$(blkid -s UUID -o value "/dev/$ROOT_PARTITION")
	fi

	cat > /mnt/etc/mkinitcpio.d/linux.preset <<- 'EOF'
	# mkinitcpio preset file for the 'linux' package

	#ALL_config="/etc/mkinitcpio.conf"
	ALL_kver="$(pacman -Qql linux | grep 'vmlinuz$')"
	#ALL_kerneldest="/boot/vmlinuz-linux"

	#PRESETS=('default')
	PRESETS=('default' 'fallback')

	#default_config="/etc/mkinitcpio.conf"
	#default_image="/boot/initramfs-linux.img"
	default_uki="/efi/uki/arch-linux/arch-linux.efi"
	default_options="--splash /usr/share/systemd/bootctl/splash-arch.bmp"

	#fallback_config="/etc/mkinitcpio.conf"
	#fallback_image="/boot/initramfs-linux-fallback.img"
	fallback_uki="/efi/uki/arch-linux/arch-linux-fallback.efi"
	fallback_options="--skiphooks autodetect --splash /usr/share/systemd/bootctl/splash-arch.bmp"
	EOF

	cat > /mnt/etc/mkinitcpio.d/linux-lts.preset <<- 'EOF'
	# mkinitcpio preset file for the 'linux' package

	#ALL_config="/etc/mkinitcpio.conf"
	ALL_kver="$(pacman -Qql linux-lts | grep 'vmlinuz$')"
	#ALL_kerneldest="/boot/vmlinuz-linux"

	#PRESETS=('default')
	PRESETS=('default' 'fallback')

	#default_config="/etc/mkinitcpio.conf"
	#default_image="/boot/initramfs-linux.img"
	default_uki="/efi/uki/arch-linux/arch-linux-lts.efi"
	default_options="--splash /usr/share/systemd/bootctl/splash-arch.bmp"

	#fallback_config="/etc/mkinitcpio.conf"
	#fallback_image="/boot/initramfs-linux-fallback.img"
	fallback_uki="/efi/uki/arch-linux/arch-linux-lts-fallback.efi"
	fallback_options="--skiphooks autodetect --splash /usr/share/systemd/bootctl/splash-arch.bmp"
	EOF

	if [ ! -d "/mnt/etc/cmdline.d" ]; then
		mkdir /mnt/etc/cmdline.d
	fi

	if [[ "$FILESYSTEM" == "btrfs" ]]; then
		if [[ "$SWAP_ENABLED" != "disabled" ]]; then
			SWAP_OFFSET=$(btrfs inspect-internal map-swapfile -r /mnt/swap/swapfile | awk '{print $NF}')
			cat > /mnt/etc/cmdline.d/root.conf <<- EOF
			root=UUID=$ROOT_UUID rootfstype=btrfs rootflags=subvol=_active/root-arch resume=UUID=$ROOT_UUID resume_offset=$SWAP_OFFSET rw lsm=landlock,lockdown,yama,integrity,apparmor,bpf audit=1 audit_backlog_limit=4096
			EOF
		else
			cat > /mnt/etc/cmdline.d/root.conf <<- EOF
			root=UUID=$ROOT_UUID rootfstype=btrfs rootflags=subvol=_active/root-arch rw lsm=landlock,lockdown,yama,integrity,apparmor,bpf audit=1 audit_backlog_limit=4096
			EOF
		fi
	elif [[ "$FILESYSTEM" == "ext4" ]]; then
		ROOT_UUID=$(blkid -s UUID -o value "/dev/$ROOT_PARTITION")
		if [[ "$SWAP_ENABLED" != "disabled" ]]; then
			cat > /mnt/etc/cmdline.d/root.conf <<- EOF
			root=UUID=$ROOT_UUID resume=UUID=$ROOT_UUID rw
			EOF
		else
			cat > /mnt/etc/cmdline.d/root.conf <<- EOF
			root=UUID=$ROOT_UUID rw
			EOF
		fi
	fi

	if [[ "$ENCRYPTION" == "YES" ]]; then
		LUKS_ROOT_UUID=$(blkid -s UUID -o value "/dev/$ROOT_PARTITION")
		LUKS_ROOT_CYPHER=$(cryptsetup -v status crypt_luks_root0 | grep cipher | awk '{print $2}')

		cat >> /mnt/etc/crypttab <<- EOF
		crypt_luks_root0 UUID=$LUKS_ROOT_UUID none cipher=$LUKS_ROOT_CYPHER,timeout=180,tries=3
		EOF
		cp /mnt/etc/crypttab /mnt/etc/crypttab.initramfs
	fi

	mkdir -p /mnt/etc/pacman.d/hooks

	#Create systemd-boot update hook for pacman
	cat > /mnt/etc/pacman.d/hooks/systemd-boot.hook <<- EOF
	[Trigger]
	Type = Package
	Operation = Upgrade
	Target = systemd

	[Action]
	Description = Updating systemd-boot
	When = PostTransaction
	Exec = /usr/bin/bootctl --esp-path=/efi update
	EOF

	if [ -f "/mnt/etc/fstab" ]; then
		sed -i "s*fmask=0022*fmask=0077*g" /mnt/etc/fstab
		sed -i "s*dmask=0022*dmask=0077*g" /mnt/etc/fstab
	fi

	if [[ "$SECURE_BOOT" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm sbctl
		sbctl create-keys
		sbctl enroll-keys -m
		sbctl sign -s /efi/EFI/BOOT/BOOTX64.EFI
		sbctl sign -s /efi/EFI/systemd/systemd-bootx64.efi
		EOF
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	mkinitcpio -p linux
	EOF
}

#-------------------------------------------------------------------------

#Install grub
script_grub() {
	ENCRYPTION="$1"
	ROOT_DRIVE="$2"
	LUKS_ROOT_PARTITION="$3"
	LUKS_KEY="$4"

	mkdir /mnt/hostlvm
	mount --bind /run/lvm /mnt/hostlvm

	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm grub
	EOF

	if [[ "$ENCRYPTION" == "YES" ]]; then
		#Write grub boot configuration
		LUKS_ROOT_UUID=$(blkid -s UUID -o value "/dev/$LUKS_ROOT_PARTITION")
		arch-chroot /mnt /bin/bash <<- EOF
		sed -i 's+^#\?\(GRUB_CMDLINE_LINUX_DEFAULT\)=.*$+\1=""+' /etc/default/grub
		sed -i 's+^#\?\(GRUB_CMDLINE_LINUX\)=.*$+\1="rd.luks.name='$LUKS_ROOT_UUID'=crypt_luks_root0 rd.luks.key='$LUKS_ROOT_UUID'=/root/crypt_luks_root0.keyfile"+' /etc/default/grub
		sed -i 's/#GRUB_ENABLE_CRYPTODISK/GRUB_ENABLE_CRYPTODISK/' /etc/default/grub
		sed -i 's+^#\?\(GRUB_PRELOAD_MODULES\)=.*$+\1="part_gpt part_msdos cryptodisk crypto luks lvm"+' /etc/default/grub
		sed -i "s+^#\?\(FILES\)=.*$+\1=(/root/crypt_luks_root0.keyfile)+" /mnt/etc/mkinitcpio.conf
		dd bs=512 count=4 if=/dev/urandom of=/root/crypt_luks_root0.keyfile iflag=fullblock
		chmod 000 /root/crypt_luks_root0.keyfile
		echo -en "$LUKS_KEY\n" | cryptsetup -v luksAddKey /dev/$LUKS_ROOT_UUID /root/crypt_luks_root0.keyfile
		EOF
	else
		#Write grub boot configuration
		arch-chroot /mnt /bin/bash <<- EOF
		sed -i 's+^#\?\(GRUB_CMDLINE_LINUX_DEFAULT\)=.*$+\1=""+' /etc/default/grub
		EOF
	fi

	#Install grub bootloader
	arch-chroot /mnt /bin/bash <<- EOF
	ln -s /hostlvm /run/lvm
	grub-install --target=i386-pc --recheck /dev/$ROOT_DRIVE
	EOF

	#Generate grub configuration
	arch-chroot /mnt /bin/bash <<- EOF
	ln -s /hostlvm /run/lvm
	grub-mkconfig -o /boot/grub/grub.cfg
	EOF
}

#-------------------------------------------------------------------------

#Generate mirrorlist
script_prep_mirrors() {
	INSTALL_SEVENTHCORE_LOCAL_REPO="$1"
	REFLECTOR_COUNTRY_CODE=$(curl -4 ifconfig.co/country-iso)
	timedatectl set-ntp true
	pacman -S --noconfirm pacman-contrib
	sed -i 's/^#Para/Para/' /etc/pacman.conf
	pacman -S --noconfirm reflector rsync
	cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup

	echo "# Generated on $(date +"%Y-%m-%d %H:%M:%S")" > /etc/pacman.d/mirrorlist
	echo '' >> /etc/pacman.d/mirrorlist
	if [[ "$INSTALL_SEVENTHCORE_LOCAL_REPO" == "YES" ]]; then
		{
		echo '# Local mirror'
		echo 'Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch'
		echo ''
		} >> /etc/pacman.d/mirrorlist
	fi
	echo "# $REFLECTOR_COUNTRY_CODE mirrors" >> /etc/pacman.d/mirrorlist
	reflector --protocol https --protocol rsync --country "$REFLECTOR_COUNTRY_CODE" --latest 10 | grep Server >> /etc/pacman.d/mirrorlist
}

#-------------------------------------------------------------------------

#Install Arch Linux and generate fstab
script_base_install() {
	pacstrap /mnt base base-devel linux linux-lts linux-firmware linux-firmware-qlogic btrfs-progs dhcpcd rsync nano util-linux usbutils inetutils
	genfstab -U /mnt >> /mnt/etc/fstab

	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Syu --noconfirm apparmor tk bash-completion htop glances screen rsync git wget cifs-utils lm_sensors lshw btrfs-progs dosfstools exfat-utils f2fs-tools e2fsprogs jfsutils nilfs-utils ntfs-3g udftools xfsprogs openvpn socat polkit linux-headers linux-lts-headers linux-firmware acpid sshfs tmux smartmontools haveged
	systemctl enable acpid.service
	systemctl enable smartd.service
	systemctl enable haveged.service
	EOF
}

#-------------------------------------------------------------------------

script_encryption_install() {
	INSTALL_FILESYSTEM="$1"
	if [[ "$INSTALL_FILESYSTEM" == "ext4" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		sed -i "s+^#\?\(HOOKS\)=.*$+\1=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block sd-encrypt lvm2 filesystems resume fsck)+" /etc/mkinitcpio.conf
		EOF
	else
		arch-chroot /mnt /bin/bash <<- EOF
		sed -i "s+^#\?\(BINARIES\)=.*$+\1=(btrfs)+" /etc/mkinitcpio.conf
		sed -i "s+^#\?\(HOOKS\)=.*$+\1=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block sd-encrypt filesystems btrfs resume fsck)+" /etc/mkinitcpio.conf
		EOF
	fi
}

#-------------------------------------------------------------------------

script_nonencryption_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	sed -i "s+^#\?\(BINARIES\)=.*$+\1=(btrfs)+" /etc/mkinitcpio.conf
	sed -i "s+^#\?\(HOOKS\)=.*$+\1=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block filesystems btrfs fsck resume)+" /etc/mkinitcpio.conf
	EOF
}

#-------------------------------------------------------------------------

#Enable multilib repository
script_lib32_install() {
	echo "[multilib]" >> /mnt/etc/pacman.conf
	echo "Include = /etc/pacman.d/mirrorlist" >> /mnt/etc/pacman.conf
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm
	EOF
}

#-------------------------------------------------------------------------

#Install arch user repository pre-built repositories
script_aur_install() {
	INSTALL_AUR_PREBUILD="$1"
	INSTALL_AUR_PREBUILD_CORE="$2"
	INSTALL_AUR_PREBUILD_CORE_PACKAGES="$3"
	INSTALL_AUR_PREBUILD_DOTNET="$4"
	INSTALL_AUR_PREBUILD_FFMPEG="$5"
	INSTALL_AUR_PREBUILD_NVIDIA390="$6"
	INSTALL_AUR_PREBUILD_NVIDIA470="$7"
	INSTALL_AUR_PREBUILD_NVIDIA550="$8"
	if [[ "$INSTALL_AUR_PREBUILD" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt <<- 'EOF'
		[aur-prebuilt]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi

	if [[ "$INSTALL_AUR_PREBUILD_CORE" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-core
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-core <<- 'EOF'
		[aur-prebuilt-core]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF

		if [[ "$INSTALL_AUR_PREBUILD_CORE_PACKAGES" == "YES" ]]; then
			arch-chroot /mnt /bin/bash <<- EOF
			pacman -Syu --noconfirm 7thcore-essentials
			EOF
		fi
	fi

	if [[ "$INSTALL_AUR_PREBUILD_DOTNET" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-dotnet
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-dotnet <<- 'EOF'
		[aur-prebuilt-dotnet]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi

	if [[ "$INSTALL_AUR_PREBUILD_FFMPEG" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-ffmpeg
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-ffmpeg <<- 'EOF'
		[aur-prebuilt-ffmpeg]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi

	if [[ "$INSTALL_AUR_PREBUILD_NVIDIA390" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-nvidia390
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-nvidia390 <<- 'EOF'
		[aur-prebuilt-nvidia390]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi

	if [[ "$INSTALL_AUR_PREBUILD_NVIDIA470" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-nvidia470
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-nvidia470 <<- 'EOF'
		[aur-prebuilt-nvidia470]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi

	if [[ "$INSTALL_AUR_PREBUILD_NVIDIA550" == "YES" ]]; then
		cat >> /mnt/etc/pacman.conf <<- EOF
		Include = /etc/pacman.d/aur-prebuilt-nvidia550
		EOF

		cat > /mnt/etc/pacman.d/aur-prebuilt-nvidia550 <<- 'EOF'
		[aur-prebuilt-nvidia550]
		SigLevel = Optional TrustAll
		Server = https://repository.7thcore.eu/archlinux/$repo/os/$arch
		EOF
	fi
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm
	EOF
}

#-------------------------------------------------------------------------

#Set language and time zone
script_lang_install() {
	INSTALL_LANGUAGE="$1"
	if [[ "$INSTALL_LANGUAGE" == "EN_SL" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		#Set language config
		echo "C.UTF-8 UTF-8" > /etc/locale.gen
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
		echo "sl_SI.UTF-8 UTF-8" >> /etc/locale.gen
		echo "LANG=en_US.UTF-8" > /etc/locale.conf
		echo "LANGUAGE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_CTYPE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_NUMERIC=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_TIME=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_COLLATE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MONETARY=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MESSAGES=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_PAPER=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_NAME=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_ADDRESS=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_TELEPHONE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MEASUREMENT=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_IDENTIFICATION=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_ALL=" >> /etc/locale.conf
		locale-gen
		echo "KEYMAP=slovene" > /etc/vconsole.conf
		EOF
	elif [[ "$INSTALL_LANGUAGE" == "EN" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		#Set language config
		echo "C.UTF-8 UTF-8" > /etc/locale.gen
		echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
		echo "LANG=en_US.UTF-8" > /etc/locale.conf
		echo "LANGUAGE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_CTYPE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_NUMERIC=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_TIME=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_COLLATE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_MONETARY=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_MESSAGES=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_PAPER=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_NAME=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_ADDRESS=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_TELEPHONE=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_MEASUREMENT=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_IDENTIFICATION=en_US.UTF-8" >> /etc/locale.conf
		echo "LC_ALL=" >> /etc/locale.conf
		locale-gen
		echo "KEYMAP=us" > /etc/vconsole.conf
		EOF
	elif [[ "$INSTALL_LANGUAGE" == "SL" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		#Set language config
		echo "C.UTF-8 UTF-8" > /etc/locale.gen
		echo "sl_SI.UTF-8 UTF-8" > /etc/locale.gen
		echo "sl_SI.UTF-8 UTF-8" >> /etc/locale.gen
		echo "LANG=sl_SI.UTF-8" > /etc/locale.conf
		echo "LANGUAGE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_CTYPE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_NUMERIC=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_TIME=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_COLLATE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MONETARY=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MESSAGES=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_PAPER=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_NAME=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_ADDRESS=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_TELEPHONE=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_MEASUREMENT=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_IDENTIFICATION=sl_SI.UTF-8" >> /etc/locale.conf
		echo "LC_ALL=" >> /etc/locale.conf
		locale-gen
		echo "KEYMAP=slovene" > /etc/vconsole.conf
		EOF
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	rm -rf /etc/localtime
	ln -s /usr/share/zoneinfo/Europe/Ljubljana /etc/localtime
	EOF
}

#-------------------------------------------------------------------------

#Add user, change passwords, add user to sudoers and set hostname
script_user_install() {
	script_shell_install() {
		if [ -f "/mnt/root/$1" ]; then
			rm "/mnt/root/$1"
		fi
		if [ -f "/mnt/root/$3" ]; then
			rm "/mnt/root/$3"
		fi
		if [ -f "/mnt/root/.tmux.conf" ]; then
			rm "/mnt/root/.tmux.conf"
		fi
		cp "/etc/7thcore/conf/$2" "/mnt/root/$1"
		cp "/etc/7thcore/conf/$4" "/mnt/root/$3"
		cp "/etc/7thcore/conf/$5" "/mnt/root/.tmux.conf"
		if [ -n "$(ls -A /mnt/home/)" ]; then
			for USER_SHELL_INSTALL in /mnt/home/*; do
				if [ -f "$USER_SHELL_INSTALL/$1" ]; then
					rm "$USER_SHELL_INSTALL/$1"
				fi
				if [ -f "$USER_SHELL_INSTALL/$3" ]; then
					rm "$USER_SHELL_INSTALL/$3"
				fi
				if [ -f "$USER_SHELL_INSTALL/.tmux.conf" ]; then
					rm "$USER_SHELL_INSTALL/.tmux.conf"
				fi
				cp "/etc/7thcore/conf/$2" "$USER_SHELL_INSTALL/$1"
				cp "/etc/7thcore/conf/$4" "$USER_SHELL_INSTALL/$3"
				cp "/etc/7thcore/conf/$5" "$USER_SHELL_INSTALL/.tmux.conf"
				chown "$(stat -c "%u" "$USER_SHELL_INSTALL")":"$(stat -c "%g" "$USER_SHELL_INSTALL")" "$USER_SHELL_INSTALL/$1" "$USER_SHELL_INSTALL/$3" "$USER_SHELL_INSTALL/.tmux.conf"
			done
		fi

		if [ -f "/mnt/etc/nanorc" ]; then
			rm "/mnt/etc/nanorc"
		fi
		cp /etc/7thcore/conf/nanorc /mnt/etc/nanorc
	}

	INSTALL_USER="$1"
	INSTALL_USER_PASS="$2"
	INSTALL_ROOT_PASS="$3"
	INSTALL_HOSTNAME="$4"
	INSTALL_SHELL="$5"

	arch-chroot /mnt /bin/bash <<- EOF
	groupadd --system audit
	systemctl enable audit
	EOF

	if [[ "$INSTALL_SHELL" == "ZSH_SHELL" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Syu --noconfirm zsh zsh-completions zsh-autosuggestions zsh-syntax-highlighting zsh-history-substring-search
		useradd -m -g users -G wheel,storage,power,audit,systemd-journal -s /bin/zsh $INSTALL_USER
		chsh --shell /bin/zsh root
		EOF
		script_shell_install ".zshrc" "zshrc" ".zshenv" "zshenv" "tmux_zsh"
	elif [[ "$INSTALL_SHELL" == "BASH_SHELL" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		useradd -m -g users -G wheel,storage,power,audit,systemd-journal -s /bin/bash $INSTALL_USER
		EOF
		script_shell_install ".bashrc" "bashrc" ".bash_profile" "bash_profile" "tmux_bash"
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	echo -en "$INSTALL_USER_PASS\n$INSTALL_USER_PASS\n" | passwd $INSTALL_USER
	echo -en "$INSTALL_ROOT_PASS\n$INSTALL_ROOT_PASS\n" | passwd root

	echo "$INSTALL_HOSTNAME" > /etc/hostname
	sed -i 's/localhost/$INSTALL_HOSTNAME/g' /etc/hosts
	sed -i 's/$INSTALL_HOSTNAME.localdomain/localhost.localdomain/g' /etc/hosts
	sed -i 's/log_group = root/log_group = audit/g' /etc/audit/auditd.conf
	chmod 750 /var/log/audit
	EOF

	if [ ! -d /mnt/etc/sudoers.d ]; then
		mkdir /mnt/etc/sudoers.d
	fi

	if [ ! -d /mnt/etc/tmpfiles.d ]; then
		mkdir /mnt/etc/tmpfiles.d
	fi
	cat > /mnt/etc/tmpfiles.d/audit.conf <<- EOF
	z /var/log/audit 750 root audit - -
	EOF

	cat > "/mnt/etc/sudoers.d/user-$INSTALL_USER" <<- EOF
	$INSTALL_USER ALL=(ALL) ALL
	EOF
}

#-------------------------------------------------------------------------

#Install OpenSSH
script_ssh_install() {
	INSTALL_USER="$1"
	INSTALL_SSH_PORT="$2"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm openssh keychain
	EOF

	cat > /mnt/etc/ssh/sshd_config <<- EOF
	# Include drop-in configurations
	Include /etc/ssh/sshd_config.d/*.conf

	# This is the sshd server system-wide configuration file. See
	# sshd_config(5) for more information.

	# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/bin

	# The strategy used for options in the default sshd_config shipped with
	# OpenSSH is to specify options with their default value where
	# possible, but leave them commented. Uncommented options override the
	# default value.

	Port $INSTALL_SSH_PORT
	#AddressFamily any
	#ListenAddress 0.0.0.0
	#ListenAddress ::

	#HostKey /etc/ssh/ssh_host_rsa_key
	#HostKey /etc/ssh/ssh_host_ecdsa_key
	#HostKey /etc/ssh/ssh_host_ed25519_key

	# Ciphers and keying
	#RekeyLimit default none

	# Logging
	#SyslogFacility AUTH
	#LogLevel INFO

	# Authentication:

	AllowUsers $INSTALL_USER
	#LoginGraceTime 2m
	#PermitRootLogin prohibit-password
	#StrictModes yes
	#MaxAuthTries 6
	#MaxSessions 10

	#PubkeyAuthentication yes

	# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
	# but this is overridden so installations will only check .ssh/authorized_keys
	AuthorizedKeysFile	.ssh/authorized_keys

	#AuthorizedPrincipalsFile none

	#AuthorizedKeysCommand none
	#AuthorizedKeysCommandUser nobody

	# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
	#HostbasedAuthentication no
	# Change to yes if you don't trust ~/.ssh/known_hosts for
	# HostbasedAuthentication
	#IgnoreUserKnownHosts no
	# Don't read the user's ~/.rhosts and ~/.shosts files
	#IgnoreRhosts yes

	# To disable tunneled clear text passwords, change to "no" here!
	#PasswordAuthentication no
	PasswordAuthentication yes
	PermitEmptyPasswords no

	# Change to "no" to disable keyboard-interactive authentication. Depending on
	# the system's configuration, this may involve passwords, challenge-response,
	# one-time passwords or some combination of these and other methods.
	#KbdInteractiveAuthentication yes

	# Kerberos options
	#KerberosAuthentication no
	#KerberosOrLocalPasswd yes
	#KerberosTicketCleanup yes
	#KerberosGetAFSToken no

	# GSSAPI options
	#GSSAPIAuthentication no
	#GSSAPICleanupCredentials yes

	# Set this to 'yes' to enable PAM authentication, account processing,
	# and session processing. If this is enabled, PAM authentication will
	# be allowed through the KbdInteractiveAuthentication and
	# PasswordAuthentication. Depending on your PAM configuration,
	# PAM authentication via KbdInteractiveAuthentication may bypass
	# the setting of "PermitRootLogin prohibit-password".
	# If you just want the PAM account and session checks to run without
	# PAM authentication, then enable this but set PasswordAuthentication
	# and KbdInteractiveAuthentication to 'no'.
	UsePAM yes

	#AllowAgentForwarding yes
	#AllowTcpForwarding yes
	#GatewayPorts no
	#X11Forwarding no
	#X11DisplayOffset 10
	#X11UseLocalhost yes
	#PermitTTY yes
	PrintMotd no
	#PrintLastLog yes
	#TCPKeepAlive yes
	#PermitUserEnvironment no
	#Compression delayed
	#ClientAliveInterval 0
	#ClientAliveCountMax 3
	#UseDNS no
	#PidFile /run/sshd.pid
	#MaxStartups 10:30:100
	#PermitTunnel no
	#ChrootDirectory none
	#VersionAddendum none

	# no default banner path
	#Banner none

	# override default of no subsystems
	Subsystem	sftp	/usr/lib/ssh/sftp-server

	# Example of overriding settings on a per-user basis
	#Match User anoncvs
	#	X11Forwarding no
	#	AllowTcpForwarding no
	#	PermitTTY no
	#	ForceCommand cvs server
	EOF

	if [ ! -d "/mnt/home/$INSTALL_USER/.config/systemd/user/" ]; then
		mkdir -p "/mnt/home/$INSTALL_USER/.config/systemd/user/"
	fi

	cat > "/mnt/home/$INSTALL_USER/.config/systemd/user/ssh-agent.service" <<- 'EOF'
	[Unit]
	Description=SSH key agent

	[Service]
	Type=simple
	Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
	Environment=DISPLAY=:0
	ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK

	[Install]
	WantedBy=default.target
	EOF

	arch-chroot /mnt /bin/bash <<- EOF
	systemctl enable sshd.service
	EOF

	arch-chroot /mnt /bin/bash <<- EOF
	chown -R $INSTALL_USER:users /home/$INSTALL_USER/.config
	EOF
}

#-------------------------------------------------------------------------

#Install network time protocol
script_ntp_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm ntp
	systemctl enable ntpd.service
	ntupdate -s time.nist.gov
	hwclock --systohc --utc
	EOF
}

#-------------------------------------------------------------------------

#Install ufw firewall
script_ufw_install() {
	INSTALL_SSH="$1"
	INSTALL_SSH_PORT="$2"
	INSTALL_SMB="$3"
	INSTALL_STEAM="$4"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm ufw
	systemctl enable ufw.service
	EOF

	if [[ "$INSTALL_SSH" == "YES" ]]; then
		cat > /mnt/etc/ufw/applications.d/ufw-openssh-custom <<- EOF
		[OpenSSH-Custom]
		title=OpenSSH-Custom
		description=OpenSSH is the premier connectivity tool for remote login with the SSH protocol. It encrypts all traffic to eliminate eavesdropping, connection hijacking, and other attacks. In addition, OpenSSH provides a large suite of secure tunneling capabilities, several authentication methods, and sophisticated configuration options.
		ports=$INSTALL_SSH_PORT/tcp
		EOF
	fi

	cat > /mnt/etc/ufw/applications.d/ufw-steam <<- EOF
	[Steam]
	title=Steam
	description=Steam is a popular game distribution platform by Valve
	ports=3478/udp|4379/udp|4380/udp|27015:27030/udp|27015:27030/tcp|27000:27030/udp|27032:27035/udp|27037:27100/udp

	[Steam In-Home Streaming]
	title=Steam In-Home Streaming
	description=Steam's in-home streaming solution
	ports=27031/udp|27036/udp|27036/tcp|27037/tcp
	EOF

	cat > /mnt/etc/ufw/applications.d/ufw-kdeconnect <<- EOF
	[KDEConnect]
	title=KDEConnect
	description=KDE's phone integration tool
	ports=1714:1764/udp|1714:1764/tcp
	EOF

	arch-chroot /mnt /bin/bash <<- EOF
	ufw app update Steam
	ufw app update KDEConnect
	ufw allow KDEConnect
	ufw default deny incoming
	ufw default allow outgoing
	EOF

	if [[ "$INSTALL_SSH" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		ufw app update OpenSSH-Custom
		ufw allow OpenSSH-Custom
		EOF
	fi

	if [[ "$INSTALL_SMB" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		ufw allow CIFS
	EOF
	fi

	if [[ "$INSTALL_STEAM" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		ufw allow Steam
	EOF
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	ufw enable
	EOF
}


#-------------------------------------------------------------------------

#Install samba file sharing
script_smb_install() {
	INSTALL_USER="$1"
	INSTALL_HOSTNAME="$2"
	INSTALL_SMBPSW="$3"
	INSTALL_SMBWG="$4"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm samba smbclient
	EOF
	cat > /mnt/etc/samba/smb.conf <<- EOF
	#======================= Global Settings =====================================
	[global]

	# network socket options
	;  socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536

	# workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH
	workgroup = $INSTALL_SMBWG

	# server string is the equivalent of the NT Description field
	server string = Arch Linux

	# server netbios name
	netbios name = $INSTALL_HOSTNAME

	# Server role
	server role = standalone server

	# Allow hosts
	;   hosts allow = 192.168.1. 192.168.2. 127.

	# Logs
	logging = systemd

	# Put a capping on the size of the log files (in Kb).
	max log size = 50

	# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
	# via DNS nslookups. The default is NO.
	dns proxy = no

	# SMB1 protocol is considered a security risk and most clients at least support SMB2.
	# So it makes sense to deny connection attempts to the server via SMB1 protocol:
	server min protocol = SMB3

	#============================ Share Definitions ==============================
	[private]
		comment = PRIVATE
		path = /home/$INSTALL_USER/Shares/Local/Private
		valid users = $INSTALL_USER
		public = no
		writable = yes
		printable = no
		vfs objects = recycle
		recycle:repository = .recycle
		recycle:keeptree = yes
		recycle:versions = yes

	[public]
		comment = PUBLIC
		path = /home/$INSTALL_USER/Shares/Local/Public
		public = yes
		writable = yes
		printable = no
		vfs objects = recycle
		recycle:repository = .recycle
		recycle:keeptree = yes
		recycle:versions = yes
	EOF

	#Generate shares for user
	mkdir -p "/mnt/home/$INSTALL_USER/Shares/Local/Private"
	mkdir -p "/mnt/home/$INSTALL_USER/Shares/Local/Public"

	#Test samba conf, add samba user and give samba acces to it's logs
	arch-chroot /mnt /bin/bash <<- EOF
	chown -R $INSTALL_USER:samba /home/$INSTALL_USER/Shares/Local/Private
	chown -R $INSTALL_USER:samba /home/$INSTALL_USER/Shares/Local/Public
	testparm
	echo -en "$INSTALL_SMBPSW\n$INSTALL_SMBPSW\n" | smbpasswd -a $INSTALL_USER
	chown $INSTALL_USER -R /home/$INSTALL_USER/Shares
	systemctl enable smb.service
	systemctl enable nmb.service
	systemctl enable winbind.service
	EOF
}

#-------------------------------------------------------------------------

#Install psotfix relay
script_postfix_install() {
	INSTALL_POSTFIXUSER="$1"
	INSTALL_POSTFIXPSW="$2"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm postfix s-nail
	EOF

	cat >> /mnt/etc/postfix/main.cf <<- EOF
	relayhost = [smtp.gmail.com]:587
	smtp_sasl_auth_enable = yes
	smtp_sasl_password_maps = lmdb:/etc/postfix/sasl_passwd
	smtp_sasl_security_options = noanonymous
	smtp_tls_CApath = /etc/ssl/certs
	smtpd_tls_CApath = /etc/ssl/certs
	smtp_use_tls = yes
	EOF

	cat > /mnt/etc/postfix/sasl_passwd <<- EOF
	[smtp.gmail.com]:587 $INSTALL_POSTFIXUSER:$INSTALL_POSTFIXPSW
	EOF

	arch-chroot /mnt /bin/bash <<- EOF
	chmod 400 /etc/postfix/sasl_passwd
	postmap /etc/postfix/sasl_passwd
	systemctl enable postfix
	EOF
}

#-------------------------------------------------------------------------

script_kde_install() {
	INSTALL_USER="$1"
	INSTALL_KDE_FULL="$2"
	INSTALL_KDE_AUTOLOGIN="$3"
	INSTALL_KDE_GAMES="$4"
	INSTALL_NMCONNECTIONEDITOR="$5"
	INSTALL_LIB32="$6"


	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm plasma-desktop ksystemlog plasma-systemmonitor libksysguard kinfocenter powerdevil konsole kate kscreen krdc krdp kdevelop kile konversation kwallet kwallet-pam kwalletmanager kcalc kfind kolourpaint dolphin kdenetwork-filesharing plasma-pa plasma-vault okular gwenview filelight spectacle gnupg plasma-browser-integration plasma-integration plasma-nm ksshaskpass kde-cli-tools kde-gtk-config ark partitionmanager kompare sweeper ghostwriter kgpg kcharselect kcolorchooser haruna elisa keysmith firefox cryfs encfs 7zip unrar sddm sddm-kcm pipewire pipewire-jack pipewire-v4l2 pipewire-pulse pipewire-zeroconf sof-firmware procps-ng sof-firmware libnotify python-notify2 python-psutil
	EOF

	if [[ "$INSTALL_KDE_FULL" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm kdeconnect bluedevil bluez bluez-utils ktimer neochat kamoso krita gimp kid3 thunderbird vlc vlc-plugins-all libreoffice-fresh obs-studio openrgb k3b cdrtools dvd+rw-tools vcdimager emovix cdrdao cdparanoia
		pacman -Sy --noconfirm v4l2loopback-dkms
		modprobe v4l2loopback
		EOF
	fi

	cat > /mnt/etc/profile.d/sshaskpass.sh <<- EOF
	export SSH_ASKPASS="/usr/bin/ksshaskpass"
	EOF

	if [[ "$INSTALL_LIB32" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm lib32-pipewire lib32-pipewire-jack lib32-pipewire-v4l2
	EOF
	fi

	if [ ! -d "/mnt/etc/systemd/system/sddm.service.d" ]; then
		mkdir -p /mnt/etc/systemd/system/sddm.service.d/
	fi

	cat > /mnt/etc/systemd/system/sddm.service.d/keyringmode.conf <<- EOF
	[Service]
	KeyringMode=inherit
	EOF

	if [ ! -d "/mnt/etc/sddm.conf.d" ]; then
		mkdir /mnt/etc/sddm.conf.d
	fi

	if [[ "$INSTALL_KDE_AUTOLOGIN" == "YES" ]] || [[ "$INSTALL_KDE_AUTOLOGIN" == "YES-LOCK" ]]; then
		cat > /mnt/etc/sddm.conf.d/kde_settings.conf <<- EOF
		[Autologin]
		Relogin=false
		Session=plasma
		User=$INSTALL_USER
		EOF
	fi

	if [ ! -d "/mnt/home/$INSTALL_USER/.config" ]; then
		mkdir -p "/mnt/home/$INSTALL_USER/.config"
	fi

	if [ ! -d "/mnt/home/$INSTALL_USER/.config/autostart" ]; then
		mkdir -p "/mnt/home/$INSTALL_USER/.config/autostart"
	fi

	cat > "/mnt/home/$INSTALL_USER/.config/autostart/apparmor-notify.desktop" <<- EOF
	[Desktop Entry]
	Type=Application
	Name=AppArmor Notify
	Comment=Receive on screen notifications of AppArmor denials
	TryExec=aa-notify
	Exec=aa-notify -p -s 1 -w 60 -f /var/log/audit/audit.log
	StartupNotify=false
	NoDisplay=true
	EOF

	if [[ "$INSTALL_KDE_AUTOLOGIN" == "YES-LOCK" ]]; then
		cat > "/mnt/home/$INSTALL_USER/.config/kscreenlockerrc" <<- EOF
		[Daemon]
		LockOnStart=true
		EOF

		arch-chroot /mnt /bin/bash <<- EOF
		chown -R $INSTALL_USER:users /home/$INSTALL_USER/.config
		EOF
	fi

	cat >> "/mnt/etc/sddm.conf.d/kde_settings.conf" <<- EOF
	[General]
	HaltCommand=/usr/bin/systemctl poweroff
	RebootCommand=/usr/bin/systemctl reboot

	[Theme]
	Current=

	[Users]
	MaximumUid=60513
	MinimumUid=1000
	EOF

	arch-chroot /mnt /bin/bash <<- EOF
	systemctl enable sddm.service
	localectl set-x11-keymap si
	EOF

	#Install KDE games
	if [[ "$INSTALL_KDE_GAMES" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm kmahjongg ktuberling kmines ksudoku picmi knavalbattle kpat kbounce kbreakout kdiamond kiriki extremetuxracer supertux supertuxkart hedgewars 0ad 0ad-data
		EOF
	fi

	#Install kde network manager gui and vpn plugins for it
	if [[ "$INSTALL_NMCONNECTIONEDITOR" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm networkmanager networkmanager-openconnect networkmanager-openvpn networkmanager-pptp networkmanager-vpnc networkmanager-qt plasma-nm
		systemctl enable NetworkManager.service
		systemctl enable NetworkManager-wait-online.service
		EOF
	fi

	if [[ "$INSTALL_KDE_FULL" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		systemctl enable bluetooth.service
		EOF
	fi
}

#-------------------------------------------------------------------------

#Install printer support
script_printer_install() {
	KDE_PRINTER="$1"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm cups hplip"
	systemctl enable cups.service"
	EOF

	if [[ "$KDE_PRINTER" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm print-manager skanpage
		EOF
	fi
}

#-------------------------------------------------------------------------

#Install amd gpu drivers
script_amd_install() {
	arch-chroot /mnt /bin/bash <<- EOF
 	pacman -Sy --noconfirm mesa xf86-video-amdgpu vulkan-radeon libva-mesa-driver opencl-mesa
	EOF
}

#-------------------------------------------------------------------------

#Install intel gpu drivers
script_intel_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm mesa xf86-video-intel vulkan-intel intel-media-driver
	EOF
}

#-------------------------------------------------------------------------

#Install nvidia gpu drivers
script_nvidia_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm nvidia nvidia-settings nvidia-utils virtualgl
	EOF
}

#-------------------------------------------------------------------------

#Install steam
script_steam_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm steam ttf-liberation
	EOF
}

#-------------------------------------------------------------------------

#Install the wine compatibility layer
script_wine_install() {
	INSTALL_LIB32="$1"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm wine winetricks wine-mono wine_gecko libpulse libxml2 mpg123 lcms2 giflib libpng gnutls gst-plugins-base gst-plugins-good
	EOF
	if [[ "$LIB32" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm lib32-libpulse lib32-libxml2 lib32-mpg123 lib32-lcms2 lib32-giflib lib32-libpng lib32-gnutls lib32-gst-plugins-base lib32-gst-plugins-good
		EOF
	fi
	cat > /mnt/etc/pacman.d/hooks/stop-wine-associations.hook <<- 'EOF'
	[Trigger]
	Type = File
	Operation = Install
	Operation = Upgrade
	Target = usr/share/wine/wine.inf

	[Action]
	Description = Stopping Wine from hijacking file associations...
	When = PostTransaction
	Exec = /bin/sh -c '/usr/bin/grep -q "HKCU,\"Software\\\Wine\\\FileOpenAssociations\",\"Enable\",2,\"N\"" /usr/share/wine/wine.inf || /usr/bin/sed -i "s/\[Services\]/\[Services\]\nHKCU,\"Software\\\Wine\\\FileOpenAssociations\",\"Enable\",2,\"N\"/g" /usr/share/wine/wine.inf'
	EOF
}

#-------------------------------------------------------------------------

#Install lutris
script_lutris_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm lutris
	EOF
}

#-------------------------------------------------------------------------

#Install nextcloud client
script_nextcloud_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm nextcloud-client
	EOF
}

#-------------------------------------------------------------------------

#Install libvirt
script_libvirt_install() {
	INSTALL_USER="$1"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm qemu-full qemu-arch-extra qemu-block-gluster qemu-block-iscsi qemu-block-rbd libvirt virt-install virt-manager ebtables dnsmasq bridge-utils openbsd-netcat edk2-ovmf
	systemctl enable libvirtd.service
	systemctl enable virtlogd.service
	systemctl enable libvirt-guests.service
	usermod -a -G kvm $INSTALL_USER
	usermod -a -G libvirt $INSTALL_USER
	EOF

	rm /mnt/etc/conf.d/libvirt-guests

	cat > /mnt/etc/conf.d/libvirt-guests <<- EOF
	BYPASS_CACHE=0
	CONNECT_RETRIES=10
	ON_BOOT=start
	ON_SHUTDOWN=shutdown
	PARALLEL_SHUTDOWN=0
	RETRIES_SLEEP=1
	SHUTDOWN_TIMEOUT=300
	START_DELAY=0
	URIS=default
	EOF

	{
	echo '/* Allow users in kvm group to manage the libvirt daemon without authentication */'
	echo 'polkit.addRule(function(action, subject) {'
	echo '	if (action.id == "org.libvirt.unix.manage" &&'
	echo '		subject.isInGroup("kvm")) {'
	echo '			return polkit.Result.YES;'
	echo '	}'
	echo '});'
	} > /mnt/etc/polkit-1/rules.d/50-libvirt.rules

	{
	echo ''
	echo 'nvram = ['
	echo '	"/usr/share/edk2-ovmf/x64/OVMF_CODE.fd:/usr/share/edk2-ovmf/x64/OVMF_VARS.fd"'
	echo '	"/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd:/usr/share/edk2-ovmf/x64/OVMF_VARS.fd"'
	echo ']'
	echo ''
	echo 'spice_tls = 0'
	echo ''
	} >> /mnt/etc/libvirt/qemu.conf
}

#-------------------------------------------------------------------------

#Install Qemu Guest Agent
script_vmguestadditions_qemu() {
	INSTALL_KDE="$1"
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm qemu-guest-agent
	systemctl enable qemu-guest-agent.service
	EOF
	if [[ "$INSTALL_KDE" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm xf86-video-qxl spice-vdagent
		systemctl enable spice-vdagentd.service
		EOF
	fi
}

#-------------------------------------------------------------------------

#Install VirtualBox Guest Additions
script_vmguestadditions_vbox() {
	INSTALL_KDE="$1"
	if [[ "$INSTALL_KDE" == "YES" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm virtualbox-guest-dkms virtualbox-guest-utils
		EOF
	elif [[ "$INSTALL_KDE" = "NO" ]]; then
		arch-chroot /mnt /bin/bash <<- EOF
		pacman -Sy --noconfirm virtualbox-guest-dkms virtualbox-guest-utils-nox
		EOF
	fi
	arch-chroot /mnt /bin/bash <<- EOF
	systemctl enable vboxservice.service
	EOF
}

#-------------------------------------------------------------------------

#Install librewolf browser
script_librewolf_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm librewolf-bin librewolf-extension-darkreader-bin librewolf-extension-istilldontcareaboutcookies-bin librewolf-extension-localcdn-bin librewolf-extension-reverse-image-search librewolf-extension-sponsorblock-bin librewolf-extension-ublock-origin-bin
	EOF
}

#-------------------------------------------------------------------------

#Install brave browser
script_brave_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm brave-bin
	EOF
}

#-------------------------------------------------------------------------

#Install teamviewer
script_teamviewer_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm teamviewer
	systemctl enable teamviewerd
	EOF
}

#-------------------------------------------------------------------------

#Install vesktop
script_vesktop_install() {
	arch-chroot /mnt /bin/bash <<- EOF
	pacman -Sy --noconfirm vesktop
	EOF
}

#-------------------------------------------------------------------------

#Install systemd-networkd
script_systemd_networkd_install() {
	INSTALL_SYSTEMD_NETWORKD_TYPE="1$"
	INSTALL_SYSTEMD_NETWORKD_MODE="2$"
	INSTALL_IP_ADDRESS="3$"
	INSTALL_GATEWAY="4$"
	INSTALL_PRIMARY_DNS="5$"
	INSTALL_SECONDARY_DNS="6$"
	INSTALL_MACADDRESS="7$"
	mkdir -p /mnt/etc/systemd/network
	if [[ "$INSTALL_SYSTEMD_NETWORKD_TYPE" == "STATIC" ]]; then
		if [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "SINGLE" ]]; then
			cat > /mnt/etc/systemd/network/network-eth0.network <<- EOF
			[Match]
			MACAddress=$INSTALL_MACADDRESS

			[Link]
			Description=LAN Controller
			Name=network-eth0

			[Network]
			Address=$INSTALL_IP_ADDRESS/24
			Gateway=$INSTALL_GATEWAY
			DNS=$INSTALL_PRIMARY_DNS
			DNS=$INSTALL_SECONDARY_DNS
			EOF
		elif [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "BRIDGE" ]]; then
			cat > /mnt/etc/systemd/network/nic0-bridge.netdev <<- EOF
			[NetDev]
			Name=nic0-bridge
			Kind=bridge
			EOF

			cat > /mnt/etc/systemd/network/nic0-bridge.network <<- EOF
			[Match]
			Name=nic0-bridge

			[Network]
			Address=$INSTALL_IP_ADDRESS/24
			Gateway=$INSTALL_GATEWAY
			DNS=$INSTALL_PRIMARY_DNS
			DNS=$INSTALL_SECONDARY_DNS
			EOF

			cat > /mnt/etc/systemd/network/nic0.network <<- EOF
			[Match]
			MACAddress=$INSTALL_MACADDRESS

			[Network]
			Bridge=nic0-bridge
			EOF
		elif [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "BRIDGE_BOND" ]]; then
			cat > /mnt/etc/systemd/network/bridge-bond0.netdev <<- EOF
			[NetDev]
			Name=bridge-bond0
			Kind=bridge
			EOF

			cat > /mnt/etc/systemd/network/bridge-bond0.network <<- EOF
			[Match]
			Name=nic-bond0

			[Network]
			Bridge=bridge-bond0
			EOF

			cat > /mnt/etc/systemd/network/net-bond0.network <<- EOF
			[Match]
			Name=bridge-bond0

			[Network]
			Address=$INSTALL_IP_ADDRESS/24
			Gateway=$INSTALL_GATEWAY
			DNS=$INSTALL_PRIMARY_DNS
			DNS=$INSTALL_SECONDARY_DNS
			EOF

			cat > /mnt/etc/systemd/network/nic-bond0.netdev <<- EOF
			[NetDev]
			Name=nic-bond0
			Description=LAG/Bond to a switch
			Kind=bond

			[Bond]
			Mode=802.3ad
			EOF

			cat > /mnt/etc/systemd/network/nic-bond0.network <<- EOF
			[Match]
			EOF

			NIC_NUMBER=0
			for MACADDR in $(ip link | awk '/ether/ {print $2}'); do
				cat >> /mnt/etc/systemd/network/nic-bond0.network <<- EOF
				Mame=nic${NIC_NUMBER}
				EOF

				cat >> /mnt/etc/udev/rules.d/10-network.rules <<- EOF
				SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${MACADDR}", NAME="nic${NIC_NUMBER}"
				EOF
				((NIC_NUMBER += 1))
			done

			cat >> /mnt/etc/systemd/network/nic-bond0.network <<- EOF

			[Network]
			Bond=nic-bond0
			EOF
		fi
		rm -rf /mnt/etc/resolv.conf

		arch-chroot /mnt /bin/bash <<- EOF
		ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
		EOF

		cat > /mnt/run/systemd/resolve/resolv.conf <<- EOF
		nameserver $INSTALL_PRIMARY_DNS
		nameserver $INSTALL_SECONDARY_DNS
		EOF
	elif [[ "$INSTALL_SYSTEMD_NETWORKD_TYPE" == "DHCP" ]]; then
		if [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "SINGLE" ]]; then
			cat > /mnt/etc/systemd/network/network-eth0.network <<- EOF
			[Match]
			MACAddress=$INSTALL_MACADDRESS

			[Link]
			Description=LAN Controller
			Name=network-eth0

			[Network]
			DHCP=ipv4
			EOF
		elif [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "BRIDGE" ]]; then
			cat > /mnt/etc/systemd/network/nic0-bridge.netdev <<- EOF
			[NetDev]
			Name=nic0-bridge
			Kind=bridge
			EOF

			cat > /mnt/etc/systemd/network/nic0-bridge.network <<- EOF
			[Match]
			Name=nic0-bridge

			[Network]
			DHCP=ipv4
			EOF

			cat > /mnt/etc/systemd/network/nic0.network <<- EOF
			[Match]
			MACAddress=$INSTALL_MACADDRESS

			[Network]
			Bridge=nic0-bridge
			EOF
		elif [[ "$INSTALL_SYSTEMD_NETWORKD_MODE" == "BRIDGE_BOND" ]]; then
			cat > /mnt/etc/systemd/network/bridge-bond0.netdev <<- EOF
			[NetDev]
			Name=bridge-bond0
			Kind=bridge
			EOF

			cat > /mnt/etc/systemd/network/bridge-bond0.network <<- EOF
			[Match]
			Name=nic-bond0

			[Network]
			Bridge=bridge-bond0
			EOF

			cat > /mnt/etc/systemd/network/net-bond0.network <<- EOF
			[Match]
			Name=bridge-bond0

			[Network]
			DHCP=ipv4
			EOF

			cat > /mnt/etc/systemd/network/nic-bond0.netdev <<- EOF
			[NetDev]
			Name=nic-bond0
			Description=LAG/Bond to a switch
			Kind=bond

			[Bond]
			Mode=802.3ad
			EOF

			cat > /mnt/etc/systemd/network/nic-bond0.network <<- EOF
			[Match]
			EOF

			NIC_NUMBER=0
			for MACADDR in $(ip link | awk '/ether/ {print $2}'); do
				cat >> /mnt/etc/systemd/network/nic-bond0.network <<- EOF
				Mame=nic${NIC_NUMBER}
				EOF

				cat >> /mnt/etc/udev/rules.d/10-network.rules <<- EOF
				SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${MACADDR}", NAME="nic${NIC_NUMBER}"
				EOF
				((NIC_NUMBER += 1))
			done

			cat >> /mnt/etc/systemd/network/nic-bond0.network <<- EOF

			[Network]
			Bond=nic-bond0
			EOF
		fi
	fi

	arch-chroot /mnt /bin/bash <<- EOF
	systemctl enable systemd-networkd.service
	systemctl enable systemd-networkd-wait-online.service
	systemctl enable systemd-resolved.service
	EOF
}

#-------------------------------------------------------------------------

echo ""
echo "--------------------------------"
echo "|Arch Linux installation script|"
echo "--------------------------------"
echo ""
echo "--------------------------------"
echo "|      Written by 7thCore      |"
echo "--------------------------------"
echo ""
echo "Welcome to the Arch Linux installation script. Before we"
echo "begin you will have to enter some basic information."
echo ""
echo "Remember if you are installing to an SSD DO NOT have a"
echo "swap partition on it. It will degrade the SSD faster."
echo "Also do not install a btrfs file system on an hdd. A btrfs"
echo "file system is built in a way that copying and writing files"
echo "causes fragmentation. But it is faster on an SSD than ext4."
echo ""

read -n 1 -s -r -p "Press any key to continue"
echo ""

clear

read -r -p "Use 7thCore's repository for installation? (y/n): "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_SEVENTHCORE_LOCAL_REPO="YES"
else
	CONFIG_SEVENTHCORE_LOCAL_REPO="NO"
fi

read -r -p "Enable encryption using luks? (y/n): "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_ENCRYPTION="YES"
else
	CONFIG_ENCRYPTION="NO"
fi

read -r -p "Enable secure boot? (y/n): "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_SECURE_BOOT="YES"
else
	CONFIG_SECURE_BOOT="NO"
fi

read -r -p "Btrfs or Ext4: (type btrfs/ext4) "
if [[ $REPLY =~ ^([bB][tT][rR][fF][sS])$ ]]; then
	CONFIG_FILESYSTEM="btrfs"
elif [[ $REPLY =~ ^([eE][xX][tT][44])$ ]]; then
	CONFIG_FILESYSTEM="ext4"
fi

if [[ "$CONFIG_ENCRYPTION" == "YES" ]] && [[ "$CONFIG_FILESYSTEM" == "btrfs" ]]; then
	if [ -d "/sys/firmware/efi/efivars" ]; then
		echo "UEFI detected"
		echo ""
		echo "If you are installing to an SSD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Boot partition"
		echo "sdX2: Luks with Btrfs (root,home)"
		echo ""
	else
		echo "BIOS detected"
		echo ""
		echo "If you are installing to an SSD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Grub offset partition (1M)"
		echo "sdX2: Luks with Btrfs (boot,root,home)"
		echo ""
	fi
elif [[ "$CONFIG_ENCRYPTION" == "YES" ]] && [[ "$CONFIG_FILESYSTEM" == "ext4" ]]; then
	if [ -d "/sys/firmware/efi/efivars" ]; then
		echo "UEFI detected"
		echo ""
		echo "If you are installing to an HDD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Boot partition"
		echo "sdX2: Luks with Ext4 partitions (swap,root,home)"
		echo ""
	else
		echo "BIOS detected"
		echo ""
		echo "If you are installing to an HDD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Grub offset partition (1M)"
		echo "sdX2: Luks with Ext4 partitions (boot,swap,root,home)"
		echo ""
	fi
elif [[ "$CONFIG_ENCRYPTION" == "NO" ]] && [[ "$CONFIG_FILESYSTEM" == "btrfs" ]]; then
	if [ -d "/sys/firmware/efi/efivars" ]; then
		echo "UEFI detected"
		echo ""
		echo "If you are installing to an SSD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Boot partition"
		echo "sdX2: Btrfs (root,home)"
		echo ""
	else
		echo "BIOS detected"
		echo ""
		echo "If you are installing to an SSD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Grub offset partition (1M)"
		echo "sdX2: Btrfs (boot,root,home)"
		echo ""
	fi
elif [[ "$CONFIG_ENCRYPTION" == "NO" ]] && [[ "$CONFIG_FILESYSTEM" == "ext4" ]]; then
	if [ -d "/sys/firmware/efi/efivars" ]; then
		echo "UEFI detected"
		echo ""
		echo "If you are installing to an HDD, I recommend you"
		echo "partition it in the following order and use Btrfs:"
		echo ""
		echo "sdX1: Boot partition"
		echo "sdX2: Ext4 partitions (swap,root,home)"
		echo ""
	else
		echo "BIOS detected"
		echo ""
		echo "If you are installing to an HDD, I recommend you"
		echo "partition it in the following order and use Ext4"
		echo ""
		echo "sdX1: Grub offset partition (1M)"
		echo "sdX2: Ext4 (swap)"
		echo "sdX3: Ext4 (root)"
		echo "sdX3: Ext4 (home)"
		echo ""
	fi
fi

read -n 1 -s -r -p "Press any key to continue"
echo ""
echo "Enter the primary drive that will contain the boot,"
read -r -p "root and home partition [ex. sdX,sdY,sdZ...]: " CONFIG_PRIMARY_DRIVE
echo ""
read -r -p "Wipe partition signatures from primary drive? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	wipefs -a "/dev/$CONFIG_PRIMARY_DRIVE"
fi
echo ""
echo "Prepare to partition the drive with cfdisk. Recomended partition scheme is gpt."
echo ""
read -n 1 -s -r -p "Press any key to continue"
echo ""
cfdisk "/dev/$CONFIG_PRIMARY_DRIVE"
echo ""

if [[ $CONFIG_ENCRYPTION == "YES" ]]; then
	read -r -p "Enter the boot partition of the primary drive (efi or offset) [ex. sdX1,sdY1,sdZ1...]: " CONFIG_BOOT_PART
	read -r -p "Enter the Luks (root,home) partition of the primary drive [ex. sdX2,sdY2,sdZ2...]: " CONFIG_ROOT_PART
	echo ""
	read -r -p "Do you want to have a swap partition on this drive? [y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		echo ""
		read -r -p "Enter the swap size in gigabytes [ex. 8]: " CONFIG_SWAP_SIZE
	else
		CONFIG_SWAP_SIZE="disabled"
	fi
	echo ""
	read -r -s -p "Enter passphrase for Luks [root,home]: " CONFIG_LUKS_KEY
	echo ""

	script_luks_create "$CONFIG_PRIMARY_DRIVE" "$CONFIG_ROOT_PART" "$CONFIG_LUKS_KEY" "crypt_luks_root0"

	if [[ "$CONFIG_FILESYSTEM" == "btrfs" ]]; then
		script_btrfs "$CONFIG_PRIMARY_DRIVE" "$CONFIG_BOOT_PART" "mapper/crypt_luks_root0" "$CONFIG_SWAP_SIZE"
	elif [[ "$CONFIG_FILESYSTEM" == "ext4" ]]; then
		script_luks_lvm "mapper/crypt_luks_root0" "$CONFIG_SWAP_SIZE"
		script_ext4 "$CONFIG_BOOT_PART" "cryptvg_luks0/root-arch" "cryptvg_luks0/home-arch" "cryptvg_luks0/swap-arch" "$CONFIG_SWAP_SIZE"
	fi
elif [[ $CONFIG_ENCRYPTION == "NO" ]]; then
	if [[ "$CONFIG_FILESYSTEM" == "btrfs" ]]; then
		read -r -p "Enter the boot partition of the primary drive [ex. sdX1,sdY1,sdZ1...]: " CONFIG_BOOT_PART
		read -r -p "Enter the btrfs (root,home) partition of the primary drive [ex. sdX2,sdY2,sdZ2...]: " CONFIG_ROOT_PART
		echo ""
		read -r -p "Do you want to have a swap partition on this drive? [y/n]: "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			echo ""
			read -r -p "Enter the swap size in gigabytes [ex. 8]: " CONFIG_SWAP_SIZE
		else
			CONFIG_SWAP_SIZE="disabled"
		fi
		script_btrfs "$CONFIG_BOOT_PART" "$CONFIG_ROOT_PART" "$CONFIG_SWAP_SIZE"
	elif [[ "$CONFIG_FILESYSTEM" == "ext4" ]]; then
		read -r -p "Enter the boot partition of the primary drive [ex. sdX1,sdY1,sdZ1...]: " CONFIG_BOOT_PART
		read -r -p "Enter the ext4 (root) partition of the primary drive [ex. sdX2,sdY2,sdZ2...]: " CONFIG_ROOT_PART
		read -r -p "Enter the ext4 (home) partition of the primary drive [ex. sdX3,sdY3,sdZ3...]: " CONFIG_HOME_PART
		echo ""
		read -r -p "Do you want to have a swap partition on this drive? [y/n]: "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			echo ""
			read -r -p "Enter the swap partition of the primary drive [ex. sdX4,sdY4,sdZ4...]: " CONFIG_SWAP_PART
		else
			CONFIG_SWAP_PART="disabled"
		fi
		script_ext4 "$CONFIG_BOOT_PART" "$CONFIG_ROOT_PART" "$CONFIG_HOME_PART" "$CONFIG_SWAP_PART"
	fi
fi

echo ""
read -r -p "Install 7thCore's repositories? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	read -r -p "Install standard AUR repository (aur-prebuild)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD=YES
	else
		CONFIG_AUR_PREBUILD=NO
	fi

	read -r -p "Install 7thCore's repository for his personal packages (aur-prebuild-core)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_CORE=YES
		read -r -p "Install the 7thcore-essentials package? [Y/n]: "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_AUR_PREBUILD_CORE_PACKAGES=YES
		else
			CONFIG_AUR_PREBUILD_CORE_PACKAGES=NO
		fi
	else
		CONFIG_AUR_PREBUILD_CORE=NO
		CONFIG_AUR_PREBUILD_CORE_PACKAGES=NO
	fi

	read -r -p "Install dotnet AUR repository (aur-prebuild-dotnet)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_DOTNET=YES
	else
		CONFIG_AUR_PREBUILD_DOTNET=NO
	fi

	read -r -p "Install ffmpeg-full AUR repository (aur-prebuild-ffmpeg)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_FFMPEG=YES
	else
		CONFIG_AUR_PREBUILD_FFMPEG=NO
	fi

	read -r -p "Install nvidia driver AUR repository for version 390 (aur-prebuilt-nvidia390)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_NVIDIA390=YES
	else
		CONFIG_AUR_PREBUILD_NVIDIA390=NO
	fi

	read -r -p "Install nvidia driver AUR repository for version 470 (aur-prebuilt-nvidia470)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_NVIDIA470=YES
	else
		CONFIG_AUR_PREBUILD_NVIDIA470=NO
	fi

	read -r -p "Install nvidia driver AUR repository for version 550 (aur-prebuilt-nvidia550)? [Y/n]: "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_AUR_PREBUILD_NVIDIA550=YES
	else
		CONFIG_AUR_PREBUILD_NVIDIA550=NO
	fi
else
	CONFIG_AUR_PREBUILD=NO
	CONFIG_AUR_PREBUILD_CORE=NO
	CONFIG_AUR_PREBUILD_CORE_PACKAGES=NO
	CONFIG_AUR_PREBUILD_FFMPEG=NO
	CONFIG_AUR_PREBUILD_NVIDIA390=NO
	CONFIG_AUR_PREBUILD_NVIDIA470=NO
	CONFIG_AUR_PREBUILD_NVIDIA550=NO
fi

echo ""
read -r -p "Language? [en_sl/en/sl] "
if [[ $REPLY =~ ^([eE][nN][__][sS][lL])$ ]]; then
	CONFIG_LANGUAGE=EN_SL
elif [[ $REPLY =~ ^([eE][nN])$ ]]; then
	CONFIG_LANGUAGE=EN
elif [[ $REPLY =~ ^([sS][lL])$ ]]; then
	CONFIG_LANGUAGE=SL
fi

echo ""
read -r -p "Enter user: " CONFIG_USER
read -r -s -p "Enter password for $CONFIG_USER: " CONFIG_USER_PASS
echo ""
read -r -s -p "Enter password for root: " CONFIG_ROOT_PASS
echo ""
read -r -p "Enter hostname: " CONFIG_HOSTNAME

echo ""
read -r -p "Zsh as default shell? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_SHELL=ZSH_SHELL
else
	CONFIG_SHELL=BASH_SHELL
fi

echo ""
read -r -p "Enable SSH? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_SSH=YES
	echo ""
	read -r -p "Enter ssh port: " CONFIG_SSH_PORT
else
	CONFIG_SSH=NO
	CONFIG_SSH_PORT=NONE
fi

echo ""
read -r -p "Install NTP (network time protocol)? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_NTP=YES
else
	CONFIG_NTP=NO
fi

echo ""
read -r -p "Install UFW firewall? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_UFW=YES
else
	CONFIG_UFW=NO
fi

echo ""
read -r -p "Install samba file sharing? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_SMB=YES
	echo ""
	read -r -s -p "Enter samba password for $CONFIG_USER: " CONFIG_SMBPSW
	read -r -p "Enter samba workgroup: " CONFIG_SMBWG
else
	CONFIG_SMB=NO
	CONFIG_SMBPSW=NONE
	CONFIG_SMBWG=NONE
fi

echo ""
read -r -p "Install postfix for automatic email sending on user configured events? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_POSTFIX=YES
	echo ""
	read -r -p "Enter postfix user (ex: example.gmail.com): " CONFIG_POSTFIXUSER
	read -r -p "Enter postfix password (application password): " CONFIG_POSTFIXPSW
else
	CONFIG_POSTFIX=NO
	CONFIG_POSTFIXUSER=NONE
	CONFIG_POSTFIXPSW=NONE
fi

echo ""
read -r -p "Enable printer support? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_PRINTERSUPP=YES
else
	CONFIG_PRINTERSUPP=NO
fi

echo ""
read -r -p "Enable KDE? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	echo ""
	read -r -p "Install the full KDE stack? (selecting no will install the basics) [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_KDE_FULL=YES
	else
		CONFIG_KDE_FULL=NO
	fi
	echo ""
	read -r -p "Enable KDE's network connection editor and system tray applet? [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_NMCONNECTIONEDITOR=YES
	else
		CONFIG_NMCONNECTIONEDITOR=NO
	fi
	echo ""
	read -r -p "Enable SDDM automatic login to KDE session for user $CONFIG_USER? [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		read -r -p "Automatic lock after automatic login for user $CONFIG_USER? (Usefull for servers and remote desktop access) [Y/n] "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_KDE_AUTOLOGIN=YES-LOCK
		else
			CONFIG_KDE_AUTOLOGIN=YES
		fi
	else
		CONFIG_KDE_AUTOLOGIN=NO
	fi
	echo ""
	read -r -p "Enable KDE games? [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_KDE_GAMES=YES
	else
		CONFIG_KDE_GAMES=NO
	fi
	if [[ "$CONFIG_AUR_PREBUILD" == "YES" ]]; then
		echo ""
		read -r -p "Install LibreWolf browser? (will install only if KDE is installed) [Y/n]: "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_AUR_PREBUILD_LIBREWOLF=YES
		else
			CONFIG_AUR_PREBUILD_LIBREWOLF=NO
		fi

		read -r -p "Install Brave browser? (will install only if KDE is installed) [Y/n]: "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_AUR_PREBUILD_BRAVE=YES
		else
			CONFIG_AUR_PREBUILD_BRAVE=NO
		fi

		read -r -p "Install TeamViewer? (will install only if KDE is installed) [Y/n] "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_AUR_PREBUILD_TEAMVIEWER=YES
		else
			CONFIG_AUR_PREBUILD_TEAMVIEWER=NO
		fi

		read -r -p "Install Vesktop? (Discord client, will install only if KDE is installed) [Y/n] "
		if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
			CONFIG_AUR_PREBUILD_VESKTOP=YES
		else
			CONFIG_AUR_PREBUILD_VESKTOP=NO
		fi
	else
		CONFIG_AUR_PREBUILD_LIBREWOLF=NO
		CONFIG_AUR_PREBUILD_BRAVE=NO
		CONFIG_AUR_PREBUILD_TEAMVIEWER=NO
		CONFIG_AUR_PREBUILD_VESKTOP=NO
	fi
	CONFIG_KDE=YES
else
	CONFIG_KDE=NO
	CONFIG_KDE_GAMES=NO
	CONFIG_NMCONNECTIONEDITOR=NO
	CONFIG_AUR_PREBUILD_LIBREWOLF=NO
	CONFIG_AUR_PREBUILD_BRAVE=NO
	CONFIG_AUR_PREBUILD_TEAMVIEWER=NO
	CONFIG_AUR_PREBUILD_VESKTOP=NO
fi

if [[ "$CONFIG_NMCONNECTIONEDITOR" == "NO" ]]; then
	echo ""
	echo "Enable systemd-networkd? [Y/n]"
	read -r -p " (Not recommended if you installed KDE and it's network applet) "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		read -r -p "Select network mode [single/bridge/bridge_bond] "
		if [[ $REPLY =~ ^([sS][iI][nN][gG][lL][eE])$ ]]; then
			CONFIG_SYSTEMD_NETWORKD_MODE=SINGLE
		elif [[ $REPLY =~ ^([bB][rR][iI][dD][gG][eE])$ ]]; then
			CONFIG_SYSTEMD_NETWORKD_MODE=BRIDGE
		elif [[ $REPLY =~ ^([bB][rR][iI][dD][gG][eE][_][bB][oO][nN][dD])$ ]]; then
			CONFIG_SYSTEMD_NETWORKD_MODE=BRIDGE_BOND
		fi
		read -r -p "Create static configuration or use DHCP? [static/dhcp] "
			if [[ $REPLY =~ ^([sS][tT][aA][tT][iI][cC])$ ]]; then
				CONFIG_SYSTEMD_NETWORKD=YES
				CONFIG_SYSTEMD_NETWORKD_TYPE=STATIC
				echo ""
				read -r -p "Enter local ip address: " CONFIG_IP_ADDRESS
				read -r -p "Enter gateway/router: " CONFIG_GATEWAY
				read -r -p "Enter primary dns: " CONFIG_PRIMARY_DNS
				read -r -p "Enter secondary dns: " CONFIG_SECONDARY_DNS
				echo ""
				echo "Available mac addresses for ethernet cards:"
				ip link | awk '/ether/ {print $2}'
				echo ""
				read -r -p "Enter mac address of ethernet card: " CONFIG_MACADDRESS
			elif [[ $REPLY =~ ^([dD][hH][cC][pP])$ ]]; then
				CONFIG_SYSTEMD_NETWORKD=YES
				CONFIG_SYSTEMD_NETWORKD_TYPE=DHCP
				CONFIG_IP_ADDRESS=DHCP
				CONFIG_GATEWAY=DHCP
				CONFIG_PRIMARY_DNS=DHCP
				CONFIG_SECONDARY_DNS=DHCP
				echo ""
				echo "Available mac addresses for ethernet cards:"
				ip link | awk '/ether/ {print $2}'
				echo ""
				read -r -p "Enter mac address of ethernet card: " CONFIG_MACADDRESS
			fi
	else
		CONFIG_SYSTEMD_NETWORKD=NO
		CONFIG_IP_ADDRESS=NONE
		CONFIG_GATEWAY=NONE
		CONFIG_PRIMARY_DNS=NONE
		CONFIG_SECONDARY_DNS=NONE
		CONFIG_MACADDRESS=DEFAULT
	fi
fi

echo ""
read -r -p "Install AMD drivers? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_AMD=YES
else
	CONFIG_AMD=NO
fi

echo ""
read -r -p "Install Intel drivers? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_INTEL=YES
else
	CONFIG_INTEL=NO
fi

echo ""
read -r -p "Install Nvidia drivers? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_NVIDIA=YES
else
	CONFIG_NVIDIA=NO
fi

echo ""
read -r -p "Install steam? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_LIB32=YES
	CONFIG_STEAM=YES
else
	CONFIG_LIB32=NO
	CONFIG_STEAM=NO
fi

echo ""
read -r -p "Install wine? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_WINE=YES
else
	CONFIG_WINE=NO
fi

echo ""
read -r -p "Install lutris? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_LUTRIS=YES
	CONFIG_WINE=YES
else
	CONFIG_LUTRIS=NO
fi

echo ""
read -r -p "Install nextcloud? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_NEXTCLOUD=YES
else
	CONFIG_NEXTCLOUD=NO
fi

echo ""
read -r -p "Install LibVirt and Qemu? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_LIBVIRT=YES
else
	CONFIG_LIBVIRT=NO
fi

echo ""
read -r -p "Is the os being installed to a VM? [Y/n] "
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
	CONFIG_VIRTUAL_MACHINE=YES
	echo ""
	read -r -p "Do you want to install Qemu Guest Agent (ONLY FOR LIBVIRT AND QEMU)? [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_QEMUGA=YES
	else
		CONFIG_QEMUGA=NO
	fi
	echo ""
	read -r -p "Do you want to install VirtualBox Guest Additions (ONLY FOR VIRTUALBOX)? [Y/n] "
	if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
		CONFIG_VBOXGUEST=YES
	else
		CONFIG_VBOXGUEST=NO
	fi
else
	CONFIG_VIRTUAL_MACHINE=NO
	CONFIG_VBOXGUEST=NO
	CONFIG_QEMUGA=NO
fi

if [ -d "/sys/firmware/efi/efivars" ]; then
	CONFIG_UEFIBIOS=UEFI
else
	CONFIG_UEFIBIOS=BIOS
fi

clear

echo ""
echo "--------------------------------"
echo "|Arch Linux installation script|"
echo "--------------------------------"
echo ""
echo "--------------------------------"
echo "|   Check your configuration   |"
echo "--------------------------------"
echo ""
echo "Check configuration:"
echo ""
echo "Username: $CONFIG_USER"
echo "Password: $CONFIG_USER_PASS"
echo "Root password: $CONFIG_ROOT_PASS"
echo "Hostname: $CONFIG_HOSTNAME"
echo ""
echo "UEFI or BIOS: $CONFIG_UEFIBIOS"
echo ""
echo "Primary drive: $CONFIG_PRIMARY_DRIVE"
echo ""
echo "Encrption: $CONFIG_ENCRYPTION"
echo ""
echo "Filesystem (Btrfs/Ext4): $CONFIG_FILESYSTEM"
echo ""
echo "Installing to a VM: $CONFIG_VIRTUAL_MACHINE"
echo "Install VirtualBox Guest Additions: $CONFIG_VBOXGUEST"
echo "Install Qemu Guest Agent: $CONFIG_QEMUGA"
echo ""
echo "Shell: $CONFIG_SHELL"
echo ""
echo "Install KDE: $CONFIG_KDE"
echo "Install KDE games: $CONFIG_KDE_GAMES"
echo "Install KDE Connection editor and systm tray applet: $CONFIG_NMCONNECTIONEDITOR"
echo ""

read -n 1 -s -r -p "Press any key to continue"
echo ""

clear

echo ""
echo "--------------------------------"
echo "|Arch Linux installation script|"
echo "--------------------------------"
echo ""
echo "--------------------------------"
echo "|   Check your configuration   |"
echo "--------------------------------"
echo ""
echo "Check configuration:"
echo ""
echo "Create network configuration for systemd-networkd: $CONFIG_SYSTEMD_NETWORKD"
echo ""
echo "Systemd-networkd type: $CONFIG_SYSTEMD_NETWORKD_TYPE"
echo "Systemd-networkd mode: $CONFIG_SYSTEMD_NETWORKD_MODE"
echo "Local ip address: $CONFIG_IP_ADDRESS"
echo "Gateway/router: $CONFIG_GATEWAY"
echo "Primary dns: $CONFIG_PRIMARY_DNS"
echo "Secondary dns: $CONFIG_SECONDARY_DNS"
echo "MAC Address: $CONFIG_MACADDRESS"
echo ""
echo "Enable SSH: $CONFIG_SSH"
echo "SSH port: $CONFIG_SSH_PORT"
echo ""
echo "Enable NTP: $CONFIG_NTP"
echo ""
echo "Enable UFW Firewall: $CONFIG_UFW"
echo ""
echo "Install standard AUR repository (aur-prebuild): $CONFIG_AUR_PREBUILD"
echo "Install 7thCore's repository for his personal packages (aur-prebuild-core): $CONFIG_AUR_PREBUILD_CORE"
echo "Install dotnet AUR repository (aur-prebuild-dotnet): $CONFIG_AUR_PREBUILD_DOTNET"
echo "Install ffmpeg-full AUR repository (aur-prebuild-ffmpeg): $CONFIG_AUR_PREBUILD_FFMPEG"
echo "Install nvidia driver AUR repository for version 390 (aur-prebuilt-nvidia390): $CONFIG_AUR_PREBUILD_NVIDIA390"
echo "Install nvidia driver AUR repository for version 470 (aur-prebuilt-nvidia470): $CONFIG_AUR_PREBUILD_NVIDIA470"
echo "Install nvidia driver AUR repository for version 550 (aur-prebuilt-nvidia550): $CONFIG_AUR_PREBUILD_NVIDIA550"
echo ""
if [[ "$CONFIG_AUR_PREBUILD" == "YES" ]]; then
	echo "Install LibreWolf browser: $CONFIG_AUR_PREBUILD_LIBREWOLF"
	echo "Install Brave browser: $CONFIG_AUR_PREBUILD_BRAVE"
	echo "Install TeamViewer: $CONFIG_AUR_PREBUILD_TEAMVIEWER"
	echo "Install Vesktop: $CONFIG_AUR_PREBUILD_VESKTOP"
fi
echo ""
echo "Install Samba: $CONFIG_SMB"
echo "Samba password for $CONFIG_USER: $CONFIG_SMBPSW"
echo "Samba workgroup: $CONFIG_SMBWG"
echo ""
echo ""

read -n 1 -s -r -p "Press any key to continue"
echo ""

clear

echo ""
echo "--------------------------------"
echo "|Arch Linux installation script|"
echo "--------------------------------"
echo ""
echo "--------------------------------"
echo "|   Check your configuration   |"
echo "--------------------------------"
echo ""
echo "Install Postfix : $CONFIG_POSTFIX"
echo "Install Postfix user : $CONFIG_POSTFIXUSER"
echo "Install Postfix pass : $CONFIG_CONFIG_POSTFIXPSW"
echo ""
echo "Install printer support: $CONFIG_PRINTERSUPP"
echo ""
echo "Install AMD drivers: $CONFIG_AMD"
echo ""
echo "Install Intel drivers: $CONFIG_INTEL"
echo ""
echo "Install Nvidia drivers: $CONFIG_NVIDIA"
echo ""
echo "Install Steam: $CONFIG_STEAM"
echo ""
echo "Install Wine, Lutris and DXVK: $CONFIG_WINE"
echo ""
echo "Install Nextcloud: $CONFIG_NEXTCLOUD"
echo ""
echo "Install LibVirt and Qemu: $CONFIG_LIBVIRT"
echo ""
echo "Use 7thCore's repository for installation: $CORE_LOCAL_REPO"
echo ""

read -r -p "Configuration correct? [Y/n] "
if [[ $REPLY =~ ^([nN][oO]|[nN])$ ]]; then
	echo "Script terminated due to wrongfull configuration by user."
	echo "To restart the script manualy run it and re-enter the configuration."
	exit 1
fi

clear

echo "--------------------------------"
echo "|    Configuation  complete    |"
echo "--------------------------------"
echo ""
echo "Go for a beer or something. The script will handle this."
echo ""
echo "I hope."
echo ""

sleep 5

#-------------------------------------------------------------------------

script_prep_mirrors "$CONFIG_SEVENTHCORE_LOCAL_REPO"
script_base_install

if [[ "$CONFIG_ENCRYPTION" == "YES" ]]; then
	script_encryption_install "$CONFIG_FILESYSTEM"
else
	script_nonencryption_install
fi

if [ -d "/sys/firmware/efi/efivars" ]; then
	script_systemd-boot "$CONFIG_ENCRYPTION" "$CONFIG_FILESYSTEM" "$CONFIG_ROOT_PART" "$CONFIG_SWAP_SIZE" "$CONFIG_SECURE_BOOT"
else
	script_grub "$CONFIG_ENCRYPTION" "$CONFIG_PRIMARY_DRIVE" "$CONFIG_ROOT_PART" "$CONFIG_LUKS_KEY"
fi

if [[ "$CONFIG_LIB32" == "YES" ]]; then
	script_lib32_install
fi

if [[ "$CONFIG_AUR_PREBUILD" == "YES" ]]; then
	script_aur_install "$CONFIG_AUR_PREBUILD" "$CONFIG_AUR_PREBUILD_CORE" "$CONFIG_AUR_PREBUILD_CORE_PACKAGES" "$CONFIG_AUR_PREBUILD_DOTNET" "$CONFIG_AUR_PREBUILD_FFMPEG" "$CONFIG_AUR_PREBUILD_NVIDIA390" "$CONFIG_AUR_PREBUILD_NVIDIA470" "$CONFIG_AUR_PREBUILD_NVIDIA550"
fi

script_lang_install "$CONFIG_LANGUAGE"
script_user_install "$CONFIG_USER" "$CONFIG_USER_PASS" "$CONFIG_ROOT_PASS" "$CONFIG_HOSTNAME" "$CONFIG_SHELL"

if [[ "$CONFIG_SSH" == "YES" ]]; then
	script_ssh_install "$CONFIG_USER" "$CONFIG_SSH_PORT"
fi

if [[ "$CONFIG_NTP" == "YES" ]]; then
	script_ntp_install
fi

if [[ "$CONFIG_UFW" == "YES" ]]; then
	script_ufw_install "$CONFIG_SSH" "$CONFIG_SSH_PORT" "$CONFIG_SMB" "$CONFIG_STEAM"
fi

if [[ "$CONFIG_SMB" == "YES" ]]; then
	script_smb_install "$CONFIG_USER" "$CONFIG_HOSTNAME" "$CONFIG_SMBPSW" "$CONFIG_SMBWG"
fi

if [[ "$CONFIG_POSTFIX" == "YES" ]]; then
	script_postfix_install "$CONFIG_POSTFIXUSER" "$CONFIG_POSTFIXPSW"
fi

if [[ "$CONFIG_PRINTERSUPP" == "YES" ]]; then
	script_printer_install "$CONFIG_KDE_FULL"
fi

if [[ "$CONFIG_KDE" == "YES" ]]; then
	script_kde_install "$CONFIG_USER" "$CONFIG_KDE_FULL" "$CONFIG_KDE_AUTOLOGIN" "$CONFIG_KDE_GAMES" "$CONFIG_NMCONNECTIONEDITOR" "$CONFIG_LIB32"
fi

if [[ "$CONFIG_SYSTEMD_NETWORKD" == "YES" ]]; then
	script_systemd_networkd_install "$CONFIG_SYSTEMD_NETWORKD_TYPE" "$CONFIG_SYSTEMD_NETWORKD_MODE" "$CONFIG_IP_ADDRESS" "$CONFIG_GATEWAY" "$CONFIG_PRIMARY_DNS" "$CONFIG_SECONDARY_DNS" "$CONFIG_MACADDRESS"
fi

if [[ "$CONFIG_AMD" == "YES" ]]; then
	script_amd_install
fi

if [[ "$CONFIG_INTEL" == "YES" ]]; then
	script_intel_install
fi

if [[ "$CONFIG_NVIDIA" == "YES" ]]; then
	script_nvidia_install
fi

if [[ "$CONFIG_KDE" == "YES" ]]; then
	if [[ "$CONFIG_STEAM" == "YES" ]]; then
		script_steam_install
	fi
	if [[ "$CONFIG_WINE" == "YES" ]]; then
		script_wine_install $CONFIG_LIB32
	fi
	if [[ "$CONFIG_LUTRIS" == "YES" ]]; then
		script_lutris_install
	fi
	if [[ "$CONFIG_NEXTCLOUD" == "YES" ]]; then
		script_nextcloud_install
	fi
fi

if [[ "$CONFIG_LIBVIRT" == "YES" ]]; then
	script_libvirt_install "$CONFIG_USER"
fi

if [[ "$CONFIG_QEMUGA" == "YES" ]]; then
	script_vmguestadditions_qemu "$CONFIG_KDE"
elif [[ "$CONFIG_VBOXGUEST" == "YES" ]]; then
	script_vmguestadditions_vbox "$CONFIG_KDE"
fi

if [[ "$CONFIG_KDE" == "YES" ]] && [[ "$CONFIG_AUR_PREBUILD" == "YES" ]]; then
	if [[ "$CONFIG_AUR_PREBUILD_LIBREWOLF" == "YES" ]]; then
		script_librewolf_install
	fi
	if [[ "$CONFIG_AUR_PREBUILD_BRAVE" == "YES" ]]; then
		script_brave_install
	fi
	if [[ "$CONFIG_AUR_PREBUILD_TEAMVIEWER" == "YES" ]]; then
		script_teamviewer_install
	fi
	if [[ "$CONFIG_AUR_PREBUILD_VESKTOP" == "YES" ]]; then
		script_vesktop_install
	fi
fi

#Cleanup boot partition because we're using UKIs
if [ -f /boot/initramfs-linux.img ]; then
	rm /boot/initramfs-linux.img
fi
if [ -f /boot/initramfs-linux-lts.img ]; then
	rm /boot/initramfs-linux-lts.img
fi
if [ -f /boot/initramfs-linux-zen.img ]; then
	rm /boot/initramfs-linux-zen.img
fi

if [[ "$(cat "/sys/block/$CONFIG_PRIMARY_DRIVE/queue/rotational")" == "0" ]]; then
	arch-chroot /mnt /bin/bash <<- EOF
	systemctl enable fstrim.timer
	EOF
fi

clear

echo ""
echo "---------------------------------"
echo "|     Installation complete     |"
echo "---------------------------------"
echo ""
echo "Congratz! The script completed without frying the CPU."
echo "Well we'll see what happens after a reboot. Brace for"
echo "the fireworks buddy and hold on to your panty hoes!"
echo ""
echo "KDE Wallet: create a new wallet named 'kdewallet' with."
echo "blowfish encryption and an empty password to enable"
echo "automatic unlock of the wallet."

read -r -p "Do you want to reboot? [Y/n] "
if [[ $REPLY =~ ^([nN][oO]|[nN])$ ]]; then
	exit 1
fi

shutdown -r now
