@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"

set EXTENSION_ID=videoPlus
set ZXP_FILE=%~dp0%EXTENSION_ID%.zxp
set INSTALL_DIR=%APPDATA%\Adobe\CEP\extensions\%EXTENSION_ID%

echo.
echo ============================================
echo   videoPlus Installer (Windows)
echo ============================================
echo.

if not exist "%ZXP_FILE%" (
    echo [ERROR] ZXP file not found: %ZXP_FILE%
    echo.
    pause
    exit /b 1
)

echo [INFO] ZXP: %ZXP_FILE%
echo [INFO] Install path: %INSTALL_DIR%
echo.

tasklist /FI "IMAGENAME eq Adobe Premiere Pro.exe" 2>nul | find /i "Adobe Premiere Pro.exe" >nul
if %errorlevel% equ 0 (
    echo [WARNING] Adobe Premiere Pro is running.
    echo.
    set /p "choice=Close Premiere Pro? (y/n): "
    if /i "!choice!"=="y" (
        echo [INFO] Closing Premiere Pro...
        taskkill /IM "Adobe Premiere Pro.exe" /F >nul 2>&1
        timeout /t 3 /nobreak >nul
    ) else (
        echo [INFO] Please close Premiere Pro and try again.
        pause
        exit /b 1
    )
)

if exist "%INSTALL_DIR%" (
    echo [INFO] Removing previous version...
    rd /s /q "%INSTALL_DIR%"
    echo [OK] Removed
)

mkdir "%INSTALL_DIR%" 2>nul

echo [INFO] Installing extension...
copy /y "%ZXP_FILE%" "%temp%\videoPlus_tmp.zip" >nul
powershell -Command "Expand-Archive -Path '%temp%\videoPlus_tmp.zip' -DestinationPath '%INSTALL_DIR%' -Force" 2>nul

if %errorlevel% neq 0 (
    echo [INFO] PowerShell failed, trying tar...
    tar -xf "%ZXP_FILE%" -C "%INSTALL_DIR%" 2>nul

    if %errorlevel% neq 0 (
        echo [ERROR] Failed to extract ZXP!
        pause
        exit /b 1
    )
)
del "%temp%\videoPlus_tmp.zip" >nul 2>&1

if exist "%INSTALL_DIR%\mimetype" del "%INSTALL_DIR%\mimetype" 2>nul

echo [INFO] Setting PlayerDebugMode...
for %%v in (9 10 11 12) do (
    reg add "HKCU\SOFTWARE\Adobe\CSXS.%%v" /v PlayerDebugMode /t REG_SZ /d 1 /f >nul 2>&1
    echo   [OK] CSXS.%%v PlayerDebugMode = 1
)

echo.
if exist "%INSTALL_DIR%\CSXS\manifest.xml" (
    echo ============================================
    echo   Install complete!
    echo ============================================
    echo.
    echo   Restart Adobe Premiere Pro, then go to
    echo   Window - Extensions - videoPlus
) else (
    echo [ERROR] Installation may have failed.
    echo         Check: %INSTALL_DIR%\CSXS\manifest.xml
)

echo.
pause
