
@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem ============================================
rem  PPTX Image Compressor - Installer/Runner (Batch-enabled)
rem  Fix: caesiumclt.exe aus [ROOT]\bin; Python-Discovery ohne MS Store Alias
rem ============================================

set "APP_NAME=PPTX Image Compressor"
set "SELF_DIR=%~dp0"
set "SCRIPT=%SELF_DIR%pptx_image_compress.py"

rem ---- Python Embeddable config ----
set "PY_EMBED_VERSION=3.14.5"
set "PY_EMBED_ZIP=python-%PY_EMBED_VERSION%-embed-amd64.zip"
set "PY_EMBED_URL=https://www.python.org/ftp/python/%PY_EMBED_VERSION%/%PY_EMBED_ZIP%"
set "PY_DIR=%SELF_DIR%python-embed"
set "PY_EXE=%PY_DIR%\python.exe"

rem ---- CaesiumCLT discovery (prefer [ROOT]\bin) ----
set "CAE_DIR=%SELF_DIR%bin"
set "CAE_EXE=caesiumclt.exe"
if exist "%CAE_DIR%\%CAE_EXE%" (
    set "PATH=%CAE_DIR%;%PATH%"
) else (    
    if exist "%SELF_DIR%%CAE_EXE%" (
        set "PATH=%SELF_DIR%;%PATH%"
    ) else (
        where /q %CAE_EXE%
        if errorlevel 1 (
            echo [ERROR] ^> 'caesiumclt.exe' nicht gefunden.
            echo         Lege 'caesiumclt.exe' in '%CAE_DIR%' oder neben diese BAT,
            echo         oder sorge dafuer, dass es im PATH liegt.
            exit /b 2
        )
    )
)

rem ---- Determine ESC for ANSI (green check) ----
for /f "delims=" %%A in ('echo prompt $E^| cmd') do set "ESC=%%A"

rem ---- Python discovery (avoid MS Store alias) ----
set "PY_CMD="
set "USE_PY_LAUNCHER="

rem 1) Prefer local embeddable first
if exist "%PY_EXE%" (
    set "PY_CMD=%PY_EXE%"
    goto :have_python
)

rem 2) Real python.exe in PATH (exclude WindowsApps alias)
for /f "delims=" %%P in ('where python.exe 2^>nul') do (
    echo %%P | find /I "WindowsApps" >nul
    if errorlevel 1 (
        set "PY_CMD=%%P"
        goto :have_python
    )
)
for /f "delims=" %%P in ('where python3.exe 2^>nul') do (
    echo %%P | find /I "WindowsApps" >nul
    if errorlevel 1 (
        set "PY_CMD=%%P"
        goto :have_python
    )
)

rem 3) Python launcher py.exe (exclude WindowsApps)
for /f "delims=" %%P in ('where py.exe 2^>nul') do (
    echo %%P | find /I "WindowsApps" >nul
    if errorlevel 1 (
        set "PY_CMD=%%P"
        set "USE_PY_LAUNCHER=1"
        goto :have_python
    )
)

rem 4) Download embeddable locally
if not exist "%SELF_DIR%%PY_EMBED_ZIP%" (
    echo [INFO] Kein Python gefunden. Lade Embeddable Python %PY_EMBED_VERSION% ...
    powershell -NoLogo -NoProfile -Command ^
     "try { Invoke-WebRequest -Uri '%PY_EMBED_URL%' -OutFile '%SELF_DIR%%PY_EMBED_ZIP%' -UseBasicParsing; exit 0 } catch { Write-Error $_; exit 1 }"
)
if not exist "%SELF_DIR%%PY_EMBED_ZIP%" (
    echo [ERROR] Embeddable-Python ZIP nicht vorhanden. Abbruch.
    exit /b 3
)

echo [INFO] Entpacke nach "%PY_DIR%" ...
if exist "%PY_DIR%" rmdir /s /q "%PY_DIR%"
mkdir "%PY_DIR%" >nul 2>&1
powershell -NoLogo -NoProfile -Command ^
 "Expand-Archive -Path '%SELF_DIR%%PY_EMBED_ZIP%' -DestinationPath '%PY_DIR%' -Force"
if errorlevel 1 (
    echo [ERROR] Konnte ZIP nicht entpacken. Abbruch.
    exit /b 4
)
set "PY_CMD=%PY_EXE%"

:have_python
if not defined PY_CMD (
    echo [ERROR] Konnte Python nicht ermitteln. Abbruch.
    exit /b 5
)

rem ---- Verify script presence ----
if not exist "%SCRIPT%" (
    echo [ERROR] Script nicht gefunden: "%SCRIPT%"
    exit /b 6
)

set "RUN_ARGS=%*"
if "%~1"=="" set "RUN_ARGS=-h"

echo.
echo [%APP_NAME%] Starte ...
echo Command: "%PY_CMD%" "%SCRIPT%" %RUN_ARGS%
echo.
"%PY_CMD%" "%SCRIPT%" %RUN_ARGS%
set "RC=%ERRORLEVEL%"

echo.
if "%RC%"=="0" (
    echo Fertig.
) else (
    echo [ERROR] Prozess endete mit Code %RC%.
)

endlocal & exit /b %RC%
