###############################################################################
# Bash and zsh common file updater
# Author: Tim Schlottmann
# Version 1.4.0
# (C) 2022-2023
###############################################################################

if [[ -v ZSH_VERSION ]]
then
    RC_NAME=".zshrc"
elif [[ -v BASH_VERSION ]]
then
    RC_NAME=".bashrc"
else
    >&2 echo "Incompatible shell detected"
    >&2 echo "Exiting..."
    exit 1
fi

THEME_NAME="thetimmoth.omp.json"

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

LOCAL_RC="${LOCAL_CONFIG_PATH}/${RC_NAME}"
REMOTE_RC="${REMOTE_LINUX_PATH}/${RC_NAME}"

CUSTOM_RC="${LOCAL_CONFIG_PATH}/.rc_local"

LOCAL_SHARED="${LOCAL_CONFIG_PATH}/.rc_shared"
REMOTE_SHARED="${REMOTE_LINUX_PATH}/.rc_shared"

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

LOCAL_THEME="${LOCAL_CONFIG_PATH}/${THEME_NAME}"
REMOTE_THEME="${REMOTE_PATH}/${THEME_NAME}"

check_prerequisits() {
    # Check for internet connectivity
    if nc -zw1 raw.timmoth.de 443
    then
        INTERNET_CONNECTIVITY=1
    else
        >&2 echo "No connection to raw.timmoth.de"
        >&2 echo "Using locally stored data"
        INTERNET_CONNECTIVITY=0
    fi

    # Detecting sudo capabilities
    if id -nG $UID | grep -qw "sudo"; then
        CMD_ROOT="sudo"
    else
        CMD_ROOT="su - -c"
    fi

    # Creating paths
    mkdir -p ${LOCAL_CONFIG_PATH} 2>&1 > /dev/null

    # Skip wget check if no internet is available
    if ! [ ${INTERNET_CONNECTIVITY} -eq 1 ]
    then
        return 0
    fi

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

get_file() {
    if [ -z "$1" ] || [ -z "$2" ]
    then
        echo "Please provide arguments"
        return 1
    fi

    # Compute checksum of old file
    CSUM_BEFORE=$(sha512sum ${2} | cut -d ' ' -f 1)

    if [ -z "${CSUM_BEFORE}" ]
    then
        CSUM_BEFORE=$(echo $RANDOM | sha512sum | cut -d ' ' -f 1)
    fi

    # Get new file
    TEMP_PATH="/tmp/$(echo ${1} | sha512sum | cut -d ' ' -f 1)"
    wget --timeout=1 -q "${1}" -O "${TEMP_PATH}"

    # Compute checksum of new file
    CSUM_AFTER=$(sha512sum ${TEMP_PATH} | cut -d ' ' -f 1)

    # # Debug Output
    # echo "1: ${1}"
    # echo "2: ${2}"
    # echo "CSUM_BEFORE: ${CSUM_BEFORE}"
    # echo "CSUM_AFTER: ${CSUM_AFTER}"
    # echo "TEMP_PATH: ${TEMP_PATH}"
    # echo "WGET: wget --timeout=1 -q \"${1}\" -O \"${TEMP_PATH}\""

    # Update file is checksum is different
    if ! [ ${CSUM_BEFORE} = ${CSUM_AFTER} ]
    then
        cp ${TEMP_PATH} ${2}
    fi

    # Delete temp file
    rm ${TEMP_PATH}

    unset CSUM_AFTER
    unset CSUM_BEFORE
    unset TEMP_PATH
}

update_setup_script() {
    # # Compute checksum of old file
    # CSUM_BEFORE=$(sha512sum ${LOCAL_SETUP} | cut -d ' ' -f 1)

    # Get new file
    get_file ${REMOTE_SETUP} ${LOCAL_SETUP}

    # # Compute checksum of new file
    # CSUM_AFTER=$(sha512sum ${LOCAL_SETUP} | cut -d ' ' -f 1)

    # Restart script if new file was downloaded
    # Disabled -> leads to a closed connection if ssh is used
    #          -> leads to an error of no ssh is used
    #             (-b as invalid option)
    # if ! [ ${CSUM_BEFORE} = ${CSUM_AFTER} ]
    # then
    #     exec $0
    # fi

    # unset CSUM_BEFORE
    # unset CSUM_AFTER
}

update_scripts() {
    get_file ${REMOTE_RC} ${LOCAL_RC}
    get_file ${REMOTE_SHARED} ${LOCAL_SHARED}
    get_file ${REMOTE_THEME} ${LOCAL_THEME}
}

source_scripts() {
    if [ -f ${CUSTOM_RC} ]
    then
        . ${CUSTOM_RC}
    fi
    . ${LOCAL_RC}
    . ${LOCAL_SHARED}
}

main() {
    check_prerequisits
    if [ ${INTERNET_CONNECTIVITY} -eq 1 ]
    then
        update_setup_script
        update_scripts
    fi
    source_scripts

    # printf "Loading personal profiles took"
}

# time main
main

# Unset variables
unset RC_NAME
unset THEME_NAME
unset LOCAL_CONFIG_PATH
unset REMOTE_PATH
unset REMOTE_LINUX_PATH
unset LOCAL_RC
unset REMOTE_RC
unset CUSTOM_RC
unset LOCAL_SHARED
unset REMOTE_SHARED
unset LOCAL_SETUP
unset REMOTE_SETUP
unset LOCAL_THEME
unset REMOTE_THEME

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

# Version 1.0.0
# - Initial release

# Version 1.1.0
# - Add possibility for local rc script called ".rc_local"
#   (Changes in that file are persistent)

# Version 1.2.0
# - Add output of runtime

# Version 1.3.0
# - Fix bug: this file gets sometimes overwritten by the ".rc_shared" file

# Version 1.3.1
# - Fix bug: handling of empty CHECKSUM_BEFORE after installtion

# Version 1.4.0 (03.07.2023)
# - Add timeout for wget call
