"""Download and join the complete, SHA-verified RealtimeCUA rocket host TAR.XZ."""
import sys,json,hashlib,urllib.request,pathlib
from urllib.parse import urljoin
manifest_url=sys.argv[1] if len(sys.argv)>1 else 'https://rongjiehuang.github.io/RealtimeCUABench/rocket/packages/manifest.json'
variant=sys.argv[2] if len(sys.argv)>2 else '210'
manifest=json.load(urllib.request.urlopen(manifest_url));info=manifest['variants'][variant];out=pathlib.Path(info['filename']);temporary=out.with_suffix('.partial');full=hashlib.sha256()
try:
 with temporary.open('wb') as stream:
  for part in info['parts']:
   data=urllib.request.urlopen(urljoin(manifest_url,part['url'])).read();assert len(data)==part['bytes'] and hashlib.sha256(data).hexdigest()==part['sha256'],'Part SHA mismatch';stream.write(data);full.update(data)
 assert full.hexdigest()==info['sha256'],'Full archive SHA mismatch'
 temporary.replace(out);print(str(out),info['sha256'])
except BaseException:
 temporary.unlink(missing_ok=True);raise
