@echo off
setlocal EnableExtensions

set "HOST_UID=1000"
set "HOST_GID=1000"
set "HOST_USER=student"
set "PDK_ROOT=/cad/share/pdk"

set "IMAGE_TAG=srampr/ee5311_iitm:jul_nov_2026"
set "SAVED_TAG=srampr/ee5311_iitm:saved"

set "ACTION=%~1"
if "%ACTION%"=="" set "ACTION=up"
if not "%~1"=="" shift

set "ARGS="
:collect_args
if "%~1"=="" goto args_done
set "ARGS=%ARGS% %1"
shift
goto collect_args
:args_done

if not exist "%USERPROFILE%\ee5311" mkdir "%USERPROFILE%\ee5311"

set "SCRIPT_DIR=%~dp0"
cd /d "%SCRIPT_DIR%"

if not defined VNC_HOST_PORT set "VNC_HOST_PORT=5999"

set "WIN_HOME=%USERPROFILE:\=/%"
set "TMP_YAML=%TEMP%\ee5311-compose-%RANDOM%.yml"

set "REF=%IMAGE_TAG%"
docker image inspect %SAVED_TAG% >nul 2>&1 && set "REF=%SAVED_TAG%"

if /I "%ACTION%"=="up"     goto do_up
if /I "%ACTION%"=="fresh"  goto do_fresh
if /I "%ACTION%"=="save"   goto do_save_verb
if /I "%ACTION%"=="down"   goto do_down
if /I "%ACTION%"=="pause"  goto do_pause
if /I "%ACTION%"=="resume" goto do_resume
goto usage

:do_up
if "%REF%"=="%IMAGE_TAG%" echo No %SAVED_TAG% snapshot yet; starting from %IMAGE_TAG%. 1>&2
call :write_yaml %REF%
type "%TMP_YAML%" | docker compose -f - up -d %ARGS%
goto finish

:do_fresh
call :write_yaml %IMAGE_TAG%
type "%TMP_YAML%" | docker compose -f - up -d --force-recreate %ARGS%
goto finish

:do_save_verb
call :do_save
if errorlevel 1 (
    echo No running ee5311 container to save; start one with '%~nx0 up' or '%~nx0 fresh'. 1>&2
    set "EXIT_CODE=1"
    goto cleanup
)
goto finish

:do_down
set "SKIP_SAVE="
if defined ARGS if not "%ARGS%"=="%ARGS:--no-save=%" (
    set "SKIP_SAVE=1"
    set "ARGS=%ARGS:--no-save=%"
)
if defined SKIP_SAVE (
    call :get_cid
    if defined CID docker unpause %CID% >nul 2>&1
) else (
    call :do_save
    if errorlevel 1 echo No running ee5311 container; nothing to save. 1>&2
)
docker image inspect %SAVED_TAG% >nul 2>&1 && set "REF=%SAVED_TAG%"
call :write_yaml %REF%
type "%TMP_YAML%" | docker compose -f - down %ARGS%
goto finish

:do_pause
call :write_yaml %REF%
type "%TMP_YAML%" | docker compose -f - pause image
goto finish

:do_resume
call :write_yaml %REF%
type "%TMP_YAML%" | docker compose -f - unpause image
goto finish

:get_cid
set "CID="
call :write_yaml %REF%
for /f %%i in ('type "%TMP_YAML%" ^| docker compose -f - ps -q image') do set "CID=%%i"
exit /b 0

:do_save
call :get_cid
if not defined CID exit /b 1
docker unpause %CID% >nul 2>&1
docker exec %CID% bash -c "touch /home/%HOST_USER%/.ee5311_saved_state && chown %HOST_UID%:%HOST_GID% /home/%HOST_USER%/.ee5311_saved_state"
docker commit %CID% %SAVED_TAG% >nul
echo Saved container as %SAVED_TAG%; '%~nx0 up' will resume from it. 1>&2
exit /b 0

:write_yaml
(
    echo name: ee5311
    echo services:
    echo   image:
    echo     image: %~1
    echo     entrypoint: ["bash", "-c", "rm -f /tmp/.X*-lock /tmp/.X11-unix/X*; exec /entrypoint.sh"]
    echo     environment:
    echo       - HOST_UID=%HOST_UID%
    echo       - HOST_GID=%HOST_GID%
    echo       - HOST_USER=%HOST_USER%
    echo     volumes:
    echo       - %WIN_HOME%/ee5311:/home/%HOST_USER%/ee5311
    echo     ports:
    echo       - "%VNC_HOST_PORT%:5901"
) > "%TMP_YAML%"
exit /b 0

:usage
echo Usage: %~nx0 [up^|fresh^|save^|down^|pause^|resume] [extra docker compose args...] 1>&2
echo   up      start from the %SAVED_TAG% snapshot if present, else %IMAGE_TAG% 1>&2
echo   fresh   force a new container from %IMAGE_TAG% (ignores saved state^) 1>&2
echo   save    commit the running container's state to %SAVED_TAG% 1>&2
echo   down    save the running container (unless --no-save^), then stop and remove the stack 1>&2
echo   pause   freeze the running container's processes in RAM (resume to continue^) 1>&2
echo   resume  unfreeze a paused container 1>&2
set "EXIT_CODE=1"
goto cleanup

:finish
set "EXIT_CODE=%ERRORLEVEL%"

:cleanup
del "%TMP_YAML%" >nul 2>&1
exit /b %EXIT_CODE%
