#!/bin/sh
# Opt-in integration proof for the public GitHub release acquisition path.
# [int->REQ-DIST-ADAPTER-RELEASE]
# [int->REQ-DIST-RELEASE-EVIDENCE]
set -u
REPO="${OMP_SPT_RELEASE_REPO:-BigscreenVR/omp-spt}"

if [ "${OMP_SPT_ACCEPTANCE:-0}" != "1" ]; then
  echo "SKIP: set OMP_SPT_ACCEPTANCE=1 to run (mutates registry + downloads from GitHub)"
  exit 0
fi

fatal() { echo "FAIL $1" >&2; exit 1; }
[ -n "${OMP_SPT_RELEASE_TAG:-}" ] \
  || fatal "OMP_SPT_RELEASE_TAG is required when OMP_SPT_ACCEPTANCE=1"
[ -n "${OMP_SPT_EXPECTED_VERSION:-}" ] \
  || fatal "OMP_SPT_EXPECTED_VERSION is required when OMP_SPT_ACCEPTANCE=1"
[ -n "${OMP_SPT_ACCEPTANCE_TARGET:-}" ] \
  || fatal "OMP_SPT_ACCEPTANCE_TARGET is required when OMP_SPT_ACCEPTANCE=1"
[ -n "${OMP_SPT_EXPECTED_ADAPTER_SHA256:-}" ] \
  || fatal "OMP_SPT_EXPECTED_ADAPTER_SHA256 is required when OMP_SPT_ACCEPTANCE=1"
[ -n "${OMP_SPT_EXPECTED_HELPER_SHA256:-}" ] \
  || fatal "OMP_SPT_EXPECTED_HELPER_SHA256 is required when OMP_SPT_ACCEPTANCE=1"
TARGET=$OMP_SPT_ACCEPTANCE_TARGET
# The target is an evidence key supplied by the operator on that native host. Acquisition cannot
# infer GNU versus musl from `uname`; the runbook requires native acceptance on the declared target.
# [int->REQ-DIST-LINUX-MUSL]
case "$TARGET" in
  x86_64-pc-windows-msvc) HELPER_BASENAME=omp-spt.exe ;;
  x86_64-unknown-linux-gnu|x86_64-unknown-linux-musl) HELPER_BASENAME=omp-spt ;;
  *) fatal "OMP_SPT_ACCEPTANCE_TARGET is not a release target: $TARGET" ;;
esac
TAG=$OMP_SPT_RELEASE_TAG
EXPECTED_VERSION=$OMP_SPT_EXPECTED_VERSION
EXPECTED_ADAPTER_SHA256=$OMP_SPT_EXPECTED_ADAPTER_SHA256
EXPECTED_HELPER_SHA256=$OMP_SPT_EXPECTED_HELPER_SHA256
case "$EXPECTED_ADAPTER_SHA256" in
  *[!0-9a-f]*)
    fatal "OMP_SPT_EXPECTED_ADAPTER_SHA256 must be 64 lowercase hexadecimal characters"
    ;;
esac
[ "${#EXPECTED_ADAPTER_SHA256}" -eq 64 ] \
  || fatal "OMP_SPT_EXPECTED_ADAPTER_SHA256 must be 64 lowercase hexadecimal characters"
case "$EXPECTED_HELPER_SHA256" in
  *[!0-9a-f]*)
    fatal "OMP_SPT_EXPECTED_HELPER_SHA256 must be 64 lowercase hexadecimal characters"
    ;;
esac
[ "${#EXPECTED_HELPER_SHA256}" -eq 64 ] \
  || fatal "OMP_SPT_EXPECTED_HELPER_SHA256 must be 64 lowercase hexadecimal characters"
[ "$TAG" = "v$EXPECTED_VERSION" ] \
  || fatal "OMP_SPT_RELEASE_TAG must equal v$EXPECTED_VERSION (got $TAG)"

command -v spt >/dev/null 2>&1 || fatal "spt is required on PATH"
command -v gh >/dev/null 2>&1 || fatal "gh is required on PATH"
gh auth status >/dev/null 2>&1 || fatal "gh is not authenticated"
command -v sha256sum >/dev/null 2>&1 || fatal "sha256sum is required on PATH"
command -v mktemp >/dev/null 2>&1 || fatal "mktemp is required on PATH"
command -v tar >/dev/null 2>&1 || fatal "tar is required on PATH"

fail=0
ok()  { echo "ok   $1"; }
bad() { echo "FAIL $1"; fail=1; }

STAGE=$(mktemp -d "${TMPDIR:-/tmp}/omp-spt-release-acquire.XXXXXX") \
  || fatal "could not create release acquisition staging directory"
trap 'rm -rf "$STAGE"' EXIT INT TERM
HOSTED_ADAPTER="$STAGE/adapter.spt"
if ! gh release download "$TAG" --repo "$REPO" --pattern adapter.spt --output "$HOSTED_ADAPTER"; then
  fatal "could not download hosted adapter.spt from $REPO@$TAG"
fi
digest_output=$(sha256sum "$HOSTED_ADAPTER") \
  || fatal "could not hash hosted adapter.spt from $REPO@$TAG"
HOSTED_ADAPTER_SHA256=${digest_output%% *}
[ "$HOSTED_ADAPTER_SHA256" = "$EXPECTED_ADAPTER_SHA256" ] \
  || fatal "hosted adapter.spt SHA-256 '$HOSTED_ADAPTER_SHA256' does not equal expected '$EXPECTED_ADAPTER_SHA256'"
ok "hosted adapter.spt SHA-256 is exactly $EXPECTED_ADAPTER_SHA256"
HELPER_MEMBER="$TARGET/$HELPER_BASENAME"
HOSTED_HELPER="$STAGE/selected-helper"
if ! tar -xOzf "$HOSTED_ADAPTER" "$HELPER_MEMBER" > "$HOSTED_HELPER"; then
  fatal "hosted adapter.spt does not contain selected helper $HELPER_MEMBER"
fi
helper_digest_output=$(sha256sum "$HOSTED_HELPER") \
  || fatal "could not hash hosted selected helper $HELPER_MEMBER"
HOSTED_HELPER_SHA256=${helper_digest_output%% *}
[ "$HOSTED_HELPER_SHA256" = "$EXPECTED_HELPER_SHA256" ] \
  || fatal "hosted $HELPER_MEMBER selected helper SHA-256 '$HOSTED_HELPER_SHA256' does not equal expected '$EXPECTED_HELPER_SHA256'"
ok "hosted $HELPER_MEMBER selected helper SHA-256 is exactly $EXPECTED_HELPER_SHA256"

ok "acceptance evidence target is exactly $TARGET"
trap 'rm -rf "$STAGE"; spt adapter remove omp-spt >/dev/null 2>&1 || true' EXIT INT TERM
spt adapter remove omp-spt >/dev/null 2>&1 || true

if out=$(spt adapter add --release "$REPO" --tag "$TAG" 2>&1); then
  case "$out" in
    *registered*|*active*|*fetched*) ok "adapter add --release $REPO@$TAG succeeded" ;;
    *) bad "adapter add returned success without acquisition evidence: $out" ;;
  esac
else
  bad "adapter add --release failed: $out"
fi

if list=$(spt adapter list 2>&1); then
  line=$(printf '%s\n' "$list" | grep '^omp-spt:' || true)
else
  line=
  bad "adapter list failed: $list"
fi
case "$line" in *active*) ok "omp-spt active after release acquisition" ;; *) bad "omp-spt not active: $line" ;; esac
case "$line" in *_github*) ok "source is the release install directory" ;; *) bad "release install source missing: $line" ;; esac
safe=$(printf '%s' "$REPO" | tr '/' '-')
case "$line" in *"$safe"*) ok "source carries repository identity $safe" ;; *) bad "repository identity missing: $line" ;; esac
case "$line" in
  *"(from "*")")
    INSTALL_DIR=${line#*"(from "}
    INSTALL_DIR=${INSTALL_DIR%")"}
    ;;
  *)
    INSTALL_DIR=
    bad "could not resolve parenthesized release install directory from: $line"
    ;;
esac
if [ -n "$INSTALL_DIR" ]; then
  INSTALLED_HELPER="$INSTALL_DIR/$HELPER_BASENAME"
  if installed_digest_output=$(sha256sum "$INSTALLED_HELPER" 2>&1); then
    INSTALLED_HELPER_SHA256=${installed_digest_output%% *}
    # GNU sha256sum prefixes the digest with "\" when a Windows filename contains
    # backslashes, marking its escaped filename output. The digest itself remains raw hex.
    INSTALLED_HELPER_SHA256=$(printf '%s' "$INSTALLED_HELPER_SHA256" | tr -d '\\')
    [ "$INSTALLED_HELPER_SHA256" = "$HOSTED_HELPER_SHA256" ] \
      && ok "installed selected helper SHA-256 matches hosted member $HOSTED_HELPER_SHA256" \
      || bad "installed selected helper SHA-256 '$INSTALLED_HELPER_SHA256' does not equal hosted member '$HOSTED_HELPER_SHA256'"
  else
    bad "could not hash installed selected helper $INSTALLED_HELPER: $installed_digest_output"
  fi
fi

if version=$(spt adapter version omp-spt 2>&1); then
  [ "$version" = "$EXPECTED_VERSION" ] \
    && ok "adapter version is exactly $EXPECTED_VERSION" \
    || bad "adapter version '$version' does not equal expected '$EXPECTED_VERSION'"
else
  bad "adapter version query failed: $version"
fi

if label=$(spt adapter get-string omp-spt adapter_label 2>&1); then
  [ "$label" = "oh-my-pi (spt)" ] && ok "shared adapter strings resolve" || bad "adapter_label='$label'"
else
  bad "adapter_label query failed: $label"
fi

[ "$fail" -eq 0 ] \
  && { echo "RELEASE-ACQUIRE-INT OK target=$TARGET adapter_sha256=$HOSTED_ADAPTER_SHA256 helper=$HELPER_MEMBER helper_sha256=$HOSTED_HELPER_SHA256"; exit 0; } \
  || { echo "RELEASE-ACQUIRE-INT FAIL target=$TARGET adapter_sha256=$HOSTED_ADAPTER_SHA256 helper=$HELPER_MEMBER helper_sha256=$HOSTED_HELPER_SHA256"; exit 1; }
