#!/usr/bin/env python3
import json,os,sys,subprocess
from pathlib import Path
d=json.loads(Path(sys.argv[1]).read_text());source=Path(d['source'])
if not source.exists():sys.exit('El juego original ya no está disponible en esta consola.')
if d.get('kind')=='pes13':
    script=Path(os.environ.get('PES13_ROOT','/userdata'))/'system/pes2013/lanzar.sh'
    os.execv('/bin/bash',['/bin/bash',str(script),str(source)])
# Mismo entorno que WineGenerator al entrar desde Windows; sh ya aporta el mapa SDL.
os.environ['SDL_JOYSTICK_HIDAPI']='0'
try:
    language=subprocess.check_output(['/usr/bin/batocera-settings-get','system.language'],text=True).strip()
    if language:os.environ.update(LANG=language+'.UTF-8',LC_ALL=language+'.UTF-8')
except (OSError,subprocess.CalledProcessError):pass
if Path('/var/tmp/nvidia.prime').exists():
    for key in ['__NV_PRIME_RENDER_OFFLOAD','__VK_LAYER_NV_optimus','__GLX_VENDOR_LIBRARY_NAME']:os.environ.pop(key,None)
os.execv('/usr/bin/batocera-wine',['/usr/bin/batocera-wine','windows','play',str(source)])
