#!/usr/local/bin/bash
#
# FILE  : dotfiles.core/new-account-setup.sh
# TARGET: none
# AUTHOR: tgwil
#
# This script sets up a new account. As this is the dotfiles.core
# installer, only things which apply to every account are done here.
#
# This installer assumes that:
# - the dotfiles.core repository has been cloned into /usr/local/share/
# - the user has at least rx permissions for /usr/local/share/dotfiles.core/
#
# This installer also assumes that dotfiles.sys and all necessary
# packages have been installed.


# Messages passed to the user by this script are in 8-char columns.
#Created:
#Removed:
#Moved  :
#Skipped:
#
#ERROR  :
#WARNING:
#INFO   :
#
#File   :
#Dir    :
#Symlink:


########################################################################
## Setup
########################################################################
# Return to home
cd

# Set script output colors
GREEN="\e[32m"
YELLOW="\e[33m"
WARN="\e[1;31m"
ENDCOLOR="\e[0m"


# Startup message
echo "### Starting dotfiles.core installation... ###"

# Warn user about dependencies
echo -e "${WARN}WARNING: This installer assumes dotfiles.sys and sys-level packages have been installed. [A]bort, [C]ontinue: ${ENDCOLOR}"
echo -en "\033[1A\033[1000C"
read -n1 confirm
confirm_invalid_selection=0
while
    case $confirm in
        "a"|"A")
            echo "INFO   : Abort!"
            exit 0;;
        "c"|"C")
            break;;
        *)
            echo -e "${WARN}ERROR  : Invalid selection, try again...${ENDCOLOR}"
            confirm_invalid_selection=1;;
    esac
    [[ $confirm_invalid_selection==1 ]]
do true; done


########################################################################
## Install Packages
########################################################################
echo "INFO   : Proceed with package installation? Requires sudo privileges [y/n]: "
echo -en "\033[1A\033[1000C"
read -n1 do_package_install
package_invalid_selection=0
while
    case $do_package_install in
        "y"|"Y")
            sudo pkg update
            sudo pkg upgrade && sudo pkg install \
                                     audio/dsbmixer \
                                     audio/gtk-mixer \
                                     graphics/drm-kmod \
                                     graphics/ImageMagick7 \
                                     graphics/poppler-glib \
                                     mail/isync \
                                     www/firefox \
                                     x11/arandr \
                                     x11-fonts/nerd-fonts \
                                     x11/xorg
            # Install Quicklisp
            if [[ ! -d $HOME/lisp/quicklisp ]]; then
                curl -o /tmp/ql.lisp http://beta.quicklisp.org/quicklisp.lisp
                sbcl --no-sysinit --no-userinit --load /tmp/ql.lisp \
                     --eval '(quicklisp-quickstart:install :path "~/lisp/quicklisp")' \
                     --quit \
                     > /dev/null # minimize output
                echo -e "${GREEN}INFO   : Installed Quicklisp${ENDCOLOR}"
            else
                echo -e "${YELLOW}Skipped: Quicklisp. Already installed${ENDCOLOR}"
            fi

            echo "INFO   : Packages installed and updated."
            break;;
        "n"|"N")
            echo "INFO   : Skipping package installation and updates."
            break;;
        *)
            echo -e "${WARN}ERROR  : Invalid selection, try again...${ENDCOLOR}"
            package_invalid_selection=1;;
    esac
    [[ $package_invalid_selection==1 ]]
do true; done


########################################################################
## Put files into place
########################################################################
# File placement function
place_symlink () {
    file=$1
    target=$2
    if [[ -e $target ]]; then
        echo -e \
             "${WARN}WARNING: File   : ${target} already exists!" \
             "[A]bort, [D]elete, [R]eplace, [S]kip: ${ENDCOLOR}"
        invalid_selection=0
        while
            # The following echo fixes read's annoying newline that
            # messes up my nice script formatting. It sends cursor back
            # to the "target already exists" line.
            # \033[1A moves up to previous line.
            # \033[1000C moves to the last column in the line up to 1000.
            echo -en "\033[1A\033[1000C"
            read -n1 selection
            case $selection in
                "a"|"A")
                    echo "INFO   : Abort!"
                    exit 0;;
                "d"|"D")
                    rm -fv $target
                    echo -e "${YELLOW}Removed: File   : ${target}${ENDCOLOR}"
                    ln -s $file $target
                    echo -e "${GREEN}Created: Symlink: ${target} -> ${file}${ENDCOLOR}"
                    return 0;;
                "r"|"R")
                    mv -f $target $target.bak
                    echo -e "${YELLOW}Moved  : File   : ${target} to ${target}.bak${ENDCOLOR}"
                    ln -s $file $target
                    echo -e "${GREEN}Created: Symlink: ${target} -> ${file}${ENDCOLOR}"
                    return 0;;
                "s"|"S")
                    echo -e "${YELLOW}Skipped: File   : ${target}${ENDCOLOR}"
                    return 0;;
                *)
                    echo -e "${WARN}ERROR  : Invalid selection, try again...${ENDCOLOR}"
                    invalid_selection=1;;
            esac
            [[ $invalid_selection==1 ]]
        do true; done
    else
        ln -s $file $target
        echo -e "${GREEN}Created: File   : ${target} -> ${file}${ENDCOLOR}"
    fi
}

# Directory placement function
place_directory () {
    target=$1
    if [[ -d $target ]]; then
        echo -e \
             "${WARN}WARNING: Dir    : ${target} already exists!" \
             "[A]bort, [D]elete, [R]eplace, [S]kip: ${ENDCOLOR}"
        invalid_selection=0
        while
            # The following echo fixes read's annoying newline that
            # messes up my nice script formatting. It sends cursor back
            # to the "target already exists" line.
            # \033[1A moves up to previous line.
            # \033[1000C moves to the last column in the line up to 1000.
            echo -en "\033[1A\033[1000C"
            read -n1 selection
            case $selection in
                "a"|"A")
                    echo "INFO   : Abort!"
                    exit 0;;
                "d"|"D")
                    rm -rf $target
                    echo -e "${YELLOW}Removed: Dir    : ${target}${ENDCOLOR}"
                    mkdir -p $target
                    echo -e "${GREEN}Created: Dir    : ${target}${ENDCOLOR}"
                    return 0;;
                "r"|"R")
                    mv -f $target $target.bak
                    echo -e "${YELLOW}Moved  : Dir    : ${target} to ${target}.bak${ENDCOLOR}"
                    mkdir -p $target
                    echo -e "${GREEN}Created: Dir    : ${target}${ENDCOLOR}"
                    return 0;;
                "s"|"S")
                    echo -e "${YELLOW}Skipped: Dir    : ${target}${ENDCOLOR}"
                    return 0;;
                *)
                    echo -e "${WARN}ERROR  : Invalid selection, try again...${ENDCOLOR}"
                    invalid_selection=1;;
            esac
            [[ $invalid_selection==1 ]]
        do true; done
    else
        mkdir -p $target
        echo -e "${GREEN}Created: Dir    : ${target}${ENDCOLOR}"
    fi
}

# Place files
echo "INFO   : Proceed with dotfiles installation? [y/n]: "
echo -en "\033[1A\033[1000C"
read -n1 do_dotfiles_install
dotfiles_invalid_selection=0
while
    case $do_dotfiles_install in
        "y"|"Y")
            # Create user dir skeleton
            place_directory $HOME/bin

            # Bash Configuration
            place_symlink /usr/local/share/dotfiles.core/home/.bash_profile $HOME/.bash_profile
            place_symlink /usr/local/share/dotfiles.core/home/.bashrc $HOME/.bashrc
            place_symlink /usr/local/share/dotfiles.core/home/.bash_aliases $HOME/.bash_aliases

            # GPG Configuration
            place_directory $HOME/.gnupg/
            place_symlink /usr/local/share/dotfiles.core/home/.gnupg/gpg-agent.conf $HOME/.gnupg/gpg-agent.conf

            # Git configuration
            place_symlink /usr/local/share/dotfiles.core/home/.gitconfig $HOME/.gitconfig
            echo -e "${WARN}WARNING: ~/.gitconfig.local should be placed manually${ENDCOLOR}"

            # Common Lisp SBCL Configuration
            place_symlink /usr/local/share/dotfiles.core/home/.sbclrc $HOME/.sbclrc
            place_directory $HOME/lisp

            echo "INFO   : Configuration files placed!"
            break;;
        "n"|"N")
            echo "INFO   : Skipping configuration file placement."
            break;;
        *)
            echo -e "${WARN}ERROR  : Invalid selection, try again...${ENDCOLOR}"
            dotfiles_invalid_selection=1;;
    esac
    [[ $dotfiles_invalid_selection==1 ]]
do true; done



########################################################################
## END OF SCRIPT
########################################################################
echo -e "INFO   : Installation completed: dotfiles.core"