#!/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)
                if [[ -z "$2" || "$2" == -* ]]; then
                    log "Error: --license-key requires an argument"
                    log "$(show_usage)"
                    exit 1
                fi
                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() {
    local token="$1"

    if [ -z "$token" ]; then
        token=$(cat "$AUTH_CONF_PATH" | sed 's|.*alpinelinux3.18-els/\([^/]*\)/stable.*|\1|')
    fi

    # Status from -w, not from `curl -i` headers: behind an HTTPS proxy those
    # start with the proxy's "HTTP/1.1 200 Connection established"
    response=$(curl -s -w '\n%{http_code}' -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

    local http_code="${response##*$'\n'}"
    local body="${response%$'\n'*}"
    # CLN answers 401 "Token not registered as an ELS server" for a token it
    # no longer knows (released in the portal, expired licence): no seat is held
    if [ "$http_code" = "401" ] && [[ "$body" == *"not registered"* ]]; then
        echo "Token is not registered in CLN any more; nothing to release"
        return 0
    fi
    if [ "$http_code" != "200" ]; then
        echo "Got incorrect status from CLN: HTTP $http_code $body"
        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
}

delete_els_alpine() {
    if els_alpine_installed; then
        if [ "$FORCE" = "true" ]; then
            # Force mode: try to unregister but continue even if it fails
            unregister_token || log "Warning: Failed to unregister, but continuing with force delete"
        else
            if ! unregister_token; then
                log "Couldn't deactivate account"
                return 12
            fi
        fi
        # 16: the package could not be removed
        remove_els_alpine || return 16
    else
        log "ELS is not installed"
    fi
    return 0
}

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() {
    do_opts "$@"

    if ! check_superuser_privileges; then
        return 14
    fi

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

    if [ "$DELETE" = "true" ];  then
        delete_els_alpine
        return $?
    fi

    # --force over a working install registers the new key first and tears
    # the old install down only once CLN has accepted it (make before break)
    local reinstall=""
    if els_alpine_installed; then
        if [ "$FORCE" = "true" ]; then
            reinstall=true
        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
        if [ -n "$reinstall" ]; then
            log "Nothing was changed. If the licence has no free seat, run with --delete first, then install again."
        fi
        return 3
    fi

    if [ -n "$reinstall" ]; then
        # The list still carries the old token: read it before the removal
        # drops it, and release it only once the removal succeeded, so a failed
        # removal leaves a working install with a live token. Only a
        # substituted repo line carries a token.
        local old_token
        old_token=$(sed -n 's|.*alpinelinux3.18-els/\([^/]*\)/stable.*|\1|p' "$AUTH_CONF_PATH" 2>/dev/null | head -1)
        if ! remove_els_alpine; then
            # CLN may have handed back the token the remaining install still uses
            if [ "$token" != "$old_token" ]; then
                unregister_token "$token" || log "Warning: Could not release the just-issued registration"
            fi
            # 16: the package could not be removed
            return 16
        fi
        # CLN may hand this host the same token again; releasing it then
        # would void the new registration
        if [[ "$old_token" == *SERVER* && "$old_token" != "$token" ]]; then
            unregister_token "$old_token" || log "Warning: Failed to unregister, but continuing with force reinstall"
        fi
    fi

    log "Applying repository token for this server..."
    # The token exists only in this shell until it is written to the list, so
    # release it on any failure before that
    if ! install_els_alpine_release; then
        unregister_token "$token" || log "Warning: Could not release the just-issued registration"
        return 9
    fi

    if ! sed -i "s/\$TOKEN/$token/g" "$AUTH_CONF_PATH"; then
        log "Error (Could not write to $AUTH_CONF_PATH)"
        unregister_token "$token" || log "Warning: Could not release the just-issued registration"
        return 10
    fi

    log "ELS for alpine installed successfully"
}

main "$@"
