#!/bin/sh
# Generated by scripts/viperlink/render.py. Reviewable, per-user source installer.
set -eu
umask 077
VERSION='0.4.0'
BASE='https://www.orbitusrobotics.com/v/packages/0.4.0'
component=cloud
prefix=''
source_dir=''
dry_run=0
fail() { printf 'ViperLink: %s\n' "$*" >&2; exit 1; }
while [ "$#" -gt 0 ]; do
    case "$1" in
        --component|--prefix|--source-dir)
            [ "$#" -ge 2 ] || fail "Missing value for $1"
            case "$1" in --component) component=$2;; --prefix) prefix=$2;; --source-dir) source_dir=$2;; esac
            shift 2;;
        --dry-run) dry_run=1; shift;;
        --help) printf '%s\n' 'sh install.sh [--component cloud|status|lan] [--prefix ABSOLUTE_DIRECTORY] [--dry-run] [--source-dir LOCAL_PACKAGE_DIRECTORY]' 'Installs source only. Private enrollment and starting the selected tool are separate steps.'; exit 0;;
        *) fail "Unknown option: $1";;
    esac
done
case "$component" in cloud|status|lan);; *) fail 'Choose cloud, status or lan';; esac
system=$(uname -s)
case "$system" in
    Darwin) default_prefix="$HOME/Library/Application Support/Orbitus/ViperLink";;
    Linux) default_prefix="$HOME/.local/share/Orbitus/ViperLink";;
    *) fail 'Use install.ps1 on Windows. This installer supports macOS and Linux.';;
esac
[ -n "$prefix" ] || prefix=$default_prefix
case "$prefix" in /*);; *) fail 'The install prefix must be absolute';; esac
case "$prefix/" in /|//|*/../*|*/./*) fail 'Choose a dedicated, normalized ViperLink directory';; esac
destination="$prefix/$component/$VERSION"
files() {
    case "$component" in
        cloud) cat <<'FILES'
a0aa910e4386ecaf4f503ed996b7af626f117a49bd0c8d9d0de45f49cf6cd4a4 cloudkit.mjs
f2dc880dd4e2ce8f963ce011d3c6dcc43f95052bb62de4626fc6f45f8af3457b report-direct.mjs
810b63462f594affd0a75a417acb5f09eefc2c9490ee6c7fa6e237e6640c185c queue-format.mjs
cd15971a84d8ffe42ee3c5a4e545873f4d9277275c8b79439037b32e43765325 queue-cloud.mjs
18bef831319e44b596b56c0391e120c160d38e1bdcfa5a2d50434f8689aec116 queue-agent.mjs
0e5fdd29fbf2e6983cd85b6cb9f3afe829c3823d42bdf97f425c5594ed6ad438 enrollment-format.mjs
a94342acfe08811c4fed33f2e2dc93045dd4162e183cdb6a27f1c24dc256a24a auto-enroll.mjs
762f8e6d761f8d3cef2788d896f29cc0db0c9524906b6348a5e75bcf1946c2aa START.txt
FILES
            ;;
        status) cat <<'FILES'
a0aa910e4386ecaf4f503ed996b7af626f117a49bd0c8d9d0de45f49cf6cd4a4 cloudkit.mjs
f2dc880dd4e2ce8f963ce011d3c6dcc43f95052bb62de4626fc6f45f8af3457b report-direct.mjs
0e5fdd29fbf2e6983cd85b6cb9f3afe829c3823d42bdf97f425c5594ed6ad438 enrollment-format.mjs
810b63462f594affd0a75a417acb5f09eefc2c9490ee6c7fa6e237e6640c185c queue-format.mjs
c9a4cb0412aabab3c239787c49c48daca2662141729ea7ec62a4de4af8643c99 START.txt
FILES
            ;;
        lan) cat <<'FILES'
391e03b24c643c88e50f507a9745bf6fa4375a7828b803875fb29b4dbd377817 viperlink.py
aa6500eb90cee472c74d2a05ab83a0a05f87c3fe7c137efbef5c3bc60aae79d8 START.txt
FILES
            ;;
    esac
}
printf 'ViperLink %s source pilot\nComponent: %s\nInstall to: %s\n' "$VERSION" "$component" "$destination"
if [ "$component" = lan ]; then
    printf '%s\n' 'Requires Python 3.10+ with OpenSSL TLS 1.3. LAN controller only; remote host provisioned separately.'
else
    printf '%s\n' 'Requires an existing Node.js 22+ runtime. Fleet credentials provisioned separately.'
fi
[ "$dry_run" -eq 0 ] || { files; exit 0; }
if [ "$component" = lan ]; then
    command -v python3 >/dev/null 2>&1 || fail 'Install Python 3.10+ from python.org or your approved package manager first.'
    python3 -c 'import sys,ssl; sys.exit(0 if sys.version_info >= (3,10) and ssl.HAS_TLSv1_3 and hasattr(ssl,"VERIFY_X509_PARTIAL_CHAIN") else 1)' || fail 'Python needs version 3.10+ and OpenSSL TLS 1.3.'
else
    command -v node >/dev/null 2>&1 || fail 'Install Node.js 22+ from nodejs.org or your approved package manager first.'
    node -e 'process.exit(Number(process.versions.node.split(".")[0]) >= 22 ? 0 : 1)' || fail 'Node.js 22+ is required.'
fi
hash_file() {
    if command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}';
    elif command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}';
    else fail 'A SHA-256 utility (shasum or sha256sum) is required.'; fi
}
# Reject symlink ancestors before creating or writing any installation directories.
ancestor=$destination
while [ "$ancestor" != / ]; do
    [ ! -L "$ancestor" ] || fail "Symlink install path: $ancestor"
    ancestor=$(dirname "$ancestor")
done
mkdir -p "$prefix/$component"
lock="$prefix/.install-lock"
mkdir "$lock" 2>/dev/null || fail 'Another installation is active, or a prior lock needs review.'
work=''
cleanup() { [ -z "$work" ] || rm -rf "$work"; rmdir "$lock"; }
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
receipt="orbitus-viperlink $VERSION $component b0ce7131a3f103b761be176d378065fc3fa62966"
verify() {
    directory=$1
    files | while read -r expected name; do
        [ ! -L "$directory/$name" ] && [ -f "$directory/$name" ] || fail "Missing or non-regular file: $name"
        [ "$(hash_file "$directory/$name")" = "$expected" ] || fail "SHA-256 mismatch: $name"
    done
}
if [ -e "$destination" ]; then
    [ -f "$destination/installation.txt" ] && [ ! -L "$destination/installation.txt" ] || fail 'Existing directory is unmanaged; it was preserved.'
    [ "$(cat "$destination/installation.txt")" = "$receipt" ] || fail 'Existing installation receipt differs; it was preserved.'
    verify "$destination"
    printf 'ViperLink %s is already installed and verified.\n' "$component"
    exit 0
fi
work=$(mktemp -d "$prefix/$component/.install.XXXXXXXX")
mkdir "$work/payload"
files | while read -r expected name; do
    if [ -n "$source_dir" ]; then
        [ ! -L "$source_dir/$name" ] && [ -f "$source_dir/$name" ] || fail "Invalid local source: $name"
        cp "$source_dir/$name" "$work/payload/$name"
    else
        curl -fsSL --proto '=https' --proto-redir '=https' --connect-timeout 15 --max-time 120 "$BASE/$component/$name" -o "$work/payload/$name"
    fi
done
verify "$work/payload"
printf '%s\n' "$receipt" > "$work/payload/installation.txt"
mv "$work/payload" "$destination"
printf '\nInstalled ViperLink %s. Read: %s/START.txt\n' "$component" "$destination"
printf '%s\n' 'Installation is complete. Enrollment and execution have not been started.'
