@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0Resources"

echo.
echo  ==========================================
echo    CGHEVEN Asset Library - Installer
echo  ==========================================
echo.

set "PY="

py -3 --version >nul 2>&1
if not errorlevel 1 (
    set "PY=py -3"
    goto run
)

python --version >nul 2>&1
if not errorlevel 1 (
    set "PY=python"
    goto run
)

echo   Python 3 not found. Trying to install it automatically...
winget --version >nul 2>&1
if errorlevel 1 goto nopy
winget install --id Python.Python.3.11 -e --silent --accept-package-agreements --accept-source-agreements
py -3 --version >nul 2>&1
if not errorlevel 1 (
    set "PY=py -3"
    goto run
)

:nopy
echo.
echo   Could not find or install Python 3 automatically.
echo   Install it from:  https://www.python.org/downloads/
echo   Tick "Add Python to PATH" during setup, then run INSTALL.bat again.
echo.
echo   Press any key to close...
pause >nul
exit /b 1

:run
if not exist "cgheven_installer.py" (
    echo   ERROR: installer files are missing next to INSTALL.bat.
    echo   Keep the whole folder together - INSTALL.bat plus the Resources folder.
    echo   Press any key to close...
    pause >nul
    exit /b 1
)

set "LOG=%~dp0install_log.txt"
echo   Installing CGHEVEN Asset Library, please wait...
echo   copying files and setting up image components - up to a minute
echo.
%PY% cgheven_installer.py --headless > "%LOG%" 2>&1
set "RC=!errorlevel!"
type "%LOG%"
echo.
echo  ==========================================
if "!RC!"=="0" (
    echo   [OK] Done. Now restart DaVinci Resolve and open:
    echo        Workspace ^> Scripts ^> Utility ^> CGHEVEN Asset Library
) else (
    echo   [!] Install had a problem ^(code !RC!^).
    echo       Send install_log.txt from this folder to CGHEVEN support.
)
echo  ==========================================
echo.
echo   Press any key to close...
pause >nul
exit /b !RC!
