@echo off
REM ============================================================
REM Video Concat - build a STANDALONE, portable Windows app.
REM
REM   Output:  dist-portable\VideoConcat\
REM   Usage:   copy that whole folder to any Windows PC and
REM            double-click "Video Concat.exe". ffmpeg is bundled.
REM
REM   Requires on THIS build machine: Node, Rust (cargo),
REM   Visual Studio 2022 C++ build tools, and ffmpeg.
REM ============================================================
setlocal EnableDelayedExpansion
set "PATH=%USERPROFILE%\.cargo\bin;%PATH%"
cd /d "%~dp0"

REM --- 1. Locate ffmpeg.exe + ffprobe.exe --------------------
set "FFMPEG="
set "FFPROBE="
if exist "C:\ffmpeg\bin\ffmpeg.exe"  set "FFMPEG=C:\ffmpeg\bin\ffmpeg.exe"
if exist "C:\ffmpeg\bin\ffprobe.exe" set "FFPROBE=C:\ffmpeg\bin\ffprobe.exe"
if not defined FFMPEG  for /f "delims=" %%i in ('where ffmpeg 2^>nul')  do if not defined FFMPEG  set "FFMPEG=%%i"
if not defined FFPROBE for /f "delims=" %%i in ('where ffprobe 2^>nul') do if not defined FFPROBE set "FFPROBE=%%i"

if not defined FFMPEG  ( echo [ERROR] ffmpeg.exe not found. Install ffmpeg or place it in C:\ffmpeg\bin & pause & exit /b 1 )
if not defined FFPROBE ( echo [ERROR] ffprobe.exe not found. Install ffmpeg or place it in C:\ffmpeg\bin & pause & exit /b 1 )
echo Using ffmpeg:  !FFMPEG!
echo Using ffprobe: !FFPROBE!

REM --- 2. Build the release app ------------------------------
if not exist "node_modules" call npm install
echo Building release (this takes a few minutes the first time)...
call npm run tauri build
if errorlevel 1 ( echo [ERROR] Build failed. & pause & exit /b 1 )

REM --- 3. Assemble the portable folder ----------------------
set "OUT=dist-portable\VideoConcat"
if exist "dist-portable" rmdir /s /q "dist-portable"
mkdir "%OUT%"
copy /y "src-tauri\target\release\video-concat.exe" "%OUT%\Video Concat.exe" >nul
copy /y "!FFMPEG!"  "%OUT%\ffmpeg.exe"  >nul
copy /y "!FFPROBE!" "%OUT%\ffprobe.exe" >nul

echo.
echo ============================================================
echo  DONE. Your standalone app is here:
echo    %CD%\%OUT%
echo.
echo  Copy that whole "VideoConcat" folder to any Windows PC and
echo  double-click "Video Concat.exe". ffmpeg is included, so
echo  there is nothing else to install.
echo ============================================================
pause
