#!/bin/bash

set -o pipefail

start_time=$(date +"%s.%N")

assert() {
    if [ $# -gt 0 ]; then stdout_log "ASSERT: ${1}"; fi
    if [[ -f ${log_file} ]]; then
        echo -e "\n\n"
	echo ""
	echo "Installation failed, Here are the last 10 lines from the log"
	echo "The full installation log is available at ${log_file}"
	echo "If more information is needed re-run the install with --debug"
	echo "$(tail ${log_file})"
    else
	echo "Installation failed prior to log file being created"
	echo "Try re-running with --debug"
	echo "Installation instructions: https://docs.platform9.com/kubernetes/PMK-CLI/#installation"
    fi
    exit 1
}

debugging() {
    # This function handles formatting all debugging text.
    # debugging is always sent to the logfile.
    # If no debug flag, this function will silently log debugging messages.
    # If debug flag is present then debug output will be formatted then echo'd to stdout and sent to logfile.
    # If debug flag is present messages sent to stdout_log will be forwarded to debugging for consistancy.

    # Avoid error if bc is not installed yet
    if (which bc > /dev/null 2>&1); then
	output="$(date +"%T")$(bc <<<$(date +"%s.%N")-${start_time}) :$(basename $0) : ${1}"
    else
	output="$(date +"%T"):$(basename $0) : ${1}"
    fi

    if [ -f "${log_file}" ]; then
	echo "${output}" 2>&1 >> ${log_file}
    fi
    #echo "${output}"
}

stdout_log() {
    echo "$1"
    debugging "$1"
}

initialize_basedir() {
    debugging "Initializing: ${pf9_basedir}"
    for dir in ${pf9_state_dirs}; do
	debugging "Ensuring ${dir} exists"
        if ! mkdir -p "${dir}" > /dev/null 2>&1; then assert "Failed to create directory: ${dir}"; fi
    done
    debugging "Ensuring ${log_file} exists"
    if ! mkdir -p "${dir}" > /dev/null 2>&1; then assert "Failed to create directory: ${dir}"; fi
    if ! touch "${log_file}" > /dev/null 2>&1; then assert "failed to create log file: ${log_file}"; fi
}

refresh_symlink() {
    # Create symlink in /usr/bin
    if [ -L /usr/bin/pf9ctl ]; then
	if ! (sudo rm /usr/bin/pf9ctl >> ${log_file} 2>&1); then
		assert "Failed to remove existing symlink: ${cli_exec}"; fi
	fi
    if ! (sudo ln -s ${cli_exec} /usr/bin/pf9ctl >> ${log_file} 2>&1); then
	    assert "Failed to create Platform9 CLI symlink in /usr/bin"; fi
}

check_installation() {
    # Checking if the new "pf9ctl_new" installation is completed since the "pf9ctl" isn't removed untill the end of the installation process
    if ! (${cli_exec}_new --help >> ${log_file} 2>&1); then
	assert "Installation of Platform9 CLI Failed"; fi
    # As this is the final check and everything is alright that means we can now rename the "pf9ctl_new" to "pf9ctl"
    sudo mv ${cli_exec}_new ${cli_exec}
}

download_cli_binary() {
	echo "Note: SUDO access required to run Platform9 CLI."
    echo "      You might be prompted for your SUDO password."
	echo ""
	echo "Downloading Platform9 CLI binary..."
    # Downloading the binary under the name pf9ctl_new so that the current installed version doesnt get deleted
	cd ${pf9_bin} > /dev/null && sudo curl -s -o pf9ctl_new "${cli_path}" -o ${log_file} 2>&1 && cd - > /dev/null
	sudo chmod 555 ${cli_exec}_new
	echo ""
	echo "Platform9 CLI binary downloaded."
	echo ""
}

print_pf9_logo() {
cat << "EOF" 
 ____  _       _    __                      ___
|  _ \| | __ _| |_ / _| ___  _ __ _ __ ___ / _ \
| |_) | |/ _` | __| |_ / _ \| '__| '_ ` _ \ (_) |
|  __/| | (_| | |_|  _| (_) | |  | | | | | \__, |
|_|   |_|\__,_|\__|_|  \___/|_|  |_| |_| |_| /_/

EOF
}

checkOS_version() {
    cat /etc/*release &>/dev/null
    if [ $? != 0 ];
    then
        echo "This OS is not supported. Check documentation for supported versions at https://platform9.com/docs/kubernetes/kubernetes-cluster-pre-requisites"
        exit
    fi
}

checkCPU_Architecture() {
    requiredArch="x86_64" 
    cpuArchitecture=$(uname -m)
    if [ $cpuArchitecture != $requiredArch ];
    then
        echo "This CPU Architecture is not supported."
        exit
    fi
}

##main

pf9_basedir=$(dirname ~/pf9/.)
log_file=${pf9_basedir}/log/cli_install.log
pf9_bin=${pf9_basedir}/bin
pf9_state_dirs="${pf9_bin} ${pf9_basedir}/db ${pf9_basedir}/log"
cli_exec=${pf9_bin}/pf9ctl
cli_path="https://pmkft-assets.s3-us-west-1.amazonaws.com/pf9ctl"

checkCPU_Architecture
checkOS_version
print_pf9_logo
initialize_basedir
download_cli_binary
echo "Installing Platform9 CLI..."
echo ""
refresh_symlink
check_installation

debugging "Platform9 CLI installation completed successfully."
echo "Platform9 CLI installation completed successfully !"
echo ""
echo "To start building a Kubernetes cluster execute:"
echo "        pf9ctl help"
echo ""
#eval "${cli_exec}" help
