#!/usr/bin/env bash
#
# install.sh
#
# Installs dotfiles and packages for my setup.
# Needs to be invoked from containing dotfile directory to work correctly.
#
# Will first install yay, then all my used packages (read from bootstrap/packages.txt)
#
# Finally, symlinks all dotfiles into their correct locations using dotter

bootstrap_dir="${DOT_BOOTSTRAP_DIR:-./bootstrap}"
unattended_install="${DOT_UNATTENDED_INSTALL:-false}"
link_systemfiles="${DOT_LINK_SYSTEMFILES:-true}"

help() {
	printf "Usage: install [-f|--force][-v|--version][-h|--help]\n\n-f     Do not ask for any confirmations but force update and installation.\n"
	exit "${1:-0}"
}

main() {
    local cmd=""
    local ret=0
	case "$1" in
	-v | --version)
		printf "Personal system bootstrap script.\n\nby Marty Oehme\n\nv0.2\n"
		;;
	-h | --help)
		help 0
		;;
	-f | --force)
		unattended_install=true
		;;
	*)
	    install
		;;
	esac
	shift

    $cmd "$@"
    ret=$((ret + $?))
    exit $ret
}

# takes default value (y/n), question, abort message as arguments
# automatically answers yes if unattended install
check_consent() {
	if [ "$unattended_install" == "true" ]; then
		true
	else
		[[ "$1" == "y" ]] && default_consent="[Y/n]" || default_consent="[y/N]"
		printf "%b %b " "$2" "$default_consent"
		read -r answer
		if [[ "$1" == "n" ]] && [[ "$answer" != y* ]]; then
			printf "%s\n" "$3"
			false
		elif [[ "$1" == "y" ]] && [[ "$answer" == n* ]]; then
			printf "%s\n" "$3"
			false
		else
			true
		fi
	fi
}

entry_question() {
	check_consent n "This will take a while and install many packages and link dotfiles all over the place depending on your selections.\nYou need to be in the base directory of the dotfiles repository.\nProceed?" "Aborting." || exit
}

enable_git_hooks() {
	check_consent y "Should we enable git hooks for this repository, so that missing or additionally installed packages are automatically compared to the repository when committing?" "Not changing repository settings." || return
	git config --local core.hooksPath .githooks/
	echo "Changed repository settings."
}

manage_dotfiles() {
	check_consent y "Link dot files?" "Not linking dotfiles." || return
	check_consent n "Link system settings files? This will require sudo access but will not overwrite existing files." "Not touching system files." || link_systemfiles=false
	if [ "$link_systemfiles" == "false" ]; then
        dotter deploy
	    echo "Linked dotfiles."
    else
	    if [ -e "/etc/pacman.conf" ]; then
		    check_consent n "Found an existing pacman.conf file, installation will error if it exists. Remove file?" && run_elevated rm "/etc/pacman.conf"
	    fi
        dotter deploy -l .dotter/systemwide.toml
	    echo "Linked dotfiles and system files."
	fi
}

run_elevated() {
	if command -v doas >/dev/null 2>&1; then
		doas "$@"
	elif command -v sudo >/dev/null 2>&1; then
		sudo "$@"
	fi
}


install_packages() {
	check_consent n "Install pre-designated packages? This will take a while, install a lot of packages and require super user privileges." "Not installing packages." || return
	if [ "$unattended_install" == "true" ]; then
		"$bootstrap_dir"/install_packages.sh -f
	else
		"$bootstrap_dir"/install_packages.sh
	fi
	echo "Installed packages."
}

headline() {
	len="${#1}"
	target_len=85
	side_len=$(((target_len - len) / 2))
	for ((i = 1; i <= side_len; i++)); do printf '='; done
	printf " %s " "$1"
	for ((i = 1; i <= side_len; i++)); do printf '='; done
	printf "\n"
}

install() {
	if [ "$unattended_install" == false ] ; then
		entry_question
	fi

	headline "BEGINNING PACKAGE INSTALLATION"
	install_packages

	headline "BEGINNING DOTFILE MANAGEMENT"
	manage_dotfiles

	headline "ENABLING GIT REPOSITORY HOOKS"
	enable_git_hooks

	echo "INSTALLATION FINISHED"
	exit 0
}

main "$@"