#!/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 stow

bootstrap_dir="${BOOTSTRAP_DIRECTORY:-./bootstrap}"

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)
        printf "Usage: install [-f|--force][-v|--version][-h|--help]\n\n-f     Do not ask for any confirmations but force update and installation.\n"
        ;;
    -f | --force)
        install true
        ;;
    *)
        install false
        ;;
    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" == "true" ]; then
        true
    else
        [[ "$1" == "y" ]] && default_consent="[Y/n]" || default_consent="[y/N]"
        printf "%s %s " "$2" "$default_consent"
        read -r answer
        if [[ "$1" == "n" ]] && [[ "$answer" != y* ]]; then
            printf "%s\n" "$3"
            false
        elif [[ "$1" == "y" ]] && [[ "$answer" == n* ]]; then
            echo second
            printf "%s\n" "$3"
            false
        else
            true
        fi
    fi
}

entry_question() {
    check_consent n "This will take a while, install many packages and link dotfiles all over the place. Proceed?" "Aborting." || exit
}

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

stow_dotfiles() {
    check_consent y "Link home directory dot files?" "Not linking dotfiles." || return
    # get all top level directories, remove their slashes and dots
    # finally get rid of .dot-directories, since they are for the repo not for my homedir
    targets="$(find . -maxdepth 1 -type d | sed -e 's/^\.\/\(.*\)$/\1/' | sed -e '/^\./d')"

    # shellcheck disable=2086
    # -- for some reason stow only works with unqoted var expansion
    stow -R ${targets} 2> >(grep -v 'Absolute/relative mismatch between Stow dir' 1>&2)
    echo "Linked dotfiles."
}

stow_system_packages() {
    check_consent n "Link system settings as well? This will require sudo access." "Not touching system files." || return
    sudo stow --dir="$bootstrap_dir" --target="/" -R system-packages/
    echo "Linked system files."
}

install_packages() {
    check_consent n "Install pre-designated packages? This will take a while." "Not installing packages." || return
    export BOOTSTRAP_PACKAGES="bootstrap/packages.txt"
    if ! "$UNATTENDED"; then
        "$bootstrap_dir"/install_packages.sh
    else
        "$bootstrap_dir"/install_packages.sh -f
    fi
    unset BOOTSTRAP_PACKAGES
    echo "Installed packages."
}

install() {
    UNATTENDED=$1
    if ! "$UNATTENDED"; then
        entry_question
    fi

    echo "====================== BEGINNING INSTALLATION ============================="
    install_packages

    echo "=================== BEGINNING DOTFILE MANAGEMENT =========================="
    stow_dotfiles
    stow_system_packages

    echo "================== ENABLING GIT REPOSITORY HOOKS =========================="
    enable_git_hooks

    echo "====================== INSTALLATION FINISHED =============================="
    exit 0
}

main "$@"
