( set -eu
  site=https://www.orbytlabs.ai
  github=https://raw.githubusercontent.com/orbytlabs-ai/ai-agent-research/main/provenance
  file=/research/agent-leadership-governance/agent-leadership-governance.pdf
  owner_keys='orbyt-owner namespaces="orbyt-provenance" ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJMl7pELNGVsAUl/c1J2pe9uQ/VKmoHFZ8RzRTSRLumlI2jssJPoL4cPTbxXHKxQgYgGuG3HJqQVMscsCFD4lZA=
orbyt-owner namespaces="orbyt-provenance" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDel/a+TT40HU7lP6NUIbSysF/u+sOpQWRbGM7Z5SC5G'
  printf 'Drag the file here and press Return: '
  IFS= read -r given || given=
  given=$(printf '%s' "$given" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  case "$given" in
    \'*\') given=$(printf '%s' "$given" | sed -e "s/^'//" -e "s/'\$//" -e "s/'\\\\''/'/g") ;;
    \"*\") given=$(printf '%s' "$given" | sed -e 's/^"//' -e 's/"$//') ;;
    *) given=$(printf '%s' "$given" | sed -e 's/\\\(.\)/\1/g') ;;
  esac
  if [ -n "$given" ]; then
    [ -f "$given" ] && [ -r "$given" ] || { echo "NOT FOUND: there is no file at $given"; exit 1; }
    hash=$(shasum -a 256 < "$given" | cut -d' ' -f1)
  fi
  mkdir -p orbyt-provenance && cd orbyt-provenance
  get() { curl -fsS "$1" -o "$2" || { echo "CANNOT FETCH: $1"; exit 1; }; }
  day='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
  get "$site/provenance/index.json" index.json
  latest=$(jq -r --arg re "$day" '.latest // empty | select(test($re))' index.json)
  [ -n "$latest" ] || { echo "NOT RECORDED YET: no manifest has been recorded yet, so there is nothing to check against."; exit 1; }
  if [ -z "$given" ]; then
    echo "SITE COPY: no file given, so this checks the site's copy of $file"
    get "$site$file" site-copy.bin
    hash=$(shasum -a 256 < site-copy.bin | cut -d' ' -f1)
  fi
  check() {
    get "$site/provenance/manifests/$1.json" "manifest-$1.json"
    jq -cSj 'del(.manifest_sha256)' "manifest-$1.json" > "manifest-$1.body" || { echo "BAD MANIFEST: the $1 manifest is not valid JSON"; exit 1; }
    mh=$(shasum -a 256 < "manifest-$1.body" | cut -d' ' -f1)
    [ "$mh" = "$(jq -r .manifest_sha256 "manifest-$1.json")" ] || { echo "BAD MANIFEST: the $1 manifest does not hash to its own manifest_sha256"; exit 1; }
  }
  check "$latest"
  if [ -n "$given" ]; then
    rec=$(jq -r --arg h "$hash" '[.artifacts[] | select(.sha256 == $h) | .path][0] // empty' "manifest-$latest.json")
  else
    rec=$(jq -r --arg p "public$file" --arg h "$hash" '[.artifacts[] | select(.path == $p and .sha256 == $h) | .path][0] // empty' "manifest-$latest.json")
  fi
  [ -n "$rec" ] || { echo "NO MATCH: this file is not one we recorded in the $latest manifest"; exit 1; }
  first=$(jq -r --arg p "$rec" --arg re "$day" '[.latest_artifacts[] | select(.path == $p) | .first_recorded][0] // empty | select(test($re))' index.json)
  days=$(jq -rn --arg a "${first:-$latest}" --arg b "$latest" 'if $a <= $b then range(($a + "T00:00:00Z" | fromdate); ($b + "T00:00:00Z" | fromdate) + 1; 86400) | strftime("%Y-%m-%d") else $b end')
  : > days.txt
  for d in $(printf '%s\n' "$days"); do
    check "$d"
    jq -e --arg p "$rec" --arg h "$hash" 'any(.artifacts[]; .path == $p and .sha256 == $h)' "manifest-$d.json" > /dev/null || { echo "CHANGED: the $d manifest does not record these bytes for ${rec#public}"; exit 1; }
    echo "$d $mh" >> days.txt
  done
  d0=$(head -n 1 days.txt | cut -d' ' -f1)
  h0=$(head -n 1 days.txt | cut -d' ' -f2)
  cp "manifest-$d0.body" manifest.body
  echo "MANIFEST: the $d0 manifest hashes to $h0"
  if curl -fsS "$site/provenance/stamps/$d0.ots" -o stamp.ots 2> /dev/null; then
    echo "STAMP: the $d0 manifest has a timestamp proof, saved as orbyt-provenance/stamp.ots"
  else
    rm -f stamp.ots
    echo "NO STAMP: the $d0 manifest has no timestamp proof yet"
  fi
  printf '%s\n' "$owner_keys" > owner.allowed_signers
  weeks=$(jq -r --arg a "$d0" --arg re "$day" '.seals[].week_ending | select(test($re) and . >= $a)' index.json)
  if [ "$(jq '.seals | length' index.json)" = 0 ]; then
    echo "NO SEAL YET: the owner has not signed a weekly seal yet"
  else
    sealed=
    for w in $(printf '%s\n' "$weeks"); do
      get "$site/provenance/seals/$w.json" "seal-$w.json"
      get "$site/provenance/seals/$w.json.sig" "seal-$w.json.sig"
      said=$(ssh-keygen -Y verify -f owner.allowed_signers -I orbyt-owner -n orbyt-provenance -s "seal-$w.json.sig" < "seal-$w.json" 2>&1) || { echo "BAD SEAL: the seal for the week ending $w does not verify against the owner key in this command"; exit 1; }
      jq -r '.manifests[] | "\(.date) \(.manifest_sha256)"' "seal-$w.json" > "seal-$w.days"
      bad=$(awk 'NR == FNR { h[$1] = $2; next } ($1 in h) && (h[$1] < $2 || h[$1] > $2) { print $1; exit }' days.txt "seal-$w.days")
      [ -z "$bad" ] || { echo "REWRITTEN: the seal for the week ending $w names a different $bad manifest"; exit 1; }
      if grep -qx "$d0 $h0" "seal-$w.days"; then sealed=$w; key=$(printf '%s' "$said" | sed -n 's/.* key \(SHA256:[^ ]*\).*/\1/p'); fi
    done
    if [ -n "$sealed" ]; then
      echo "SEALED: the owner's seal for the week ending $sealed names the $d0 manifest, signed by key $key"
    else
      echo "NOT YET SEALED: no seal names the $d0 manifest yet"
    fi
  fi
  missing=
  list=owner.allowed_signers
  for d in $(printf '%s\n' "$days"); do list="$list manifest-$d.json"; done
  for w in $(printf '%s\n' "$weeks"); do list="$list seal-$w.json seal-$w.json.sig"; done
  for f in $(printf '%s\n' "$list"); do
    case "$f" in manifest-*) g="manifests/${f#manifest-}" ;; seal-*) g="seals/${f#seal-}" ;; *) g="$f" ;; esac
    code=$(curl -sS -o "github-copy" -w '%{http_code}' "$github/$g" 2> /dev/null || true)
    case "$code" in
      200) cmp -s github-copy "$f" || { echo "GITHUB DIFFERS: the GitHub copy of $g is not what this command or this site holds"; exit 1; } ;;
      *) missing="${missing:-$g}" ;;
    esac
  done
  rm -f github-copy
  if [ -n "$missing" ]; then
    echo "NOT ON GITHUB YET: the GitHub copy of $missing could not be read, so not everything was compared"
  else
    echo "GITHUB: the GitHub copies of the owner key, the manifests and the seals are the same files"
  fi
  echo "MATCH: ${rec#public} has the bytes first recorded on $d0, unchanged through $latest, sha256 $hash"
)
