#!/bin/zsh

# This is the intial install script for quaxlyqueen/scripts.
# It will install the scripts and set up the environment.

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
}

install_gh() {
    # Install github-cli
    if ! command -v "gh" &> /dev/null
    then
        echo "gh not found. Installing gh..."
        COMMAND="gh"
        PACKAGE="github-cli"

        if [[ "$OSTYPE" == "darwin"* ]]; then

            DISTRO=$(cat /etc/os-release | grep -oP '(?<=^ID=).+' | tr -d '"')

            if [[ $DISTRO == "arch" ]]; then
                yes | sudo pacman -S $PACKAGE
            elif [[ $DISTRO == "archarm" ]]; then
                yes | sudo pacman -S $PACKAGE
            elif [[ $DISTRO == "ubuntu" ]]; then
                yes | sudo apt-get install $PACKAGE
            elif [[ $DISTRO == "fedora" ]]; then
                yes | sudo dnf install $PACKAGE
            elif [[ $DISTRO == "centos" ]]; then
                yes | sudo yum install $PACKAGE
            elif [[ $DISTRO == "debian" ]]; then
                yes | sudo apt install $PACKAGE
            else
                echo "Unsupported distro. Please install $COMMAND from $PACKAGE manually."
            fi

        elif [[ "$OSTYPE" == "darwin"* ]]; then
            echo "gh not found. Installing gh..."

            if command -v "brew" &> /dev/null
            then
                yes | brew install $COMMAND

            elif command -v "port" &> /dev/null
            then
                yes | sudo port install $COMMAND

            else
                echo "Homebrew or MacPorts not found. Please install one of them before running this script."
            fi
        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

    # If the user has not set up github-cli, do so now
    install_gh
    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

    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
}

# 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
