@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
title VibeEdit - Cai dat

set "BUNDLEID=com.vibeedit.panel"
set "EXTROOT=%APPDATA%\Adobe\CEP\extensions"
set "TARGET=%EXTROOT%\VibeEdit"
set "PAYLOAD=%~dp0VibeEdit"

echo.
echo   ========================================
echo     VibeEdit - Cai dat / Cap nhat
echo   ========================================
echo.

REM --- 0. Kiem tra payload ton tai ---
if not exist "%PAYLOAD%\CSXS\manifest.xml" (
  echo   [LOI] Khong thay thu muc VibeEdit ben canh install.bat.
  echo         Hay giai nen TOAN BO file zip roi chay lai install.bat.
  echo.
  pause
  exit /b 1
)

REM --- 1. Yeu cau dong Premiere (khong tu kill de tranh mat viec chua luu) ---
:checkpr
tasklist /fi "imagename eq Adobe Premiere Pro.exe" 2>nul | find /i "Adobe Premiere Pro.exe" >nul
if not errorlevel 1 (
  echo   [!] Premiere Pro dang mo. Vui long DONG han Premiere.
  echo       Sau khi dong, nhan phim bat ky de tiep tuc...
  pause >nul
  goto checkpr
)
REM engine con (neu co) - dong an toan
taskkill /f /im vibeedit-engine.exe >nul 2>&1

REM --- 2. Bat PlayerDebugMode (HKCU, khong can admin, khong UAC) ---
echo   - Bat che do nap extension (PlayerDebugMode)...
reg add "HKCU\Software\Adobe\CSXS.11" /v PlayerDebugMode /t REG_SZ /d 1 /f >nul 2>&1
reg add "HKCU\Software\Adobe\CSXS.12" /v PlayerDebugMode /t REG_SZ /d 1 /f >nul 2>&1
reg add "HKCU\Software\Adobe\CSXS.13" /v PlayerDebugMode /t REG_SZ /d 1 /f >nul 2>&1

REM --- 3. Go MOI ban cu co cung BundleId (bat ke ten thu muc) ---
echo   - Kiem tra va go ban cu...
if exist "%EXTROOT%" (
  for /d %%D in ("%EXTROOT%\*") do (
    if exist "%%D\CSXS\manifest.xml" (
      findstr /c:"%BUNDLEID%" "%%D\CSXS\manifest.xml" >nul 2>&1
      if not errorlevel 1 (
        echo       go: %%~nxD
        rd /s /q "%%D"
      )
    )
  )
)

REM --- 4. Copy ban moi ---
echo   - Cai ban moi vao: %TARGET%
if not exist "%EXTROOT%" mkdir "%EXTROOT%"
robocopy "%PAYLOAD%" "%TARGET%" /E /NFL /NDL /NJH /NJS /NC /NS >nul
if errorlevel 8 (
  echo.
  echo   [LOI] Copy that bai. Kiem tra quyen ghi vao %EXTROOT%.
  echo.
  pause
  exit /b 1
)

echo.
echo   ========================================
echo     HOAN TAT!
echo   ========================================
echo.
echo   Mo Premiere Pro - Window - Extensions - VibeEdit.
echo.
echo   Ho tro / thao luan (Zalo):
echo   https://zalo.me/g/sa9rpbqorwsw82u1qnze
echo.
pause
exit /b 0
