#!/bin/sh
# Publication template; scripts/build-licensed.py replaces every @...@ pin.
# Initial trust is this HTTPS-delivered script. The compiled verifier embeds:
# control-trust SHA256 1743cd29b07e7344c50580c62db8422ea1bbd464b6260b7ffc223fc142dd5ce1
# TUF root SHA256 cc6003456c3e738f407b3414ac91704a6ef964bf2d803dfe711b9562b1ffdde6
genious_bootstrap() (
    set -eu
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    export PATH
    umask 077
    if [ "$(uname -s)" != Linux ] || [ "$(uname -m)" != x86_64 ]; then
        printf '%s\n' 'This release requires Ubuntu 26.04 amd64.' >&2
        exit 1
    fi
    command -v curl >/dev/null
    command -v sha256sum >/dev/null
    stage=$(mktemp -d /tmp/genious-bootstrap.XXXXXXXX)
    trap 'rm -rf -- "$stage"' 0
    trap 'exit 130' INT
    trap 'exit 143' TERM HUP
    printf '%s\n' 'Downloading installer…' >&2
    progress=--silent
    if [ -t 1 ] && [ -t 2 ] && [ "${TERM:-}" != dumb ]; then
        progress=--progress-bar
    fi
    curl --fail "$progress" --show-error --proto '=https' --tlsv1.2 \
        --connect-timeout 15 --max-time 300 \
        'https://releases.geniousdeploy.com/bootstrap/d1874c9ffe741699b00765b8b907498deeaf02d3b5c4f9c223cd9da814be78da.genious-install' \
        --output "$stage/genious-install"
    if ! printf '%s  %s\n' 'd1874c9ffe741699b00765b8b907498deeaf02d3b5c4f9c223cd9da814be78da' "$stage/genious-install" | sha256sum --check --status; then
        printf '%s\n' 'Installer verification failed.' >&2
        exit 1
    fi
    chmod 0700 "$stage/genious-install"
    if [ "$(id -u)" = 0 ]; then
        "$stage/genious-install" --automatic --licensed --yes --target 'appliance/rel_20260930_stable012/bundle.tar.gz' "$@"
    else
        if ! command -v sudo >/dev/null 2>&1; then
            printf '%s\n' 'Administrator privileges are required to install Genious Deploy.' >&2
            exit 1
        fi
        # Elevate a verified executable, not a shell still consuming this script.
        # Root re-copies and verifies into its own private directory before exec;
        # checking user-writable staging only before sudo would leave a race.
        genious_elevated() {
            sudo -n /bin/sh -c '
                set -eu
                PATH=/usr/sbin:/usr/bin:/sbin:/bin
                export PATH
                umask 077
                private=$(mktemp -d /tmp/genious-root-bootstrap.XXXXXXXX)
                trap '\''rm -rf -- "$private"'\'' 0
                trap '\''exit 130'\'' INT
                trap '\''exit 143'\'' TERM HUP
                timeout 30 dd "if=$1" "of=$private/genious-install" bs=1048576 count=64 status=none
                shift
                if ! printf "%s  %s\n" "d1874c9ffe741699b00765b8b907498deeaf02d3b5c4f9c223cd9da814be78da" "$private/genious-install" | sha256sum --check --status; then
                    printf "%s\n" "Installer verification failed." >&2
                    exit 1
                fi
                chmod 0700 "$private/genious-install"
                "$private/genious-install" "$@"
            ' genious-root-bootstrap "$stage/genious-install" --automatic --licensed --yes --target 'appliance/rel_20260930_stable012/bundle.tar.gz' "$@"
        }
        if ( : </dev/tty ) 2>/dev/null; then
            if ! sudo -v </dev/tty; then
                printf '%s\n' 'Administrator privileges are required to install Genious Deploy.' >&2
                exit 1
            fi
            genious_elevated "$@" </dev/tty
        else
            if ! sudo -n -v </dev/null; then
                printf '%s\n' 'Administrator privileges are required to install Genious Deploy.' >&2
                exit 1
            fi
            genious_elevated "$@" </dev/null
        fi
    fi
)
genious_bootstrap "$@"
