from pathlib import Path
import hashlib,json,tarfile,urllib.request
root=Path('/var/www/html/artcare');full=root/'storage/app/elderly-support-deploy-20261016';out=root/'storage/app/elderly-support-save-feedback-20261020'
archive=full/'elderly-support-runtime.tar.gz'
expected='8667887534159aafb421fd70c5e60ac17d44ecc4c7b586bce7b9b5a2db8711bd'
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
assert sha(archive)==expected
with tarfile.open(archive) as data:
    for member in data.getmembers():
        assert not member.name.startswith('/') and '..' not in Path(member.name).parts
    data.extractall(full/'stage')
manifest=json.loads((full/'stage/manifest.json').read_text())
for row in manifest:
    assert sha(root/row['path'])==row['sha256'],row['path']
    assert sha(full/'stage'/row['path'])==row['sha256'],row['path']
request=urllib.request.Request('http://127.0.0.1:8089/elderly-support/register',headers={'Host':'elderly.mam-app.org','X-Forwarded-Proto':'https'})
body=urllib.request.urlopen(request).read()
assert b'placeholder="DD-MM-YYYY"' in body and b'data-date-field' in body
assert b'v=save-feedback-20261020' in body and b'id="esFeedback"' in body and b'id="esFeedbackDismiss"' in body
ready=urllib.request.urlopen('http://127.0.0.1:20249/ready').status
assert ready==200
result={'runtime_and_stage_files_verified':len(manifest),'archive_sha256':expected,'live_date_fields':True,'live_feedback_controls_and_versions':True,'live_dictionary_version':'1.7','tunnel_ready_status':ready}
(out/'backup-verification.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result,indent=2))
