@echo off
setlocal
cd /d "%~dp0"
title Clipshelf

rem An INSTALLED copy already has its own interpreter and its own
rem dependencies. Everything below builds a .venv from a system Python
rem instead - a second, parallel runtime inside the install directory,
rem which the uninstaller does not remove and the updater preserves
rem forever. Hand straight over to the bundled one, using the console
rem interpreter so whatever went wrong stays visible: anyone running
rem run.bat by hand is already debugging a shortcut.
if not exist "installed" goto :portable
if not exist "python\python.exe" goto :portable
"python\python.exe" "run.py" %*
exit /b %errorlevel%

:portable

rem NOTE: keep this file ASCII-only, CRLF, and free of shell metacharacters
rem in comments. cmd.exe parses redirection even on rem lines, and labels
rem inside parenthesised blocks are unreliable. Structure is deliberately
rem flat: goto + labels, no clever nesting.

rem A leftover PYTHONHOME or PYTHONPATH from another Python install breaks
rem every python we launch. Our venv needs neither, so clear them here.
rem This only affects this window, not the user's environment.
if defined PYTHONHOME echo [i] ignoring PYTHONHOME for this app
if defined PYTHONPATH echo [i] ignoring PYTHONPATH for this app
set "PYTHONHOME="
set "PYTHONPATH="

set "PY="
py -3 -c "import sys; sys.exit(0 if sys.version_info >= (3,11) else 1)" >nul 2>&1
if not errorlevel 1 set "PY=py -3"
if defined PY goto :have_python
python -c "import sys; sys.exit(0 if sys.version_info >= (3,11) else 1)" >nul 2>&1
if not errorlevel 1 set "PY=python"
if defined PY goto :have_python

echo.
echo   Python 3.11+ is required. Install it with:
echo       winget install Python.Python.3.12
echo   then close this window and run run.bat again.
echo.
pause
exit /b 1

:have_python
where ffmpeg >nul 2>&1
if not errorlevel 1 goto :have_ffmpeg
echo.
echo   ffmpeg is required. Install it with:
echo       winget install Gyan.FFmpeg
echo   then close this window and run run.bat again.
echo.
pause
exit /b 1

:have_ffmpeg
if exist ".venv" goto :have_venv
echo first run: setting up a private Python environment...
%PY% -m venv .venv
if errorlevel 1 goto :venv_failed

:have_venv
".venv\Scripts\python.exe" -c "import encodings, ssl, sqlite3" >nul 2>&1
if not errorlevel 1 goto :venv_ok

echo.
echo   The private Python environment is broken.
echo   Usually a leftover PYTHONHOME or PYTHONPATH pointing at another
echo   Python. Check them with:  echo %%PYTHONHOME%%
echo   Clear them in Settings, System, About, Advanced system settings,
echo   Environment Variables. Then delete the .venv folder and retry.
echo.
pause
exit /b 1

:venv_ok
if exist ".venv\deps-ok" goto :have_deps
echo installing dependencies, one-time, a few minutes...
".venv\Scripts\python.exe" -m pip install --upgrade pip
".venv\Scripts\python.exe" -m pip install -r requirements.txt
if errorlevel 1 goto :deps_failed
echo ok> ".venv\deps-ok"

:have_deps
where node >nul 2>&1
if errorlevel 1 goto :no_node
if exist "node_modules" goto :launch
echo installing render dependencies...
call npm ci --silent >nul 2>&1
goto :launch

:no_node
echo [!] Node.js not found. Editing works, but video exports are disabled.
echo     Install it from https://nodejs.org and run run.bat again.

:launch
rem  run.bat --selftest   check this machine and print a report
rem  run.bat --console    keep this window open and show all output
rem  run.bat --browser    no app window, use the default browser
rem  run.bat --lan        also listen on the network, trusted networks only
if "%~1"=="" goto :windowed
if /i "%~1"=="--console" goto :console
".venv\Scripts\python.exe" run.py %*
pause
exit /b 0

:console
".venv\Scripts\python.exe" run.py --browser
pause
exit /b 0

:windowed
if not exist ".venv\Scripts\pythonw.exe" goto :fallback_console
del /q ".running" >nul 2>&1
echo starting...
start "" ".venv\Scripts\pythonw.exe" run.py
set /a _tries=0

:wait_loop
if exist ".running" goto :started
set /a _tries+=1
if %_tries% GEQ 40 goto :never_started
ping -n 2 127.0.0.1 >nul
goto :wait_loop

:started
exit /b 0

:never_started
echo.
echo   Clipshelf did not start. Last lines of app.log:
echo.
if exist "app.log" powershell -NoProfile -Command "Get-Content app.log -Tail 25"
if not exist "app.log" echo   no app.log was written
echo.
echo   Run  run.bat --console  to watch it start in this window.
echo.
pause
exit /b 1

:fallback_console
".venv\Scripts\python.exe" run.py
pause
exit /b 0

:venv_failed
echo.
echo   Could not create the Python environment. The output above says why.
echo.
pause
exit /b 1

:deps_failed
echo.
echo   Dependency install failed. The output above says why.
echo   Most often: no internet, or a proxy or antivirus blocking pip.
echo.
pause
exit /b 1
