#!/bin/sh

###############################################################################
# Bash and zsh customization
# Author: Tim Schlottmann
# Version 1.1.1
# (C) 2022
###############################################################################

###############################################################################
# Run this script using the following command:
# . <(wget -q https://raw.timmoth.de/scripts/fancy_shell/linux/install.sh -O -)
###############################################################################

echo "Please run this script for every user that should have the fancy shell"


usage() {
    echo "Bash and zsh customization - installation script"
    echo "Version 1.0.0"
    echo "Author: Tim Schlottmann"
    echo "(C) 2022"
    echo ""
    echo "Usage:"
    echo $(basename "$0" /path/for/oh-my-posh)
}


REMOTE_LINUX_PATH="https://raw.timmoth.de/scripts/fancy_shell/linux"
LOCAL_CONFIG_PATH="${HOME}/.config/fancy_shell"

BASHRC_PATH="${REMOTE_LINUX_PATH}/.bashrc"
ZSHRC_PATH="${REMOTE_LINUX_PATH}/.zshrc"

ARCH=$(dpkg --print-architecture)
REMOTE_OHMYPOSH_PATH="https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-${ARCH}"
REMOTE_OHMYPOSH_THEMES_PATH="https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip"

LOCAL_SETUP="${LOCAL_CONFIG_PATH}/setup.sh"
REMOTE_SETUP="${REMOTE_LINUX_PATH}/setup.sh"

if ! [ -v $1 ]
then
    LOCAL_OHMYPOSH_PATH="$1"
    echo "Using custom path for oh-my-posh. Please make sure that the path $1 is available via PATH"
    echo "Remark: PATH can be modified in the file \"${LOCAL_CONFIG_PATH}/.rc_local\". This file will be sourced before oh-my-posh is needed."
else
    LOCAL_OHMYPOSH_PATH="/usr/local/bin"
fi
LOCAL_OHMYPOSH_THEMES_PATH="${LOCAL_CONFIG_PATH}/.poshthemes"

check_prerequisits() {
    # Check for internet connectivity
    if ! nc -zw1 raw.timmoth.de 443
    then
        # Detecting sudo capabilities
        # Disabled -> Does not work
        # if id -nG $UID | grep -qw "sudo"; then
        #     echo "Using sudo..."
        #     CMD_ROOT="sudo"
        # else
            >&2 echo "No connection to raw.timmoth.de..."
            >&2 echo "Exiting..."
            exit 1
        #     echo "Using su..."
        #     CMD_ROOT="su - -c"
        # fi
    fi

    if [ $UID -eq 0 ] || [ ! -v $1 ]
    then
        SUDO_CMD=""
    else
        SUDO_CMD="sudo"
    fi

    # nc
    if [ -z $(which nc) ]
    then
        >&2 echo "nc not found"
        >&2 echo "Trying installation..."
        >&2 ${SUDO_CMD} bash -c "apt-get install netcat -y"
        >&2 echo "Installation finished..."
    fi

    # wget
    if [ -z $(which wget) ]
    then
        >&2 echo "wget not found"
        >&2 echo "Trying installation..."
        >&2 ${SUDO_CMD} bash -c "apt-get install wget -y"
        >&2 echo "Installation finished..."
    fi

    # # unzip
    # if [ -z $(which unzip) ]
    # then
    #     >&2 echo "unzip not found"
    #     >&2 echo "Trying installation..."
    #     >&2 ${SUDO_CMD} bash -c "apt-get install unzip -y"
    #     >&2 echo "Installation finished..."
    # fi

    # Creating paths
    mkdir -p ${LOCAL_CONFIG_PATH}
}

install_oh-my-posh() {
    echo "Downloading and installing oh-my-posh..."

    ${SUDO_CMD} wget -q ${REMOTE_OHMYPOSH_PATH} -O ${LOCAL_OHMYPOSH_PATH}/oh-my-posh > /dev/null
    ${SUDO_CMD} chmod +x ${LOCAL_OHMYPOSH_PATH}/oh-my-posh > /dev/null

    # Themes are not needed, since custom theme is used

    # echo "Downloading poshthemes and unpack them to $2/.poshthemes"
    # mkdir ${LOCAL_OHMYPOSH_THEMES_PATH} > /dev/null
    # wget -q ${REMOTE_OHMYPOSH_THEMES_PATH} -O ${LOCAL_OHMYPOSH_THEMES_PATH}/themes.zip > /dev/null
    # unzip ${LOCAL_OHMYPOSH_THEMES_PATH}/themes.zip -d ${LOCAL_OHMYPOSH_THEMES_PATH} > /dev/null
    # chmod u+rw ${LOCAL_OHMYPOSH_THEMES_PATH}/*.json > /dev/null
    # rm ${LOCAL_OHMYPOSH_THEMES_PATH}/themes.zip > /dev/null
}

install_update_script() {
    ${SUDO_CMD} echo "source <(wget -q https://raw.timmoth.de/scripts/fancy_shell/linux/install.sh -O -)" > /sbin/update_fancy_shell
    ${SUDO_CMD} chmod +x /sbin/update_fancy_shell > /dev/null
}

setup_configurations() {
    echo "Deleting previous configurations..."
    mv -i ${HOME}/.zshrc ${HOME}/.zshrc.pre-fancy_shell.bak
    mv -i ${HOME}/.bashrc ${HOME}/.bashrc.pre-fancy_shell.bak
    rm ${HOME}/.zshrc
    rm ${HOME}/.bashrc

    echo "Setting up new configurations..."
    ln -s ${LOCAL_SETUP} "${HOME}/.bashrc"
    ln -s ${LOCAL_SETUP} "${HOME}/.zshrc"

    echo "Downloading additional file"
    wget -q "${REMOTE_SETUP}" -O "${LOCAL_SETUP}"
    if ! [ -s ${LOCAL_SETUP} ]
    then
        echo "File is empty. This might indicate a failure"
    fi

    echo "Activating configuration"
    if [[ -v ZSH_VERSION ]]
    then
        . ${HOME}/.zshrc
    elif [[ -v BASH_VERSION ]]
    then
        . ${HOME}/.bashrc
    fi

    echo "Finished..."
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]
then
    usage
    return 0
fi

check_prerequisits
if [ -z $(which oh-my-posh) ]
then
    install_oh-my-posh
fi
install_update_script
setup_configurations

# Unset variables
unset REMOTE_LINUX_PATH
unset LOCAL_CONFIG_PATH
unset BASHRC_PATH
unset ZSHRC_PATH
unset ARCH
unset REMOTE_OHMYPOSH_PATH
unset REMOTE_OHMYPOSH_THEMES_PATH
unset LOCAL_OHMYPOSH_PATH
unset LOCAL_OHMYPOSH_THEMES_PATH
unset LOCAL_SETUP
unset REMOTE_SETUP
unset SUDO_CMD


###############################################################################
# Changelog
###############################################################################

# Version 1.0.0
# - Initial release

# Version 1.1.0
# - Allow definition of custom path for oh-my-posh definition

# Version 1.1.1
# - Fix oh-my-posh installation
