#!/usr/bin/env sh

#
#
#            ••••••••••
#       ••••••••••••••••••••
#     ••••••••••••••••••••••••
#   ••••••••••••••••••••••••••••
#  ••••••••••••••••••••••••••••••
# ••••••••••••••••••••••••••••••••
# ••••••••••••••••••••••••••••••••
#  ••••••••••••••••   •••••••••••
#   •••••••••••••••••   ••••••••
#     •••••••••••••••••   ••••
#       ••••••••••••••••••••
#            ••••••••••      *
#
#          ┏┓ ┏┓ ┏┓ ┳ ┳┓┏┳┓
#          ┃┃ ┃┃ ┃┃ ┃ ┃┃ ┃
#          ┗┣ ┣┛ ┗┛ ┻ ┛┗ ┻
#
# This is an installation script for Qtap (https://qpoint.io). Please run:
#
#     curl -s https://get.qpoint.io/install | sudo sh
#
#

set -eu

# configurable variables
VERSION=${VERSION:-latest}

# script variables
OS=
ARCH=

RED=
GREEN=
PURPLE=
BOLD=
DIM=
RESET=
RESET_BOLD=
# terminal output - use colors
if [ -t 1 ]; then
    RED='[0;31m'
    GREEN='[0;32m'
    PURPLE='[0;35m'
    BOLD='[1m'
    DIM='[2m'
    RESET_BOLD='[22m'
    RESET='[0m'
fi

bold() {
    printf "${BOLD}%s${RESET_BOLD}" "$*"
}

action() {
    printf "${PURPLE}→ %s${RESET}\n" "$*"
}

error() {
    printf >&2 "${RED}✗ %s${RESET}\n" "$*"
    exit 1
}

success() {
    printf "${GREEN}✓ %s${RESET}\n" "$*"
}

download_url() {
    printf "https://downloads.qpoint.io/qpoint/qtap-%s-%s-%s.tgz" "$VERSION" "$OS" "$ARCH"
}

setup() {
    case "$(uname -sr)" in
    Linux*Microsoft*)
        error "windows/wsl is not supported"
        ;;

    Linux*)
        OS="linux"
        ;;

    *)
        error "unsupported operating system $(bold "$(uname -sr)")"
        ;;
    esac

    case "$(uname -m)" in
    x86_64)
        ARCH="amd64"
        ;;

    aarch64 | arm64)
        ARCH="arm64"
        ;;

    *)
        error "unsupported architecture $(bold "$(uname -m)")"
        ;;
    esac

    if [ "$(id -u)" != "0" ]; then
        error "please run this script as root

    curl -s https://get.qpoint.io/install | sudo sh
    
alternatively, follow the manual installation instructions:

    https://docs.qpoint.io/qtap/installation/linux-binary#manual-installation

"
    fi
}

banner() {
    echo ""
    echo "${PURPLE}           ••••••••••            ${RESET}"
    echo "${PURPLE}      ••••••••••••••••••••       ${RESET}"
    echo "${PURPLE}    ••••••••••••••••••••••••     ${RESET}"
    echo "${PURPLE}  ••••••••••••••••••••••••••••   ${RESET}"
    echo "${PURPLE} ••••••••••••••••••••••••••••••  ${RESET}"
    echo "${PURPLE}•••••••••••••••••••••••••••••••• ${RESET}"
    echo "${PURPLE}•••••••••••••••••••••••••••••••• ${RESET}"
    echo "${PURPLE} ••••••••••••••••   •••••••••••  ${RESET}"
    echo "${PURPLE}  •••••••••••••••••   ••••••••   ${RESET}"
    echo "${PURPLE}    •••••••••••••••••   ••••     ${RESET}"
    echo "${PURPLE}      ••••••••••••••••••••       ${RESET}"
    echo "${PURPLE}           ••••••••••      ${BOLD}*${RESET}"
    echo ""
    echo "${PURPLE}        ┏┓ ┏┓ ┏┓ ┳ ┳┓┏┳┓         ${RESET}"
    echo "${PURPLE}        ┃┃ ┃┃ ┃┃ ┃ ┃┃ ┃          ${RESET}"
    echo "${PURPLE}        ┗┣ ┣┛ ┗┛ ┻ ┛┗ ┻          ${RESET}"
    echo ""
}

install() {
    action "installing Qtap [$(bold "${VERSION}")]"

    if ! dir=$(mktemp -d); then
        error "failed to create temporary directory"
    fi

    cd "$dir"
    # shellcheck disable=SC2064 # we want $dir to expand now
    trap "rm -rf '$dir'" EXIT INT

    curl -fsSL "$(download_url)" >qtap.tgz
    tar -xzf qtap.tgz
    mv qtap-* /usr/local/bin/qtap
    chmod +x /usr/local/bin/qtap

    success "successfully installed $(bold "$(/usr/local/bin/qtap --version)") at $(bold "/usr/local/bin/qtap")"
    printf '\n'
}

usage_instructions() {
    action "usage instructions"
    echo "${DIM}1.${RESET} log in at $(bold "https://app.qpoint.io/")"
    echo "${DIM}2.${RESET} navigate to $(bold "Settings -> Installation") and copy the auth token"
    echo "${DIM}3.${RESET} verify that the installation is successful by running:"
    echo ""
    echo "    sudo qtap --registration-token $(bold "<auth-token>")"
    echo ""
    echo "${PURPLE}ℹ${RESET} to run qtap as a service, follow the instructions at"
    echo ""
    echo "    $(bold "https://docs.qpoint.io/installation/linux-binary#running-qtap-as-a-service")"
    echo ""
}

banner
setup
install
usage_instructions
