#!/usr/bin/env python3
# BEGIN CHANGE: apply WA allowlist patch to open-tibia-library outfit generator
"""Copy otl-patches/outfitImageGenerator.allowlist.ts -> open-tibia-library/outfitImageGenerator.ts"""
from __future__ import annotations

import shutil
import subprocess
import sys
from pathlib import Path

TOOLS = Path(__file__).resolve().parent
PATCH = TOOLS / "otl-patches" / "outfitImageGenerator.allowlist.ts"
OTL = TOOLS / "open-tibia-library"
TARGET = OTL / "outfitImageGenerator.ts"


def main() -> int:
    if not PATCH.is_file():
        print("Missing patch:", PATCH)
        return 1
    if not OTL.is_dir():
        print("Missing OTL clone:", OTL)
        print("Clone gesior/open-tibia-library into tools/open-tibia-library then npm install")
        return 1
    shutil.copy2(PATCH, TARGET)
    print("Applied patch ->", TARGET)
    if "--build" in sys.argv:
        print("Running npm run build in", OTL)
        r = subprocess.run("npm run build", cwd=str(OTL), shell=True)
        return r.returncode
    print("Run: cd tools/open-tibia-library && npm run build")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
# END CHANGE
