from __future__ import annotations
import hashlib
from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parent
manifest = ROOT / 'PACKAGE_MANIFEST_SHA256.txt'
errors = []
checked = 0
for line in manifest.read_text(encoding='utf-8').splitlines():
    if not line.strip():
        continue
    expected, relative = line.split('  ', 1)
    path = ROOT / relative
    if not path.is_file():
        errors.append(f'MISSING {relative}')
        continue
    h = hashlib.sha256()
    with path.open('rb') as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b''):
            h.update(chunk)
    actual = h.hexdigest()
    if actual != expected:
        errors.append(f'HASH_MISMATCH {relative}')
    checked += 1
if errors:
    print('\n'.join(errors))
    sys.exit(1)
print(f'PACKAGE_INTEGRITY_PASS files={checked}')
