#!/usr/bin/env bash

source $HOME/dotfiles/bin/helpers/functions.sh
export DOTFILES_CONFIG=$HOME/dotfiles/config/config.yaml

status() {
    $HOME/dotfiles/bin/actions/status.sh $@
}

update() {
    $HOME/dotfiles/bin/actions/update.sh $@
}

help() {
    $HOME/dotfiles/bin/actions/help.sh $@
}

exports() {
    $HOME/dotfiles/bin/actions/export.sh $@
}

secrets() {
    $HOME/dotfiles/bin/actions/secrets.sh $@
}

push() {
    $HOME/dotfiles/bin/actions/push.sh $@
}

ensure_git_hooks() {
    # If ~/dotfiles/.git/hooks is a symlink, skip this
    if [[ -L ~/dotfiles/.git/hooks ]]; then
        # Let's make sure the symlink is correct
        if [[ $(readlink ~/dotfiles/.git/hooks) != $HOME/dotfiles/bin/actions/git ]]; then
            printfe "%s\n" "yellow" "The ~/dotfiles/.git/hooks symlink is incorrect. Please remove it and run this script again."
        fi
        return
    fi

    if [[ -d ~/dotfiles/.git/hooks ]]; then
        rm -rf ~/dotfiles/.git/hooks
        printfe "%s\n" "yellow" "The ~/dotfiles/.git/hooks directory already exists. We're removing it!"
    fi

    ln -s $HOME/dotfiles/bin/actions/git ~/dotfiles/.git/hooks
    printfe "%s\n" "green" "Git hooks are now set up!"
}

ensure_git_hooks

# switch case for parameters
case $1 in
    "update")
        logo
        update $@
        ;;
    "push")
        logo continue
        push $@
        ;;
    "status")
        logo continue
        status $@
        ;;
    "export")
        logo
        exports $@
        ;;
    "help"|"--help"|"")
        help $@
        ;;
    "secrets")
        secrets $@
        ;;
    "startup")
        $HOME/dotfiles/bin/actions/startup-runner.sh $@
        ;;
    "term")
        $HOME/dotfiles/bin/actions/term.sh $@
        ;;
    "hotkey-daemon")
        x-terminal-emulator -e $HOME/dotfiles/bin/actions/hotkey-daemon.sh $@
        ;;
    *)
        printfe "%s\n" "red" "Unknown command $1"
        help $@
        ;;
esac
