#!/bin/bash

## <script src="/dist/scripts/cli-bash.js"></script>
## <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/themes/prism-okaidia.min.css" rel="stylesheet" />
## <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-core.min.js" data-manual></script>
## <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-bash.min.js"></script>
## <style>body {color: #272822; background-color: #272822; font-size: 0.8em;} </style>
# Love open-source, dev-tooling and passionate about code as much as we do?
# ---
# We're always looking for awesome hackers like you to join our 100% remote team!
# Check and see if you find any relevant position @ https://{{ spec.title | lower }}.io/company/careers 👩‍💻 😎
# (and let us know you found this message...)

# This script contains hidden JS code to allow better readability and syntax highlighting
# You can use "View source" of this page to see the full script.

# {{ spec.title | caseUcfirst }} CLI location
{{ spec.title | upper }}_INSTALL_DIR="/usr/local/bin"

# {{ spec.title | caseUcfirst }} CLI Executable name 
{{ spec.title | upper }}_EXECUTABLE_NAME={{ language.params.executableName }}

# {{ spec.title | caseUcfirst }} executable file path 
{{ spec.title | upper }}_EXECUTABLE_FILEPATH="${{ spec.title | upper }}_INSTALL_DIR/${{ spec.title | upper }}_EXECUTABLE_NAME"

# {{ spec.title | caseUcfirst }} CLI temp name 
{{ spec.title | upper }}_TEMP_NAME=temp-$(date +%s)

# {{ spec.title | caseUcfirst }} CLI image name
GITHUB_REPOSITORY_NAME={{ sdk.gitUserName }}/{{ sdk.gitRepoName | caseDash }}

# sudo is required to copy executable to {{ spec.title | upper }}_INSTALL_DIR for linux
USE_SUDO="false"
OS=""
ARCH=""

# Add some color to life
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color


greeting() {
    echo -e "${RED}"
    cat << "EOF"
{{ language.params.logoUnescaped | raw }}
EOF
    echo -e "${NC}\n"
    echo "🔥 Welcome to the {{ spec.title | caseUcfirst }} CLI install shield 🛡"
}

getSystemInfo() {
    echo "[1/4] Getting System Info ..."
    
    ARCH=$(uname -m)
    case $ARCH in
        i386|i686) ARCH="x64" ;;
        x86_64) ARCH="x64";;
        armv6*) ARCH="arm64" ;;
        armv7*) ARCH="arm64" ;;
        aarch64*) ARCH="arm64" ;;
    esac

    OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')

    # Need root access if its a linux system
    if [ "$OS" == "linux" ] && [ "${{ spec.title | upper }}_INSTALL_DIR" == "/usr/local/bin" ]; then
        USE_SUDO="true"
    fi
    
    # Need root access if its Apple Silicon
    if [ "$OS" == "darwin" ] && [[ "$(uname -a)" = *ARM64* ]]; then
        USE_SUDO="true"
    fi

    printf "${GREEN}\nOS : $OS \nARCH : $ARCH \nREQUIRES ROOT : $USE_SUDO\n\n${NC}"
}

runAsRoot() {
    local CMD="$*"
    if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
        CMD="sudo $CMD"
    fi
    $CMD
}

printSuccess() {
    printf "${GREEN}✅ Done ... ${NC}\n\n"
}

downloadBinary() {
    echo "[2/4] Downloading executable for $OS ($ARCH) ..."

    GITHUB_LATEST_VERSION="{{ sdk.version }}"
    GITHUB_FILE="{{ language.params.npmPackage }}-${OS}-${ARCH}"
    GITHUB_URL="https://github.com/$GITHUB_REPOSITORY_NAME/releases/download/$GITHUB_LATEST_VERSION/$GITHUB_FILE"

    printf "${GREEN}🚦 Downloading {{ spec.title | caseUcfirst }} CLI $GITHUB_LATEST_VERSION ... ${NC}\n"
    res=$(curl -s $GITHUB_URL)
    if [[ "$res" == *"Not Found"* ]]; then
        printf "${RED}❌ Couldn't find executable for $OS ($ARCH). Please contact the {{ spec.title | caseUcfirst }} team ${NC} \n"
        exit 1
    fi
    curl -L -o ${{ spec.title | upper }}_TEMP_NAME $GITHUB_URL
    printSuccess
}

install() {
    echo "[3/4] Installing ..."

    printf "${GREEN}🚧 Setting Permissions ${NC}\n"
    chmod +x ${{ spec.title | upper }}_TEMP_NAME
    if [ $? -ne 0 ]; then
        printf "${RED}❌ Failed to set permissions ... ${NC}\n"
        exit 1
    fi
    printSuccess

    printf "${GREEN}📝 Copying temporary file to ${{ spec.title | upper }}_EXECUTABLE_FILEPATH ... ${NC}\n"
    runAsRoot cp ${{ spec.title | upper }}_TEMP_NAME ${{ spec.title | upper }}_EXECUTABLE_FILEPATH
    if [ $? -ne 0 ]; then
        printf "${RED}❌ Failed to copy temporary file to ${{ spec.title | upper }}_EXECUTABLE_FILEPATH ... ${NC}\n"
        exit 1
    fi
    printSuccess
}

cleanup() {
    printf "${GREEN}🧹 Cleaning up mess ... ${NC}\n"
    rm ${{ spec.title | upper }}_TEMP_NAME 
    if [ $? -ne 0 ]; then
        printf "${RED}❌ Failed to remove temporary file ... ${NC}\n"
        exit 1
    fi
    printSuccess

}

installCompleted() {
    echo "[4/4] Wrapping up installation ... "
    cleanup
    echo "🚀 To get started with {{ spec.title | caseUcfirst }} CLI, please visit {{ sdk.url }}/docs/command-line"
    echo "As first step, you can login to your {{ spec.title | caseUcfirst }} account using 'appwrite login'"
}

# Installation Starts here 
greeting
getSystemInfo
downloadBinary
install
installCompleted