@echo off
setlocal
title The G.I.R.L. System - Installer

set "DIR=%APPDATA%\GIRL\muscle"
set "ROOT=%APPDATA%\GIRL"
set "BASE=https://girlai.live/agent/muscle"
set "ICON_URL=https://girlai.live/agent/GIRL-System.ico"
set "APP_NAME=The G.I.R.L. System"

mkdir "%DIR%" 2>nul
mkdir "%ROOT%" 2>nul

echo Installing %APP_NAME%...
echo Log file: %DIR%\install.log
echo.

powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Invoke-WebRequest -Uri 'https://girlai.live/agent/install-agent.ps1' -OutFile '%DIR%\install-agent.ps1' -UseBasicParsing } catch { if (Test-Path '%~dp0install-agent.ps1') { Copy-Item '%~dp0install-agent.ps1' '%DIR%\install-agent.ps1' -Force } else { throw } }"
if errorlevel 1 goto :fail

powershell -NoProfile -ExecutionPolicy Bypass -File "%DIR%\install-agent.ps1" -Dir "%DIR%" -Root "%ROOT%" -Base "%BASE%" -IconUrl "%ICON_URL%"
if errorlevel 1 goto :fail

if not exist "%DIR%\muscle_agent.py" (
    echo muscle_agent.py missing after install.
    goto :fail
)

echo Stopping old PC agent...
powershell -NoProfile -ExecutionPolicy Bypass -File "%DIR%\stop-agent.ps1" 2>nul

powershell -NoProfile -ExecutionPolicy Bypass -Command "$name='%APP_NAME%'; $d=[Environment]::GetFolderPath('Desktop'); $w=New-Object -ComObject WScript.Shell; $l=$w.CreateShortcut($d+'\'+$name+'.lnk'); $l.TargetPath=$env:APPDATA+'\GIRL\muscle\girl-open.cmd'; $l.WorkingDirectory=$env:APPDATA+'\GIRL\muscle'; $l.Description=$name; $icon=$env:APPDATA+'\GIRL\GIRL-System.ico'; if (Test-Path $icon) { $l.IconLocation=$icon+',0' }; $l.Save()"

echo @echo off> "%DIR%\girl-open.cmd"
echo call "%%APPDATA%%\GIRL\muscle\run-muscle.cmd">> "%DIR%\girl-open.cmd"
echo start chrome "https://girlai.live/members/?product=agent">> "%DIR%\girl-open.cmd"

call "%DIR%\run-muscle.cmd"
start chrome "https://girlai.live/members/?product=agent"

echo.
echo %APP_NAME% installed. PC agent restarted. Chrome opened for sign-in.
pause
exit /b 0

:fail
echo.
echo ========== INSTALL FAILED ==========
echo Read the log for the real reason:
echo   %DIR%\install.log
echo.
if exist "%DIR%\install.log" type "%DIR%\install.log"
echo ====================================
pause
exit /b 1
