#!/usr/bin/env bash
# Production-only verifier. DEV/private-QA verifier remains one directory above.
set -euo pipefail

readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PUBLIC_KEY="$SCRIPT_DIR/release-public-key.pem"
# Independently reviewed production identity; private custody is OpenBao.
readonly PUBLIC_KEY_SHA256='05ad0731923fdb6e3fe156f524443d09fcef2c50f9f3e75bc3f2e2a363e385fa'
readonly ARCHIVE="${1:?usage: $0 ARCHIVE MANIFEST SIGNATURE}"
readonly MANIFEST="${2:?usage: $0 ARCHIVE MANIFEST SIGNATURE}"
readonly SIGNATURE="${3:?usage: $0 ARCHIVE MANIFEST SIGNATURE}"

[[ -f "$PUBLIC_KEY" && -f "$ARCHIVE" && -f "$MANIFEST" && -f "$SIGNATURE" ]] || {
  echo 'archive, manifest, signature, or pinned public key is missing' >&2; exit 2;
}
actual_public_key_sha256="$(sha256sum "$PUBLIC_KEY" | awk '{print $1}')"
[[ "$actual_public_key_sha256" == "$PUBLIC_KEY_SHA256" ]] || {
  echo 'release public key differs from the independently pinned production key' >&2; exit 1;
}
openssl pkeyutl -verify -pubin -inkey "$PUBLIC_KEY" -rawin \
  -in "$MANIFEST" -sigfile "$SIGNATURE" >/dev/null || {
  echo 'release manifest signature verification failed' >&2; exit 1;
}

expected_archive=''
expected_sha256=''
expected_version=''
expected_protocol_version=''
seen_artifact=0
seen_sha256=0
seen_version=0
seen_protocol_version=0
while IFS='=' read -r key value; do
  case "$key" in
    artifact)
      ((seen_artifact == 0)) || {
        echo 'release manifest repeats artifact' >&2; exit 1;
      }
      expected_archive="$value"
      seen_artifact=1
      ;;
    sha256)
      ((seen_sha256 == 0)) || {
        echo 'release manifest repeats sha256' >&2; exit 1;
      }
      expected_sha256="$value"
      seen_sha256=1
      ;;
    version)
      ((seen_version == 0)) || {
        echo 'release manifest repeats version' >&2; exit 1;
      }
      expected_version="$value"
      seen_version=1
      ;;
    protocol_version)
      ((seen_protocol_version == 0)) || {
        echo 'release manifest repeats protocol_version' >&2; exit 1;
      }
      expected_protocol_version="$value"
      seen_protocol_version=1
      ;;
    ''|'#'*) ;;
    *) echo "unsupported release manifest field: $key" >&2; exit 1 ;;
  esac
done < "$MANIFEST"
[[ "$expected_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
  echo 'release manifest has no valid semantic version' >&2; exit 1;
}
[[ "$expected_protocol_version" == 4 ]] || {
  echo 'release manifest has no valid protocol version' >&2; exit 1;
}
[[ "$expected_archive" == "$(basename "$ARCHIVE")" ]] || {
  echo 'release manifest artifact does not match archive' >&2; exit 1;
}
[[ "$expected_archive" == \
  "trackself-host-integration-linux-$expected_version-x86_64.tar.gz" ]] || {
  echo 'release manifest version does not match artifact' >&2; exit 1;
}
[[ "$expected_sha256" =~ ^[0-9a-f]{64}$ ]] || {
  echo 'release manifest has no valid sha256 digest' >&2; exit 1;
}
actual_sha256="$(sha256sum "$ARCHIVE" | awk '{print $1}')"
[[ "$actual_sha256" == "$expected_sha256" ]] || {
  echo 'archive digest does not match signed release manifest' >&2; exit 1;
}
echo "verified $(basename "$ARCHIVE") against the pinned release key"
