"""Compile the original Room for Change Java/GWT game; no game logic replacement."""
from pathlib import Path
import os,subprocess,json,shutil
ROOT=Path(__file__).resolve().parents[1]
tools=ROOT/'vendor/gwt-build-tools'
source=ROOT/'vendor/game-91/source/roomforchange'
html=source/'roomforchange-html'
module=html/'src/com/sturdyhelmetgames/roomforchange/GwtDefinition.gwt.xml'
text=module.read_text(encoding='utf-8')
if 'user.agent' not in text:text=text.replace('</module>',"<set-property name='user.agent' value='safari' />\n</module>")
module.write_text(text,encoding='utf-8')
entries=[html/'src',source/'roomforchange/src',*tools.glob('*.jar'),*list((source/'roomforchange/libs').glob('*.jar')),*list((html/'war/WEB-INF/lib').glob('*.jar'))]
output=ROOT/'public/games/game-91'
output.mkdir(parents=True,exist_ok=True)
java=next(tools.glob('*/bin/java.exe'))
command=[str(java),'-Dgwt.persistentunitcachedir='+str(tools/'unit-cache'),'-Xmx1024m','-Xss16m','-cp',os.pathsep.join(str(p) for p in entries),'com.google.gwt.dev.Compiler','-logLevel','INFO','-style','OBF','-localWorkers','2','-war',str(output),'com.sturdyhelmetgames.roomforchange.GwtDefinition']
print('Compiling original Room for Change GWT source',flush=True)
result=subprocess.run(command,cwd=html)
if result.returncode:raise SystemExit(result.returncode)
shutil.copytree(html/'war/assets',output/'assets',dirs_exist_ok=True)
page=(html/'war/index.html').read_text(encoding='utf-8')
page=page.replace('<style>','<meta name="viewport" content="width=device-width,initial-scale=1"><style>html,body{margin:0;background:#151515;overflow:hidden} canvas{max-width:100%;max-height:100vh;}')
(output/'index.html').write_text(page,encoding='utf-8')
print('Browser files built:',output,flush=True)
