#!/usr/bin/env python3
"""Package each supported platform identically. Does not publish anything."""
import argparse
import gzip
import hashlib
import json
import lzma
from pathlib import Path
import shutil
import subprocess
import sys
import tarfile
import tempfile
import tomllib
import zipfile
from shader_bundle import package as package_shaders, verify_archive_seeds
from release_inventory import archive_inputs, example_payload
from runtime_platform import check_runtime_platform, macos_manifest

def digest(path):
    with path.open('rb') as stream:
        return hashlib.file_digest(stream, 'sha256').hexdigest()


ROOT = Path(__file__).resolve().parents[1]
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--binary', type=Path, required=True)
p.add_argument('--platform', choices=['darwin-arm64', 'linux-x64', 'windows-x64'], required=True)
p.add_argument('--output', type=Path, required=True)
p.add_argument('--shader-cache', type=Path, help='Portable bundle from scripts/prepare-shaders; defaults to shaders beside the binary')
p.add_argument('--slang-toolchain', type=Path, required=True, help='Pinned Slang toolchain directory; its compiler, runtime libraries and notices ship')
p.add_argument('--oidn-toolchain', type=Path, required=True, help='Pinned OIDN distribution from scripts/install-oidn; its runtime libraries and notices ship')
p.add_argument('--msvc-runtime', type=Path, help='windows-x64: Visual Studio redist folder (VC\\Redist\\MSVC\\<version>\\x64\\Microsoft.VC14x.CRT); the VC++ runtime DLLs that shipped DLLs import are copied beside them')
a = p.parse_args()
subprocess.run([sys.executable, str(ROOT / 'scripts/sync-version'), '--check'], check=True)
cargo_version = tomllib.loads((ROOT / 'Cargo.toml').read_text())['workspace']['package']['version']
try:
    reported = subprocess.check_output([str(a.binary.resolve()), '--version'], text=True).split()
except (OSError, subprocess.CalledProcessError) as error:
    p.error(f'cannot read binary version: {error}')
if len(reported) < 2 or reported[0] != 'vork' or reported[1] != cargo_version:
    p.error(f'binary version {reported!r} does not match Cargo version {cargo_version!r}')
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip()
if reported != ['vork', cargo_version, '(commit', commit + ';', 'API', 'v2)']:
    p.error(f'binary source identity {reported!r} does not match full source commit {commit}')
windows = a.platform.startswith('windows')
if windows and not (a.msvc_runtime and a.msvc_runtime.is_dir()):
    p.error('windows-x64 needs --msvc-runtime: OIDN imports the VC++ runtime, which ships app-local beside it')
compiler = a.slang_toolchain / ('bin/slangc.exe' if windows else 'bin/slangc')
if not compiler.is_file():
    p.error(f'Slang toolchain missing compiler: {compiler}')
# slangc prints its version on stderr.
try:
    version = subprocess.run([str(compiler), '-version'], capture_output=True, text=True, check=True).stderr.strip()
except (OSError, subprocess.CalledProcessError) as error:
    p.error(f'cannot run pinned Slang compiler {compiler}: {error}')
pinned = json.loads((ROOT / 'dist/slang.json').read_text())['version']
if version != pinned:
    p.error(f'Slang {pinned} required, found {version}')
for required in ['LICENSE', 'LICENSES', 'third-party-notices', 'bin', 'lib']:
    if not (a.slang_toolchain / required).exists():
        p.error(f'Slang toolchain missing {required}')
oidn_pin = json.loads((ROOT / 'dist/oidn.json').read_text())
try:
    oidn_receipt = json.loads((a.oidn_toolchain / 'vork-oidn.json').read_text())
except (OSError, ValueError) as error:
    p.error(f'OIDN needs scripts/install-oidn receipt: {error}')
if oidn_receipt != {'version': oidn_pin['version'], 'platform': a.platform, 'sha256': oidn_pin['archives'][a.platform]['sha256']}:
    p.error('OIDN receipt does not match pinned version and target platform')
for required in ['doc/LICENSE.txt', 'doc/third-party-programs.txt', 'bin', 'lib']:
    if not (a.oidn_toolchain / required).exists():
        p.error(f'OIDN distribution missing {required}')
subprocess.run([sys.executable, str(ROOT / 'scripts/generate-notice'), '--check'], check=True)
# Ship what vork runs plus every upstream notice. vork drives slangc only for
# Metal, SPIR-V and WGSL, so Slang's LLVM (CPU/host targets only), language
# server, interpreter, GFX, headers and docs stay out. Standard modules and the
# GLSL module stay: authored programs may import them. The SPIR-V optimizer stays where Vulkan runs.
# OIDN keeps every runtime library (all device backends and built-in models)
# and drops its sample tools, headers, CMake files and manuals.
NOTICES = ['LICENSE', 'LICENSES', 'third-party-notices']
SLANG = {
    'darwin-arm64': NOTICES + ['vork-build.json', 'bin/slangc', 'lib/libslang-compiler.0.*.dylib', 'lib/libslang-glsl-module-*.dylib', 'lib/slang-standard-module-*'],
    'linux-x64': NOTICES + ['bin/slangc', 'lib/libslang-compiler.so.*', 'lib/libslang-glsl-module-*.so', 'lib/libslang-glslang-*.so', 'lib/slang-standard-module-*'],
    'windows-x64': NOTICES + ['bin/slangc.exe', 'bin/slang-compiler.dll', 'bin/slang-glsl-module.dll', 'bin/slang-glslang.dll', 'bin/slang-standard-module-*'],
}
OIDN_NOTICES = ['vork-oidn.json', 'doc/LICENSE.txt', 'doc/third-party-programs*.txt']
OIDN = {
    'darwin-arm64': OIDN_NOTICES + ['lib/*.dylib'],
    'linux-x64': OIDN_NOTICES + ['lib/*.so*'],
    'windows-x64': OIDN_NOTICES + ['bin/*.dll'],
}


def copy_selected(source, destination, patterns):
    for pattern in patterns:
        matches = sorted(source.glob(pattern))
        if not matches:
            p.error(f'{source} has no {pattern}')
        for f in matches:
            target = destination / f.relative_to(source)
            target.parent.mkdir(parents=True, exist_ok=True)
            if f.is_symlink():
                target.symlink_to(f.readlink())
            elif f.is_dir():
                shutil.copytree(f, target, symlinks=True, ignore=shutil.ignore_patterns('._*'))
            else:
                shutil.copy2(f, target)


# Visual C++ runtime DLL names (the Microsoft.VC14x.CRT redist set). UCRT
# (api-ms-win-crt-*, ucrtbase) is part of Windows 10 and later and is not shipped.
VC_RUNTIME = ('vcruntime', 'msvcp', 'concrt', 'vccorlib', 'vcamp', 'vcomp')


def pe_imports(path):
    """DLL names in a PE file's import and delay-import tables."""
    import struct
    d = path.read_bytes()
    pe = struct.unpack_from('<I', d, 0x3c)[0]
    if d[pe:pe + 4] != b'PE\0\0':
        p.error(f'{path} is not a PE file')
    sections, optional_size = struct.unpack_from('<H', d, pe + 6)[0], struct.unpack_from('<H', d, pe + 20)[0]
    optional = pe + 24
    directories = optional + (112 if struct.unpack_from('<H', d, optional)[0] == 0x20b else 96)
    table = [struct.unpack_from('<8sIIII', d, optional + optional_size + i * 40)[1:] for i in range(sections)]
    def offset(rva):
        return next(rva - va + raw for size, va, raw_size, raw in table if va <= rva < va + max(size, raw_size))
    names = []
    for index, step, field in ((1, 20, 12), (13, 32, 4)):
        rva = struct.unpack_from('<I', d, directories + index * 8)[0]
        if not rva:
            continue
        entry = offset(rva)
        while (name := struct.unpack_from('<I', d, entry + field)[0]):
            start = offset(name)
            names.append(d[start:d.index(b'\0', start)].decode('ascii'))
            entry += step
    return names


def ship_vc_runtime(directory):
    """Copy the VC++ runtime DLLs that this directory's DLLs/EXEs import (transitively) beside them."""
    pending = sorted(f for f in directory.iterdir() if f.suffix.lower() in ('.dll', '.exe'))
    present = {f.name.lower() for f in pending}
    redist = {f.name.lower(): f for f in a.msvc_runtime.glob('*.dll')}
    while pending:
        for name in pe_imports(pending.pop()):
            if not name.lower().startswith(VC_RUNTIME) or name.lower() in present:
                continue
            if name.lower() not in redist:
                p.error(f'{a.msvc_runtime} has no {name}, which {directory.name} imports')
            shutil.copy2(redist[name.lower()], directory / redist[name.lower()].name)
            present.add(name.lower())
            pending.append(directory / redist[name.lower()].name)


a.output.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(dir=a.output) as tmp:
    stage = Path(tmp)
    shutil.copy2(a.binary, stage / ('vork.exe' if windows else 'vork'))
    if a.platform == 'linux-x64':
        build_receipt = a.binary.resolve().with_name('vork-build.json')
        try:
            receipt = json.loads(build_receipt.read_text())
        except (OSError, ValueError) as error:
            p.error(f'Linux distribution requires a build receipt: {error}')
        if receipt['binary_sha256'] != digest(stage / 'vork'):
            p.error('Linux build receipt does not match the staged executable')
        required = json.loads((ROOT / 'dist/linux-build.json').read_text())['native_footage_reader']
        if required and receipt['native_footage_reader'] != required:
            p.error(f'Linux distribution requires a {required} build receipt')
        shutil.copy2(build_receipt, stage / build_receipt.name)
    adapter_name = 'vork-slang-filesystem.exe' if windows else 'vork-slang-filesystem'
    adapter = a.binary.resolve().parent / adapter_name
    if not adapter.is_file():
        p.error('Slang filesystem adapter missing beside binary; rebuild the workspace')
    shutil.copy2(adapter, stage / adapter_name)
    shaders = a.shader_cache or a.binary.resolve().parent / 'shaders'
    if not shaders.is_dir():
        p.error('portable shaders are required: run scripts/prepare-shaders with this binary before packaging')
    package_shaders(stage / ('vork.exe' if windows else 'vork'), shaders, stage / 'shaders')
    expected_seeds = {p.name: digest(p) for p in (stage / 'shaders/pipeline-seeds').glob('*.vorkseed')}
    for source, name, patterns in [(a.slang_toolchain, 'slang', SLANG[a.platform]), (a.oidn_toolchain, 'oidn', OIDN[a.platform])]:
        copy_selected(source, stage / name, patterns)
    if windows:
        # vork.exe and Slang are static-CRT; OIDN's DLLs import MSVCP140/VCRUNTIME140.
        # vork loads OIDN with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR, so its dependencies resolve here.
        for directory in (stage, stage / 'slang/bin', stage / 'oidn/bin'):
            ship_vc_runtime(directory)
    try:
        check_runtime_platform((f for f in stage.rglob('*') if f.is_file()), a.platform)
    except ValueError as error:
        p.error(str(error))
    sdk = ROOT / 'packages/ts'
    if not (sdk / 'dist/index.js').exists():
        p.error('Build the vendored SDK first: cd packages/ts && npm ci && npm run build')
    # This manifest also owns release carry-over's shipped source inventory.
    for entry in archive_inputs():
        source = ROOT / entry['source']
        target = stage / entry['destination']
        target.parent.mkdir(parents=True, exist_ok=True)
        if source.is_dir():
            shutil.copytree(source, target,
                            ignore=shutil.ignore_patterns(*entry.get('ignore', [])))
        else:
            shutil.copy2(source, target)
    # Match the public source payload, never sweep the private checkout or downloads.
    for relative in example_payload():
        path = Path(relative)
        if path.is_absolute() or '..' in path.parts or not (path.parts[0] == 'examples' or path.parts[:2] == ('skills', 'vork')):
            p.error(f'invalid example payload path: {relative}')
        source = ROOT / path
        if source.is_symlink() or not source.is_file():
            p.error(f'missing or symlink example payload input: {relative}')
        target = stage / path
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(source, target)
    metadata = json.loads((sdk / 'package.json').read_text(encoding='utf-8'))
    # Archives flatten dist/ into sdk/; keep the canonical public entry points.
    def archive_exports(value):
        if isinstance(value, dict):
            return {key: archive_exports(entry) for key, entry in value.items()}
        if isinstance(value, list):
            return [archive_exports(entry) for entry in value]
        return './' + value[len('./dist/'):] if isinstance(value, str) and value.startswith('./dist/') else value
    archive_metadata = {key: metadata[key] for key in ('name', 'version', 'type', 'license', 'engines')}
    archive_metadata['exports'] = archive_exports(metadata['exports'])
    (stage / 'sdk/package.json').write_text(json.dumps(archive_metadata, indent=2) + '\n', encoding='utf-8')
    # integrations/ (the GPL Blender bridge) is a separate component and never ships in an archive.
    if any('integrations' in f.relative_to(stage).parts for f in stage.rglob('*')):
        p.error('release archive must not include integrations/')
    if a.platform == 'darwin-arm64':
        (stage / 'macos-runtime.json').write_text(json.dumps(macos_manifest(stage), indent=2) + '\n')
    archive = a.output / f'vork-{cargo_version}-{a.platform}.{"zip" if windows else "tar.gz"}'
    if windows:
        with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as z:
            for f in sorted(stage.rglob('*')):
                z.write(f, f.relative_to(stage).as_posix())
    else:
        with tarfile.open(archive, 'w:gz') as tar:
            for f in sorted(stage.iterdir()):
                tar.add(f, arcname=f.name)
    archives = [archive]
    if not windows:
        # The smaller download for installers that can read xz: the .tar.gz's own tar stream, recompressed,
        # so both archives hold the same bytes (cut-release refuses a pair whose streams differ).
        compact = archive.with_name(archive.name.removesuffix('.gz') + '.xz')
        with gzip.open(archive, 'rb') as source, lzma.open(compact, 'wb', preset=9) as target:
            shutil.copyfileobj(source, target, 1 << 20)
        archives.append(compact)
        if expected_seeds:
            for path in archives:
                verify_archive_seeds(path, expected_seeds)
        else:
            print(f'WARNING: {a.platform} archive has no native pipeline seed', file=sys.stderr)
    sums = a.output / 'SHA256SUMS'
    rows = sums.read_text(encoding='utf-8').splitlines() if sums.exists() else []
    names = {archive.name for archive in archives}
    rows = [r for r in rows if r.split()[-1] not in names]
    rows += [f'{digest(archive)}  {archive.name}' for archive in archives]
    sums.write_text('\n'.join(sorted(rows)) + '\n', encoding='utf-8', newline='\n')
    for archive in archives:
        print(f'{archive}: {archive.stat().st_size} bytes')
