#!/bin/sh
set -eu

archive_url="https://archive.smartycode.com"
manifest_url="https://smartycode.com/artifacts/lab-001-route-inventory.json"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT INT TERM

curl --fail --silent --show-error "$manifest_url" > "$tmp_dir/routes.json"
curl --fail --silent --show-error --head "$archive_url/" > "$tmp_dir/headers"
curl --fail --silent --show-error "$archive_url/" > "$tmp_dir/home.html"

python3 - "$tmp_dir" <<'PY'
import json
import pathlib
import sys

root = pathlib.Path(sys.argv[1])
manifest = json.loads((root / "routes.json").read_text())
headers = (root / "headers").read_text().lower()
html = (root / "home.html").read_text().lower()

assert manifest["stats"]["total"] == 442, manifest["stats"]
assert "content-type: text/html" in headers
assert "<script" not in html
assert "content-security-policy" in html

print(json.dumps({
    "archive": "reachable",
    "inventory_records": manifest["stats"]["total"],
    "scripts_in_home": 0,
    "result": "verified",
}, indent=2))
PY
