#!/usr/bin/env sh
set -eu

test -s docs/SIMD.md
test -s docs/SIMD_ADMISSION.md
test -s docs/UNSAFE.md
test -s docs/BENCHMARKS.md
test -s docs/RELEASE_EVIDENCE.md
test -s docs/SIMD_ENCODE_ADMISSION_DRAFT.md

active_variants="$(
    awk '
        /pub\(crate\) enum ActiveBackend/ {
            inside = 1
            next
        }
        inside && /^}/ {
            inside = 0
        }
        inside {
            line = $0
            sub(/\/\/.*/, "", line)
            if (line ~ /^[[:space:]]*[A-Za-z][A-Za-z0-9_]*,/) {
                gsub(/[[:space:],]/, "", line)
                print line
            }
        }
    ' src/simd/mod.rs
)"

expected_active_variants="Scalar
Avx512Vbmi
Avx2
Ssse3Sse41
Neon
WasmSimd128
Rvv"
if [ "$active_variants" != "$expected_active_variants" ]; then
    echo "simd admission: ActiveBackend must contain only Scalar and admitted AVX-512/AVX2/SSSE3/NEON/wasm/RVV backends" >&2
    printf '%s\n' "$active_variants" >&2
    exit 1
fi

if grep -R -e 'ActiveBackend::Simd' src; then
    echo "simd admission: non-admitted accelerated ActiveBackend dispatch was added without admission gate update" >&2
    exit 1
fi

if ! awk '
    /fn detect_active_backend\(\) -> ActiveBackend/ {
        inside = 1
    }
    inside && /ActiveBackend::Avx512Vbmi/ {
        avx512 = 1
    }
    inside && /ActiveBackend::Avx2/ {
        avx2 = 1
    }
    inside && /ActiveBackend::Ssse3Sse41/ {
        ssse3 = 1
    }
    inside && /ActiveBackend::Neon/ {
        neon = 1
    }
    inside && /ActiveBackend::WasmSimd128/ {
        wasm = 1
    }
    inside && /ActiveBackend::Rvv/ {
        rvv = 1
    }
    inside && /ActiveBackend::Scalar/ {
        scalar = 1
    }
    inside && /^}/ {
        exit (scalar && avx512 && avx2 && ssse3 && neon && wasm && rvv) ? 0 : 1
    }
    END {
        if (!inside) {
            exit 1
        }
    }
' src/simd/mod.rs; then
    echo "simd admission: detect_active_backend must explicitly return admitted AVX-512, AVX2, SSSE3/SSE4.1, NEON, wasm simd128, exact-profile RVV, and scalar fallback" >&2
    exit 1
fi

for required_text in \
    "Do not advertise SIMD acceleration" \
    "Benchmark evidence that reports hardware" \
    "register-retention cleanup strategy" \
    "explicit register cleanup implementation and tests" \
    "real non-dispatchable prototype" \
    "candidate only" \
    "admitted backend" \
    "runtime-probed \`std\` dispatch or compile-time-proven" \
    "Decode acceleration" \
    "Required precision" \
    "Performance numbers are release notes evidence only" \
    "Admitted backends: AVX-512 VBMI encode, AVX2 encode, SSSE3/SSE4.1 encode, NEON encode, AVX-512 VBMI strict decode, AVX2 strict decode, SSSE3/SSE4.1 strict decode, NEON strict decode, and exact-profile RVV 1.0 encode/strict decode" \
    "Active encode priority: AVX-512 VBMI, then AVX2, then SSSE3/SSE4.1" \
    "active strict-decode priority: AVX2, then SSSE3/SSE4.1" \
    "wasm simd128 runtime smoke evidence" \
    "The active non-scalar backends" \
    "Advertise SIMD acceleration only with the admitted backend name and scope"
do
    if ! grep -R -q "$required_text" docs/SIMD.md docs/SIMD_ADMISSION.md docs/UNSAFE.md docs/RELEASE_EVIDENCE.md docs/SIMD_ENCODE_ADMISSION_DRAFT.md; then
        echo "simd admission: missing required SIMD admission text: $required_text" >&2
        exit 1
    fi
done

backend_rows="$(
    awk '
        /^\| AVX-512 VBMI / || /^\| AVX2 / || /^\| SSSE3\/SSE4\.1 / || /^\| NEON / || /^\| wasm `simd128` / || /^\| RVV 1\.0 / {
            print
        }
    ' docs/SIMD_ADMISSION.md
)"

backend_row_count="$(printf '%s\n' "$backend_rows" | sed '/^$/d' | wc -l | tr -d ' ')"
if [ "$backend_row_count" -ne 6 ]; then
    echo "simd admission: expected exactly six backend rows in docs/SIMD_ADMISSION.md" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

avx512_row="$(printf '%s\n' "$backend_rows" | grep '^| AVX-512 VBMI ')"
if ! printf '%s\n' "$avx512_row" | grep '| admitted encode and exact/static strict decode |' >/dev/null 2>&1; then
    echo "simd admission: AVX-512 VBMI row must distinguish automatic encode from exact/static decode" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

avx2_row="$(printf '%s\n' "$backend_rows" | grep '^| AVX2 ')"
if ! printf '%s\n' "$avx2_row" | grep '| admitted backend |' >/dev/null 2>&1; then
    echo "simd admission: AVX2 row must be an admitted backend" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

ssse3_row="$(printf '%s\n' "$backend_rows" | grep '^| SSSE3/SSE4\.1 ')"
if ! printf '%s\n' "$ssse3_row" | grep '| admitted backend |' >/dev/null 2>&1; then
    echo "simd admission: SSSE3/SSE4.1 row must be an admitted backend" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

neon_row="$(printf '%s\n' "$backend_rows" | grep '^| NEON ')"
if ! printf '%s\n' "$neon_row" | grep '| admitted backend |' >/dev/null 2>&1; then
    echo "simd admission: NEON row must be an admitted backend" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

wasm_row="$(printf '%s\n' "$backend_rows" | grep '^| wasm `simd128` ')"
if ! printf '%s\n' "$wasm_row" | grep '| admitted backend |' >/dev/null 2>&1; then
    echo "simd admission: wasm simd128 row must be an admitted backend" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

rvv_row="$(printf '%s\n' "$backend_rows" | grep '^| RVV 1\.0 ')"
if ! printf '%s\n' "$rvv_row" | grep '| admitted exact-profile backend |' >/dev/null 2>&1; then
    echo "simd admission: RVV 1.0 row must remain limited to the admitted exact profile" >&2
    printf '%s\n' "$backend_rows" >&2
    exit 1
fi

if ! grep -q 'Firefox/SpiderMonkey, and operator-run Safari/WebKit' docs/SIMD_ADMISSION.md docs/SIMD.md; then
    echo "simd admission: wasm admission must distinguish automated Firefox/SpiderMonkey from operator-run Safari/WebKit evidence" >&2
    exit 1
fi

if ! grep -q 'Firefox/SpiderMonkey runtime smoke evidence' docs/SIMD_ADMISSION.md docs/SIMD.md; then
    echo "simd admission: wasm admission must name Firefox/SpiderMonkey runtime smoke evidence" >&2
    exit 1
fi

if ! grep -q 'Safari/WebKit runtime smoke evidence' docs/SIMD_ADMISSION.md docs/SIMD.md; then
    echo "simd admission: wasm admission must name Safari/WebKit runtime smoke evidence" >&2
    exit 1
fi

if ! grep -F -q "fn standard_family_decode_surfaces_cover_tails_and_padding()" src/decode_surface_tests.rs; then
    echo "simd admission: missing Standard-family tail/padding decode surface evidence" >&2
    exit 1
fi

if ! grep -F -q "fn standard_family_decode_error_surfaces_match_scalar()" src/decode_surface_tests.rs; then
    echo "simd admission: missing Standard-family malformed decode surface evidence" >&2
    exit 1
fi

if ! grep -F -q "RuntimeAlphabetMapperFor::<A>::VALUE" src/scalar.rs \
    || ! grep -F -q ".decode::<A>(byte)" src/scalar.rs; then
    echo "simd admission: scalar strict decode must derive mapping from Alphabet::ENCODE" >&2
    exit 1
fi
if grep -F -q "A::decode(byte)" src/scalar.rs src/simd/mod.rs; then
    echo "simd admission: overridable Alphabet::decode must not be an engine or admission boundary" >&2
    exit 1
fi
for source in src/simd/x86/decode.rs src/simd/neon.rs src/simd/wasm.rs; do
    if ! grep -F -q "supports_decode_alphabet" "$source"; then
        echo "simd admission: $source is missing decode-specific alphabet admission" >&2
        exit 1
    fi
done

echo "simd admission: automatic and exact/static backend scopes are gated"
