#!/bin/sh -e

. ../common-script.sh

gitpath="$HOME/.local/share/mybash"

installDepend() {
    if [ ! -f "/usr/share/bash-completion/bash_completion" ] || ! command_exists bash tar bat tree unzip fc-list git; then
        printf "%b\n" "${YELLOW}Installing Bash...${RC}"
        case "$PACKAGER" in
            pacman)
                "$ESCALATION_TOOL" "$PACKAGER" -S --needed --noconfirm bash bash-completion tar bat tree unzip fontconfig git fzf 
                ;;
            apk)
                "$ESCALATION_TOOL" "$PACKAGER" add bash bash-completion tar bat tree unzip fontconfig git
                ;;
            xbps-install)
                "$ESCALATION_TOOL" "$PACKAGER" -Sy bash bash-completion tar bat tree unzip fontconfig git
                ;;
            *)
                "$ESCALATION_TOOL" "$PACKAGER" install -y bash bash-completion tar bat tree unzip fontconfig git
                ;;
        esac
    fi
}

cloneMyBash() {
    # Check if the dir exists before attempting to clone into it.
    if [ -d "$gitpath" ]; then
        rm -rf "$gitpath"
    fi
    mkdir -p "$HOME/.local/share" # Only create the dir if it doesn't exist.
    cd "$HOME" && git clone https://github.com/ChrisTitusTech/mybash.git "$gitpath"
}

installFont() {
    # Check to see if the MesloLGS Nerd Font is installed (Change this to whatever font you would like)
    FONT_NAME="MesloLGS Nerd Font Mono"
    if fc-list :family | grep -iq "$FONT_NAME"; then
        printf "%b\n" "${GREEN}Font '$FONT_NAME' is installed.${RC}"
    else
        printf "%b\n" "${YELLOW}Installing font '$FONT_NAME'${RC}"
        # Change this URL to correspond with the correct font
        FONT_URL="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/Meslo.zip"
        FONT_DIR="$HOME/.local/share/fonts"
        TEMP_DIR=$(mktemp -d)
        curl -sSLo "$TEMP_DIR"/"${FONT_NAME}".zip "$FONT_URL"
        unzip "$TEMP_DIR"/"${FONT_NAME}".zip -d "$TEMP_DIR"
        mkdir -p "$FONT_DIR"/"$FONT_NAME"
        mv "${TEMP_DIR}"/*.ttf "$FONT_DIR"/"$FONT_NAME"
        fc-cache -fv
        rm -rf "${TEMP_DIR}"
        printf "%b\n" "${GREEN}'$FONT_NAME' installed successfully.${RC}"
    fi
}

_install_pkg() {
    case "$PACKAGER" in
        pacman) "$ESCALATION_TOOL" "$PACKAGER" -S --needed --noconfirm "$1" ;;
        apk)    "$ESCALATION_TOOL" "$PACKAGER" add "$1" ;;
        xbps-install) "$ESCALATION_TOOL" "$PACKAGER" -Sy "$1" ;;
        dnf|apt-get|nala|zypper|eopkg) "$ESCALATION_TOOL" "$PACKAGER" install -y "$1" ;;
        *)      printf "%b\n" "${YELLOW}Unsupported package manager, falling back to manual install...${RC}" && return 1 ;;
    esac
}

installStarshipAndFzf() {
    if ! command_exists starship; then
        printf "%b\n" "${YELLOW}Installing Starship...${RC}"
        _install_pkg starship 2>/dev/null || {
            printf "%b\n" "${YELLOW}Package manager install failed, using curl...${RC}"
            curl -sSL https://starship.rs/install.sh | "$ESCALATION_TOOL" sh || {
                printf "%b\n" "${RED}Failed to install starship!${RC}"
                exit 1
            }
        }
    else
        printf "%b\n" "${GREEN}Starship already installed${RC}"
    fi

    if ! command_exists fzf; then
        printf "%b\n" "${YELLOW}Installing fzf...${RC}"
        _install_pkg fzf 2>/dev/null || {
            printf "%b\n" "${YELLOW}Package manager install failed, using git...${RC}"
            git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
            "$ESCALATION_TOOL" ~/.fzf/install
        }
    else
        printf "%b\n" "${GREEN}Fzf already installed${RC}"
    fi
}

installZoxide() {
    if command_exists zoxide; then
        printf "%b\n" "${GREEN}Zoxide already installed${RC}"
        return
    fi

    printf "%b\n" "${YELLOW}Installing Zoxide...${RC}"
    _install_pkg zoxide 2>/dev/null || {
        printf "%b\n" "${YELLOW}Package manager install failed, using curl...${RC}"
        if ! curl -sSL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh; then
            printf "%b\n" "${RED}Something went wrong during zoxide install!${RC}"
            exit 1
        fi
    }
}

linkConfig() {
    OLD_BASHRC="$HOME/.bashrc"
    if [ -e "$OLD_BASHRC" ] && [ ! -e "$HOME/.bashrc.bak" ]; then
        printf "%b\n" "${YELLOW}Moving old bash config file to $HOME/.bashrc.bak${RC}"
        if ! mv "$OLD_BASHRC" "$HOME/.bashrc.bak"; then
            printf "%b\n" "${RED}Can't move the old bash config file!${RC}"
            exit 1
        fi
    fi

    printf "%b\n" "${YELLOW}Linking new bash config file...${RC}"
    ln -svf "$gitpath/.bashrc" "$HOME/.bashrc" || {
        printf "%b\n" "${RED}Failed to create symbolic link for .bashrc${RC}"
        exit 1
    }

    mkdir -p "$HOME/.config"
    ln -svf "$gitpath/starship.toml" "$HOME/.config/starship.toml" || {
        printf "%b\n" "${RED}Failed to create symbolic link for starship.toml${RC}"
        exit 1
    }
    printf "%b\n" "${GREEN}Done! restart your shell to see the changes.${RC}"
}

checkEnv
installDepend
cloneMyBash
installFont
installStarshipAndFzf
installZoxide
linkConfig
