@echo off
title Installazione EGW-3CX Bridge
echo.
echo ========================================
echo   EGW-3CX Bridge - Installazione
echo ========================================
echo.

:: Cartella di installazione in AppData (non richiede admin)
set "INSTALL_DIR=%LocalAppData%\Egw3cxApp\bin"
set "SHORTCUT_NAME=EGW-3CX Bridge"

:: Chiudi eventuale istanza in esecuzione
taskkill /IM Egw3cxApp.exe /F >nul 2>&1
timeout /t 2 /nobreak >nul

:: Crea cartella
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"

:: Copia l'exe dalla stessa cartella dello script
echo Copia file in %INSTALL_DIR%...
copy /Y "%~dp0Egw3cxApp.exe" "%INSTALL_DIR%\Egw3cxApp.exe" >nul
if exist "%~dp0Resources\icon.ico" (
    if not exist "%INSTALL_DIR%\Resources" mkdir "%INSTALL_DIR%\Resources"
    copy /Y "%~dp0Resources\icon.ico" "%INSTALL_DIR%\Resources\icon.ico" >nul
)

:: Crea collegamento sul Desktop
echo Creazione collegamento sul Desktop...
powershell -NoProfile -Command ^
  "$ws = New-Object -ComObject WScript.Shell; ^
   $sc = $ws.CreateShortcut([IO.Path]::Combine([Environment]::GetFolderPath('Desktop'), '%SHORTCUT_NAME%.lnk')); ^
   $sc.TargetPath = '%INSTALL_DIR%\Egw3cxApp.exe'; ^
   $sc.WorkingDirectory = '%INSTALL_DIR%'; ^
   $sc.Description = 'EGW-3CX Bridge - Integrazione EGroupware e 3CX'; ^
   $sc.IconLocation = '%INSTALL_DIR%\Egw3cxApp.exe,0'; ^
   $sc.Save()"

:: Crea collegamento nel menu Start
set "STARTMENU=%AppData%\Microsoft\Windows\Start Menu\Programs"
echo Creazione collegamento nel Menu Start...
powershell -NoProfile -Command ^
  "$ws = New-Object -ComObject WScript.Shell; ^
   $sc = $ws.CreateShortcut([IO.Path]::Combine('%STARTMENU%', '%SHORTCUT_NAME%.lnk')); ^
   $sc.TargetPath = '%INSTALL_DIR%\Egw3cxApp.exe'; ^
   $sc.WorkingDirectory = '%INSTALL_DIR%'; ^
   $sc.Description = 'EGW-3CX Bridge - Integrazione EGroupware e 3CX'; ^
   $sc.IconLocation = '%INSTALL_DIR%\Egw3cxApp.exe,0'; ^
   $sc.Save()"

:: Registra avvio automatico con Windows
echo Registrazione avvio automatico...
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Egw3cxApp" /t REG_SZ /d "\"%INSTALL_DIR%\Egw3cxApp.exe\" --minimized" /f >nul

:: Registra per disinstallazione (senza admin, in HKCU)
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "DisplayName" /t REG_SZ /d "EGW-3CX Bridge" /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "UninstallString" /t REG_SZ /d "\"%INSTALL_DIR%\uninstall.bat\"" /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "Publisher" /t REG_SZ /d "AT Srl" /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "DisplayVersion" /t REG_SZ /d "1.0.0" /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "NoModify" /t REG_DWORD /d 1 /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /v "NoRepair" /t REG_DWORD /d 1 /f >nul

:: Crea script di disinstallazione
echo @echo off > "%INSTALL_DIR%\uninstall.bat"
echo title Disinstallazione EGW-3CX Bridge >> "%INSTALL_DIR%\uninstall.bat"
echo taskkill /IM Egw3cxApp.exe /F ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo timeout /t 2 /nobreak ^>nul >> "%INSTALL_DIR%\uninstall.bat"
echo reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Egw3cxApp" /f ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Egw3cxApp" /f ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo del "%AppData%\Microsoft\Windows\Start Menu\Programs\%SHORTCUT_NAME%.lnk" ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo del "%UserProfile%\Desktop\%SHORTCUT_NAME%.lnk" ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo rd /s /q "%INSTALL_DIR%" ^>nul 2^>^&1 >> "%INSTALL_DIR%\uninstall.bat"
echo echo Disinstallazione completata. >> "%INSTALL_DIR%\uninstall.bat"
echo pause >> "%INSTALL_DIR%\uninstall.bat"

echo.
echo ========================================
echo   Installazione completata!
echo ========================================
echo.
echo   Installato in: %INSTALL_DIR%
echo   Collegamento: Desktop + Menu Start
echo   Avvio automatico: attivato
echo.
echo   Per disinstallare: Impostazioni ^> App
echo   oppure eseguire: %INSTALL_DIR%\uninstall.bat
echo.

:: Avvia l'applicazione
echo Avvio EGW-3CX Bridge...
start "" "%INSTALL_DIR%\Egw3cxApp.exe"

pause
