#!/usr/bin/env bash
set -euo pipefail

BASE='https://kpp.kenosian.com/k-lean/deck'
SOURCE_VERIFY_URL='https://kpp.kenosian.com/v1/verify?receipt_id=a8e676b2f7ba1790a91070e9'
SOURCE_SHA='d31fee3aaa31d82ba435d310d0970850cb1199dfcca9a47091441e2a77314b64'
MANIFEST_SHA='d9c86744cc9ec89043d4ddbdf0d2608e7ebc04c4f4824a08bf9292e898496a96'


work="${TMPDIR:-/tmp}/deck-review-$$"
mkdir -p "$work"
trap 'rm -rf "$work"' EXIT

curl --fail --location --silent --show-error "$BASE/DECK_Invariants.lean" -o "$work/DECK_Invariants.lean"
curl --fail --location --silent --show-error "$BASE/manifest.json" -o "$work/manifest.json"
printf '%s  %s\n' "$SOURCE_SHA" "$work/DECK_Invariants.lean" | sha256sum --check --status
printf '%s  %s\n' "$MANIFEST_SHA" "$work/manifest.json" | sha256sum --check --status

curl --fail --location --silent --show-error "$SOURCE_VERIFY_URL" -o "$work/source-receipt.json"
python3 - "$work/DECK_Invariants.lean" "$work/source-receipt.json" "$SOURCE_SHA" <<'PY_CHECK'
import hashlib,json,sys
source,receipt,sha=sys.argv[1:]
r=json.load(open(receipt))
assert hashlib.sha256(open(source,'rb').read()).hexdigest()==sha
assert r['verified'] is True and r['content_hash']=='sha256:'+sha
print('PASS: unchanged Lean source digest and public source receipt. Relocated manifest is descriptive, not separately sealed.')
PY_CHECK

mkdir -p "$work/project/KLean"
cat > "$work/project/lakefile.lean" <<'DECK_LAKEFILE_LEAN'
import Lake
open Lake DSL
package DECKReview where
require mathlib from git
  "https://github.com/leanprover-community/mathlib4" @ "dde4f2ecaec222a1c7db638d7536f0b195d8e953"
lean_lib KLean where
DECK_LAKEFILE_LEAN
cat > "$work/project/lean-toolchain" <<'DECK_LEAN_TOOLCHAIN'
leanprover/lean4:v4.14.0
DECK_LEAN_TOOLCHAIN
cat > "$work/project/lake-manifest.json" <<'DECK_LAKE_MANIFEST_JSON'
{"version": "1.1.0",
 "packagesDir": ".lake/packages",
 "packages":
 [{"url": "https://github.com/leanprover-community/mathlib4",
   "type": "git",
   "subDir": null,
   "scope": "",
   "rev": "dde4f2ecaec222a1c7db638d7536f0b195d8e953",
   "name": "mathlib",
   "manifestFile": "lake-manifest.json",
   "inputRev": "dde4f2ecaec222a1c7db638d7536f0b195d8e953",
   "inherited": false,
   "configFile": "lakefile.lean"},
  {"url": "https://github.com/leanprover-community/plausible",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "42dc02bdbc5d0c2f395718462a76c3d87318f7fa",
   "name": "plausible",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/LeanSearchClient",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "d7caecce0d0f003fd5e9cce9a61f1dd6ba83142b",
   "name": "LeanSearchClient",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/import-graph",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "519e509a28864af5bed98033dd33b95cf08e9aa7",
   "name": "importGraph",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v4.14.0",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/ProofWidgets4",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "68280daef58803f68368eb2e53046dabcd270c9d",
   "name": "proofwidgets",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v0.0.47",
   "inherited": true,
   "configFile": "lakefile.lean"},
  {"url": "https://github.com/leanprover-community/aesop",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "5a0ec8588855265ade536f35bcdcf0fb24fd6030",
   "name": "aesop",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v4.14.0",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/quote4",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "303b23fbcea94ac4f96e590c1cad6618fd4f5f41",
   "name": "Qq",
   "manifestFile": "lake-manifest.json",
   "inputRev": "master",
   "inherited": true,
   "configFile": "lakefile.lean"},
  {"url": "https://github.com/leanprover-community/batteries",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "8d6c853f11a5172efa0e96b9f2be1a83d861cdd9",
   "name": "batteries",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v4.14.0",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover/lean4-cli",
   "type": "git",
   "subDir": null,
   "scope": "leanprover",
   "rev": "726b3c9ad13acca724d4651f14afc4804a7b0e4d",
   "name": "Cli",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"}],
 "name": "KLean",
 "lakeDir": ".lake"}
DECK_LAKE_MANIFEST_JSON
cp "$work/DECK_Invariants.lean" "$work/project/KLean/DECK_Invariants.lean"
cd "$work/project"
lake exe cache get
lake env lean -j1 -M4096 KLean/DECK_Invariants.lean
printf '%s\n' 'PASS: Lean kernel accepted the supplied DECK file.'
