#!/bin/zsh

help() {
    echo "This script installs and sets up the configuration for the repo quaxlyqueen/scripts."
    echo "It will install the scripts in /usr/local/bin and set up the environment variables and aliases."
    echo "The user will be prompted to:"
    echo "  - Set up the environment variables and aliases"
    echo "  - Create a theme for supported applications"
    echo "  - Set up Git, github-cli (gh), and gclone"

    echo "\nUsage: sudo ./install"
    echo "       sudo ./install -h | --help"
    exit 0
}

# The script must be run as root
is_root() {
    if [[ $EUID -ne 0 ]]; then
        echo "This script must be run as root." 
        exit 1
    fi
}

create_theme() {
    USER=$1
    su $USER -c "mkdir /home/$USER/.config/theme"

    echo "\nTheming currently supports .theme files with 3 colors: base, accent, and hover."
    echo "These themes can be changed at anytime by editing the .theme file in ~/.config/theme."
    echo "Supported applications will automatically use the active.theme link to theme themselves."
    echo "active.theme is a symbolic link to (for eg.) light.theme or dark.theme."

    echo "\nMany themes can be created with this format, and using the 'set-theme <theme>' command."
    echo "See 'set-theme --help' for more information."

    echo "\n\nYou can create a theme now or use the default light and dark themes."
    read "?Do you want to create a theme now? (y/n) " CHOICE

    USER=$SUDO_USER
    DIR="/home/$USER/.config/theme"

    if [[ $CHOICE == "y" ]]; then
        echo "\n"
        read "?Please enter the base color in hex format (e.g. #ffffff): " BASE
        read "?Please enter the accent color in hex format (e.g. #ffffff): " ACCENT
        read "?Please enter the hover color in hex format (e.g. #ffffff): " HOVER
        read "?Please enter the name of the theme: " NAME

        su $USER -c "echo 'base=$BASE' > $DIR/$NAME.theme"
        su $USER -c "echo 'accent=$ACCENT' >> $DIR/$NAME.theme"
        su $USER -c "echo 'hover=$HOVER' >> $DIR/$NAME.theme"

        echo "Theme created successfully."
        echo "You can now use the 'set-theme $NAME' command to use this theme."
        echo "Setting the active theme to $NAME.theme..."
        su $USER -c "ln -s $DIR/$NAME.theme $DIR/active.theme"

    else
        echo "Creating default light and dark themes..."
        su $USER -c "echo 'base=#C6CAED' > $DIR/light.theme"
        su $USER -c "echo 'accent=#4D5382' >> $DIR/light.theme"
        su $USER -c "echo 'hover=#262940' >> $DIR/light.theme"

        su $USER -c "echo 'base=#262940' > $DIR/dark.theme"
        su $USER -c "echo 'accent=#4D5382' >> $DIR/dark.theme"
        su $USER -c "echo 'hover=#C6CAED' >> $DIR/dark.theme"

        read "?Do you want to set the active theme to the dark theme? (y/n) " CHOICE

        if [[ $CHOICE == "y" ]]; then
            echo "Setting the active theme to dark.theme..."
            su $USER -c "ln -s $DIR/dark.theme $DIR/active.theme"
        else
            echo "Setting the active theme to light.theme..."
            su $USER -c "ln -s $DIR/light.theme $DIR/active.theme"
        fi
    fi
}

setup_git() {
    # Set up gclone
    if [ ! -d "/home/$USER/.config/gclone" ]; then
        su $USER -c "mkdir /home/$USER/.config/gclone"
        su $USER -c "touch /home/$USER/.config/gclone/gclone.conf"

        read "?Please enter your GitHub user name for gclone: " GHUSER
        su $USER -c "gclone --set-default $GHUSER"
    fi

    # If the user has not set up git, do so now
    if [ ! -f "/home/$USER/.gitconfig" ]; then
        read "?Please enter your name for Git: " NAME
        read "?Please enter your email for Git: " EMAIL

        su $USER -c "git config --global user.name $NAME"
        su $USER -c "git config --global user.email $EMAIL"
    fi

    # Install github-cli if it is not already installed
    check_dependency "gh" "github-cli"

    # If the user has not set up github-cli, do so now
    if [ ! -f "/home/$USER/.config/gh/hosts.yml" ]; then
        echo "Please log in to GitHub using github-cli."
        echo "This will allow you to modify your repositories and other GitHub features."

        su $USER -c "gh auth login"
    fi
}

# Create environment variables and global aliases
setup_environment() {
    echo "Setting up the environment..."
    read "?By default, the scripts will install in the /usr/local/bin directory. Is this OK? (y/n): " CHOICE

    if [[ $CHOICE == "n" ]]; then
        read "?Please enter the directory to install the scripts: " DIR
    else
        DIR="/usr/local/bin"
    fi

    echo "# Auto-generated by quaxlyqueen/scripts. Environment setup" >> ~/.zshrc
    echo "alias gt='. goto $1'" >> ~/.zshrc
    echo "alias goto='. goto $1'" >> ~/.zshrc
    echo "export PATH=$PATH:$DIR" >> ~/.zshrc

    # Source the .zshrc file to update the environment
    source ~/.zshrc

    USER=$SUDO_USER
    if [ ! -d "/home/$USER/.config" ]; then
        su $USER -c "mkdir /home/$USER/.config"
        create_theme $USER
    fi

    if [ ! -d "/home/$USER/.config/theme" ]; then
        create_theme $USER
    fi

    setup_git
}

is_root
if [[ $# -eq 1 ]]; then
    if [[ $1 == "-h" || $1 == "--help" ]]; then
        help
    fi
fi

# Install the scripts
echo "Installing scripts..."
sudo cp * /usr/local/bin
setup_environment

echo "Installation complete. Please restart your terminal to use the new commands, or run 'source ~/.zshrc'."
echo "To remove the scripts, simply delete the files in /usr/local/bin and remove the aliases from your .zshrc file."
echo "Thank you for using quaxlyqueen/scripts!"
exit 0
