#!/usr/bin/env sh
# Standalone native Hokio. Never modifies hokio-agent or manages services.
set -eu

fail() { printf '%s\n' "$*" >&2; exit 1; }
legacy='https://hokio.dev/static/downloads/install-legacy.sh'
case "${1:-}" in
    -h|--help)
        printf '%s\n' 'Install native Hokio (Webhook + URL) for the current user.' \
            'Requires Linux (x64, ARM64, i686/SSE2 or ARMv7 hard-float), curl, sha256sum and trusted CA certificates.' \
            'Does not migrate devices, change hokio-agent, or start a service.' \
            "Legacy installer and its options: $legacy"
        exit 0 ;;
esac
[ "$#" = 0 ] || fail "This installer takes no options. For legacy installation options use $legacy"
if [ -n "${HOKIO_INSTALL_DIR:-}${HOKIO_DOWNLOAD_BASE_URL:-}${HOKIO_LIBC:-}" ]; then
    fail "Legacy installer settings detected. Use $legacy to keep that installation workflow."
fi
[ "$(uname -s)" = Linux ] || fail 'This installer requires Linux.'
case "$(uname -m)" in
    x86_64|amd64) architecture=x64 ;;
    aarch64|arm64) architecture=arm64 ;;
    i686) architecture=x86 ;;
    armv7|armv7l) architecture=armv7 ;;
    *) fail "Native Hokio supports Linux x64, ARM64, i686/SSE2 and ARMv7 hard-float. Nothing was changed. Legacy installer: $legacy" ;;
esac
asset="hokio-linux-$architecture-static"
for command in curl sha256sum mktemp awk; do
    command -v "$command" >/dev/null 2>&1 || fail "Required command missing: $command"
done
[ -n "${HOME:-}" ] || fail 'HOME must be set.'
case "$HOME" in /*) ;; *) fail 'HOME must be an absolute path.' ;; esac
destination="$HOME/.local/bin/hokio"
for directory in "$HOME/.local" "$HOME/.local/bin"; do
    [ ! -L "$directory" ] || fail "Refusing a symbolic-link installation directory: $directory"
done
[ ! -L "$destination" ] || fail 'The destination is a symbolic link. Nothing was changed.'
existing=$(command -v hokio 2>/dev/null || true)
if [ -n "$existing" ] && [ "$existing" != "$destination" ]; then
    fail "Another hokio command exists at $existing. Nothing was changed; resolve the command conflict first."
fi

release='https://github.com/m-nader90/hokio-releases/releases/download/native-v0.2.0-alpha.4'
temporary=$(mktemp -d)
staging=''
cleanup() {
    [ -z "$staging" ] || rm -f -- "$staging"
    rm -rf -- "$temporary"
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
if [ -e "$destination" ]; then
    [ -f "$destination" ] && [ -x "$destination" ] || fail 'The destination is not a native executable. Nothing was changed.'
    case "$("$destination" --home "$temporary/existing-probe" --version 2>/dev/null || true)" in
        'hokio 0.2.'*) ;;
        *) fail 'An unrelated hokio command exists in ~/.local/bin. Nothing was changed.' ;;
    esac
fi
cd "$temporary"
curl --proto '=https' --tlsv1.2 --fail --silent --show-error --location --connect-timeout 20 --max-time 180 "$release/$asset" -o hokio-native
curl --proto '=https' --tlsv1.2 --fail --silent --show-error --location --connect-timeout 20 --max-time 180 "$release/$asset.sha256" -o checksum.txt
# Limit checksum input to the expected artifact, never a path supplied by a manifest.
digest=$(awk 'NR == 1 {print $1}' checksum.txt)
[ "${#digest}" = 64 ] || fail 'Invalid release checksum.'
case "$digest" in *[!0-9a-fA-F]*) fail 'Invalid release checksum.' ;; esac
printf '%s  hokio-native\n' "$digest" | sha256sum -c -
chmod 755 hokio-native
version=$(./hokio-native --home "$temporary/probe" --version) || fail 'The portable Linux binary could not run on this device. Nothing was installed. Check its architecture and system execution policy.'
[ "$version" = 'hokio 0.2.0-alpha.4' ] || fail 'Unexpected native release version. Nothing was installed.'
mkdir -p "$HOME/.local/bin"
staging=$(mktemp "$HOME/.local/bin/.hokio-native.XXXXXX")
cat hokio-native > "$staging"
chmod 755 "$staging"
mv -f "$staging" "$destination"
staging=''
printf 'Installed %s (%s static Linux) at %s\n' "$version" "$architecture" "$destination"
printf '%s\n' 'Existing hokio-agent installations, credentials and services were left unchanged.'
printf 'Next, run this command in your terminal:\n  "%s" setup\n' "$destination"
if command -v systemctl >/dev/null 2>&1; then
    printf 'After setup, with a systemd user manager available:\n  "%s" service install\n  "%s" service start\n  "%s" status\n' "$destination" "$destination" "$destination"
else
    printf 'After setup, run in the foreground or under your existing supervisor:\n  "%s" run\n' "$destination"
fi
printf '%s\n' 'Keep the distribution ca-certificates package installed for HTTPS and WSS verification.' \
    'The service commands require an available systemd user manager.' \
    'On OpenRC, runit or other init systems, supervise hokio run using that system; do not use systemd commands.' \
    'Use the full path above if ~/.local/bin is not on PATH. The installer does not edit shell profiles.'
