#/usr/bin/env bash
if [ "$(whoami)" != "root" ]; then
	echo "Need to be root or run with sudo. Exiting..."
	exit 1
fi

set -u
tmpdir="/tmp/add-pacman-repository-$(date +%Y%m%d%H%M%S)"
reponame=""
sigcheck=""
sigtrust=""
server=""
selected=""
yesorno=""
keyid=""


function main() {
	while true;do
		echo -e "Add a repository? [1=yes or 2=no]"
		local yesorno
		read -n1 yesorno
		case ${yesorno} in
			1) caution ;;
			2) repo_abort ;;
		esac
	done
}

function caution() {
	while true;do
		yesorno=""
		mkdir ${tmpdir}
		echo ""
		echo "!!!CAUTION!!!"
		echo "While the script is running, you MUST NOT press any arrow keys or stop working in the middle."
		repo_name
	done
}

function repo_name() {
	while true;do
		echo ""
		echo "Enter repository name."
		read reponame
		if [[ ${reponame} == "" ]]; then
			echo "Need to enter the repository name!"
			repo_name
		fi

		echo -e "\n[${reponame}]"|tee -a ${tmpdir}/preview.txt
		siglevel_setting
	done
}

function siglevel_setting() {
	while true;do
		yesorno=""
		echo ""
		echo "Set SigLevel? [1=yes or 2=no]"
		read -n1 yesorno
		case ${yesorno} in
			1) repo_siglevel ;; 
			2) repo_key
		esac
	done
}

function repo_siglevel() {
	while true;do
		yesorno=""
		echo ""
		echo "Select the level of signature checking."
		echo "1) Never    - All signature checking is suppressed."
		echo "2) Optional - Signatures are checked if present."
		echo "3) Required - Signatures are required."
		read -n1 selected
		case ${selected} in
			1) echo "" && echo "SigLevel = Never"|tee -a ${tmpdir}/preview.txt;;
			2) sigcheck=Optional ;;
			3) sigcheck="Required DatabaseOptional" ;;
		esac

		if [[ ${selected} == "1" ]]; then
			repo_server
		fi

		selected=""
		echo ""
		echo "Select the level of signature trusting."
		echo "1) TrustedOnly - Signature must be fully trusted."
		echo "2) TrustAll    - Signature isn't requred to be assigned a trust level."
		read -n1 selected
		case ${selected} in
			1) sigtrust=TrustedOnly ;;
			2) sigtrust=TrustAll    ;;
		esac
		echo ""
		echo "SigLevel = ${sigcheck} ${sigtrust}"|tee -a ${tmpdir}/preview.txt
		repo_key
	done
}

function repo_key() {
	while true;do
		yesorno=""
		selected=""
		echo ""
		echo "Where do you import keyrings from?"
		echo "1) Keyring files    - Extensions such as .key or .gpg"
		echo "2) Key ID           - From online servers"
		echo "3) Keyring packages - Extensions such as .zst or .xz"
		echo "4) Not import any keyrings"
		read -n1 selected
		case ${selected} in
			1) key_file  ;;
			2) key_server ;;
			3) key_pkgs   ;;
			4) key_noimport ;;
		esac
	done
}

function key_file() {
	while true;do
		yesorno=""
		selected=""
		echo ""
		echo "Download a keyring file from the Internet."
		echo "Enter the URL. (example: https://example.com/hogehoge.key)"
		echo "(To return to the previous, press return key.)"
		local keyurl
		read keyurl
		if [[ ${keyurl} = "" ]]; then
			repo_key
		fi
 		mkdir ${tmpdir}/keyring
		wget -O ${tmpdir}/keyring/archive.key ${keyurl}
		pacman-key --add ${tmpdir}/keyring/archive.key
		echo ""
		echo "Sign to the key locally. Enter the Key-ID."
		pacman-key --lsign-key ${keyid}
		additional_keys
	done
}

function key_server() {
	while true;do
		yesorno=""
		echo ""
		echo "Import keyring from the Internet. Enter the Key-ID."
		echo "(To return to the previous, press return key.)"
  read keyid
		if [[ ${keyid} = "" ]]; then
			repo_key
		fi
		echo "Specify the key-server. (example: pgp.mit.edu)"
		local keyserver
		read keyserver
		if [[ ${keyserver} = "" ]]; then
			echo "Need to enter the key server URL!"
			key_server
		fi
		pacman-key --recv-key ${keyid} --keyserver ${keyserver}
		pacman-key --lsign-key ${keyid}
	additional_keys
	done
}

function key_pkgs() {
	while true;do
		yesorno=""
		echo ""
		echo "Download a keyring package from the Internet. Enter the URL."
		echo "(example: https://example.com/hoge/fuga-keyring-x.y.z-r-any.pkg.tar.zst)"
		echo "(To return to the previous, press return key.)"
		local pkgurl
		read pkgurl
		if [[ ${pkgurl} = "" ]]; then
			repo_key
		fi
 		mkdir ${tmpdir}/keyring
		wget -NP ${tmpdir}/keyring ${pkgurl}
		yes|pacman -U ${tmpdir}/keyring/*.zst
	additional_keys
	done
}

function additional_keys() {
	while true;do
		echo "Import additional keys? [1=yes or 2=no]"
		read -n1 yesorno
		case ${yesorno} in
			1) repo_key ;;
			2) repo_server ;;
		esac
	done
}

function key_noimport() {
	while true;do
		echo ""
		echo "!!!CAUTION!!!"
		echo "Some repositories require additional keys."
		echo "Are you sure you don't want to add them? [1=yes or 2=no]"
		read -n1 yesorno
		case ${yesorno} in
			1) repo_server ;;
			2) repo_key ;;
		esac
	done
}

function repo_server() {
	while true;do
		yesorno=""
		selected=""
		echo ""
		echo "Use a mirror server list?"
		echo "1) Yes   - Mirror lists are usually placed in /etc/pacman.d/"
		echo "2) No    - Use a server listed directly in /etc/pacman.conf"
		read -n1 yesorno
		case ${yesorno} in
			1) server_mirrorlist ;;
			2) server_directly ;;
		esac
	done
}

function server_mirrorlist() {
	while true;do
		yesorno=""
		echo ""
		echo "Download a mirror list package from the Internet. Enter the URL."
		echo "(example: https://example.com/hoge/fuga-mirrorlist-x.y.z-r-any.pkg.tar.zst)"
		echo "(To return to the previous, press return key.)"
		local pkgurl
		read pkgurl
		if [[ ${pkgurl} = "" ]]; then
			repo_server
		fi
		yes|pacman -U "${pkgurl}"

		echo "Enter the path to mirror list."
		echo "(example: /etc/pacman.d/fuga-mirrorlist)"
		local mirrorpath
		read mirrorpath
		if [[ ${mirrorpath} = "" ]]; then
			echo "Need to enter the path to mirror list!"
			server_mirrorlist
		fi
		echo "Include = ${mirrorpath}"|tee -a ${tmpdir}/preview.txt
		sed -i 's/# Server/Server/g' ${mirrorpath}
		repo_confirm
	done
}

function server_directly() {
	while true;do
		yesorno=""
		echo ""
		echo "Enter the URL of repository."
		echo "(example: https://example.com/hoge/x86_64)"
		echo "(To return to the previous, press return key.)"
		local serverurl
		read serverurl
		echo "Server = ${serverurl}"|tee -a ${tmpdir}/preview.txt
		repo_confirm
		done
}

function repo_confirm() {
	while true;do
		yesorno=""
		echo ""
		echo "Are you sure you want to add following content? [1=yes or 2=no]"
		echo ""
		cat ${tmpdir}/preview.txt
		echo ""
		echo ""
		read -n1 yesorno
		case ${yesorno} in
			1) repo_end ;;
			2) repo_abort ;;
		esac
	done
}
	
function repo_end() {
	echo "Writing to /etc/pacman.conf..."
	cp /etc/pacman.conf /etc/pacman.conf.bak
	cat ${tmpdir}/preview.txt|tee -a /etc/pacman.conf
	pacman -Syyuu
	rm -rf ${tmpdir}
	echo "Done!"
	echo ""
	echo "If something goes wrong, run the following command to fix it:"
	echo "sudo mv /etc/pacman.conf.bak /etc/pacman.conf"
	exit 0
}

function repo_abort() {
	echo ""
	echo "Operation Aborted."
	exit 0
}
	main
