#!/usr/bin/env bash

CLN_REGISTER_SERVER="https://cln.cloudlinux.com/cln/api/els/server/register"
CLN_UNREGISTER_SERVER="https://cln.cloudlinux.com/cln/api/els/server/unregister"
LICENSE=""
HOSTNAME=`hostname`
AUTH_CONF_PATH="/etc/apk/repositories.d/els-alpine-release.list"
PACKAGE_URI="https://repo.tuxcare.com/alpinelinux3.18-els/els-alpine-release-install.apk"


exec 3>&1
trap "exec 3>&-" EXIT

log() {
    printf "%s\\n" "$1" 1>&3
}

show_usage() {
    echo 'Usage: install-els-alpine-repo.sh [OPTION]...'
    echo ''
    echo '  -l, --license-key   User license key'
    echo '  -f, --force         Force re-register if ELS is already installed'
    echo '  -d, --delete        Delete ELS from server'
    echo '  -h, --help          Show this message and exit'
}

do_opts() {
    if [ $# -eq 0 ]; then
        log "$(show_usage)"
        exit 1
    fi
    while [ $# -gt 0 ]; do
        key="$1"
        case $key in
            -h|--help)
                log "$(show_usage)"
                exit 0
                ;;
            -l|--license-key)
                LICENSE="$2"
                shift
                ;;
            -f|--force)
                FORCE=true
                ;;
            -d|--delete)
                DELETE=true
                ;;
            -*|--*)
                log "Unknown option: $key"
                log "$(show_usage)"
                exit 1
                ;;
        esac
        shift
    done

    if [ -z "$LICENSE" ] && [ "$DELETE" != "true" ]; then
        log "Required argument not provided: --license-key"
        log "$(show_usage)"
        exit 1
    fi
}

els_alpine_installed() {
    log "Checking if els-alpine-release is already installed..."
    if apk info -e els-alpine-release >/dev/null 2>&1; then
        log "els-alpine-release package is already installed."
        return 0
    fi
    log "els-alpine-release package is not installed"
    return 1
}

unregister_token() {
    token=$(cat "$AUTH_CONF_PATH" | sed 's|.*alpinelinux3.18-els/\([^/]*\)/stable.*|\1|')
    response=$(curl -i -s -X POST "${CLN_UNREGISTER_SERVER}?token=${token}")
    curl_exit_code=$?

    if [ $curl_exit_code -ne 0 ]; then
        echo "ERROR: Failed to connect to unregister server (curl exited with status: $curl_exit_code)"
        return 1
    fi

    if ! echo "$response" | grep -qi '^HTTP/[0-9.].*200.*OK'; then
        if [[ -z "$response" ]]; then
            echo "elstoken wasn't found. Server is not registered"
        else
            echo "Got incorrect status from CLN: $response"
        fi
        return 1
    fi

    echo "Unregistered successfully"
    return 0
}

remove_els_alpine() {
    log "Removing els-alpine-release package... "
    if apk del els-alpine-release 1>&3; then
        log "Ok"
    else
        log "Error (Could not remove els-alpine-release package)"
        return 1
    fi
}

get_auth_token() {
    log "Request repository token for this server... "
    local data="{\"key\": \"$3\", \"host_name\": \"$2\"}"
    local ret
    ret=$(curl -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d "$data" "$1")
    if [ $? -ne 0 ]; then
        log "Error (Curl command failed)"
        return 1
    fi
    local token
    token=$(echo "$ret" | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
    if [ -n "$token" ]; then
        log "Ok"
        echo "$token"
        return 0
    fi
    log "Error (No token was returned from CLN)"
    return 1
}

install_els_alpine_release() {
    log "Installing els-alpine-release..."

    TEMP_APK=$(mktemp /tmp/els-alpine-release-XXXXXX)
    mv "$TEMP_APK" "$TEMP_APK.apk"
    TEMP_APK="$TEMP_APK.apk"

    trap 'rm -f "$TEMP_APK"' EXIT

    if ! curl -fsSL -o "$TEMP_APK" "$PACKAGE_URI"; then
        log "Error: Couldn't download els-alpine-release.apk"
        return 1
    fi

    if ! apk add "$TEMP_APK" --allow-untrusted; then
        log "Error: Couldn't install els-alpine-release"
        return 3
    fi
}

validate_supported_os() {
    # Check if the system is Alpine Linux
    if [[ -f /etc/alpine-release ]]; then
        local version
        version=$(cat /etc/alpine-release)
        log "Detected Alpine Linux system, version $version"

        if [[ $version == 3.18* ]]; then
            log "Alpine version 3.18 is supported"
            return 0
        else
            log "Unsupported Alpine version: $version (requires 3.18)"
            return 1
        fi
    fi

    log "Not an Alpine Linux system"
    return 1
}

check_superuser_privileges() {
    log "Checking for superuser privileges..."
    if [ "$(id -u)" -ne 0 ]; then
        log "Error: This script must be run with superuser privileges"
        return 1
    fi
    log "Superuser privileges confirmed"
    return 0
}

main() {
    if ! check_superuser_privileges; then
        return 14
    fi

    do_opts "$@"

    if ! validate_supported_os; then
        log "Unsupported OS detected"
        return 1
    fi

    if [ "$DELETE" = "true" ];  then
        if els_alpine_installed; then
            if ! unregister_token; then
                log "Couldn't deactivate account"
                return 12
            fi
            remove_els_alpine
        else
            log "ELS is not installed"
        fi
        return 0
    fi

    if els_alpine_installed; then
        if [ "$FORCE" = "true" ]; then
            unregister_token
            remove_els_alpine
        else
            log "This server has installed ELS repo and token"
            log "For re-registration license run script with --force"
            return 2
        fi
    fi

    log "Registering in CLN..."
    local token
    token=$(get_auth_token "$CLN_REGISTER_SERVER" "$HOSTNAME" "$LICENSE")
    if [ $? -ne 0 ]; then
        return 3
    fi

    log "Applying repository token for this server..."
    if ! install_els_alpine_release; then
        return 9
    fi

    if ! sed -i "s/\$TOKEN/$token/g" "$AUTH_CONF_PATH"; then
        log "Error (Could not write to $AUTH_CONF_PATH)"
        return 10
    fi

    log "ELS for alpine installed successfully"
}

main "$@"
