@echo off
setlocal EnableExtensions
REM ==================================================================
REM  SoundStudio Helper setup (Windows 10/11, 64-bit)
REM  Run it on its own (it downloads the helper files) or from an unzipped
REM  folder that has them. Installs into %USERPROFILE%\SoundStudioHelper
REM  Nothing is deleted. Safe to run again (it repairs or updates).
REM  Optional: install.cmd /nostartup   (do not start with Windows)
REM ==================================================================
set "SRC=%~dp0"
set "URL=https://jeffapierson.com/journal_tools/soundstudio/helper"
REM Use files next to this script only when the whole helper folder is here (unzipped copy)
set "LOCAL="
if exist "%SRC%analyzer\engine.js" if exist "%SRC%server.py" set "LOCAL=1"
set "H=%USERPROFILE%\SoundStudioHelper"
if not "%SS_HELPER_DIR%"=="" set "H=%SS_HELPER_DIR%"
echo.
echo  SoundStudio Helper setup
echo  Installing into: %H%
echo  This downloads about 1.5 GB the first time and can take 5 to 15 minutes.
echo.
if not exist "%H%" mkdir "%H%"
if not exist "%H%\analyzer" mkdir "%H%\analyzer"
if not exist "%H%\models\rnnoise" mkdir "%H%\models\rnnoise"

echo [1/6] Checking winget...
where winget >nul 2>&1 || (echo  winget not found. Install "App Installer" from the Microsoft Store, then run this again. & goto :fail)

echo [2/6] FFmpeg (full build, includes Rubber Band and RNNoise)...
where ffmpeg >nul 2>&1 || winget install -e --id Gyan.FFmpeg --accept-source-agreements --accept-package-agreements
where ffmpeg >nul 2>&1 || if exist "%LOCALAPPDATA%\Microsoft\WinGet\Links\ffmpeg.exe" set "PATH=%LOCALAPPDATA%\Microsoft\WinGet\Links;%PATH%"
where ffmpeg >nul 2>&1 || (echo  FFmpeg did not install. Close this window, open a new one and run setup again. & goto :fail)

echo [3/6] uv (Python installer)...
where uv >nul 2>&1 || winget install -e --id astral-sh.uv --accept-source-agreements --accept-package-agreements
where uv >nul 2>&1 || if exist "%LOCALAPPDATA%\Microsoft\WinGet\Links\uv.exe" set "PATH=%LOCALAPPDATA%\Microsoft\WinGet\Links;%PATH%"
where uv >nul 2>&1 || (echo  uv did not install. Close this window, open a new one and run setup again. & goto :fail)

echo [4/6] Node.js (for the click analyzer in the watch folder, optional)...
where node >nul 2>&1 || winget install -e --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements

echo [5/6] Helper files, Python 3.11 and the AI tools (Demucs, Whisper, DeepFilterNet)...
call :getfile server.py.txt "server.py" || goto :fail
call :getfile README.txt "README.txt" || goto :fail
call :getfile start_helper.cmd "Start SoundStudio Helper.cmd" || goto :fail
call :getfile engine.js "analyzer\engine.js" || goto :fail
call :getfile analyze_song.js "analyzer\analyze_song.js" || goto :fail
if /i not "%~f0"=="%H%\install.cmd" copy /y "%~f0" "%H%\install.cmd" >nul
if exist "%SRC%models\rnnoise\sh.rnnn" if /i not "%SRC%"=="%H%\" copy /y "%SRC%models\rnnoise\sh.rnnn" "%H%\models\rnnoise\" >nul
pushd "%H%"
if not exist ".venv\Scripts\python.exe" uv venv --python 3.11 .venv || goto :failpop
uv pip install --python .venv\Scripts\python.exe "torch==2.0.1" "torchaudio==2.0.2" --index-url https://download.pytorch.org/whl/cpu || goto :failpop
uv pip install --python .venv\Scripts\python.exe "numpy<2" "demucs==4.0.1" soundfile faster-whisper "deepfilternet==0.5.6" || goto :failpop
if not exist "models\rnnoise\sh.rnnn" powershell -NoProfile -Command "Invoke-WebRequest -UseBasicParsing 'https://raw.githubusercontent.com/GregorR/rnnoise-models/master/somnolent-hogwash-2018-09-01/sh.rnnn' -OutFile 'models\rnnoise\sh.rnnn'"
popd

echo [6/6] Start with Windows and start now...
if /i not "%~1"=="/nostartup" powershell -NoProfile -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut([Environment]::GetFolderPath('Startup')+'\SoundStudio Helper.lnk'); $s.TargetPath='%H%\.venv\Scripts\pythonw.exe'; $s.Arguments='\"%H%\server.py\"'; $s.WorkingDirectory='%H%'; $s.Description='SoundStudio Helper'; $s.Save()"
if /i not "%~1"=="/nostartup" start "" /b "%H%\.venv\Scripts\pythonw.exe" "%H%\server.py"
echo.
echo  Done. Open SoundStudio, click the Helper tab, and choose Allow if Chrome asks
echo  to let the site reach apps on this device. Status page: http://127.0.0.1:8737/
echo.
pause
exit /b 0
:getfile
REM %1 = name on the website, %2 = path inside the helper folder
if defined LOCAL if exist "%SRC%%~2" (
  if /i "%SRC%"=="%H%\" exit /b 0
  copy /y "%SRC%%~2" "%H%\%~2" >nul
  exit /b 0
)
powershell -NoProfile -Command "try { Invoke-WebRequest -UseBasicParsing '%URL%/%~1' -OutFile '%H%\%~2'; exit 0 } catch { Write-Host ('  Could not download %~1: ' + $_.Exception.Message); exit 1 }"
exit /b %errorlevel%
:failpop
popd
:fail
echo.
echo  Setup stopped. Nothing was removed. Fix the message above and run setup again.
pause
exit /b 1
