@echo off
title KAPI IP Santral - Resmi Masaustu Kurulum Sihirbazi
color 0B

set "SANTRAL_URL=https://ip.kapigroup.com.tr"
set "APP_DIR=%LOCALAPPDATA%\KAPI_Santral"
set "UNINSTALL_KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\KAPI_Santral"

echo ======================================================================
echo   KAPI IP SANTRAL - KURUMSAL MASAUSTU KURULUMU (v1.2.1)
echo ======================================================================
echo.

if not exist "%APP_DIR%" mkdir "%APP_DIR%"

echo [1/5] Onceki arka plan telefon ajani kontrol ediliyor...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-CimInstance Win32_Process -Filter \"Name = 'powershell.exe'\" | Where-Object { $_.CommandLine -match 'kapi_santral_pc_agent' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }" >nul 2>&1

echo [2/5] Resmi KAPI IP Santral logosu, ajan ve kaldirma araci indiriliyor...
curl.exe -k -f -s -L "%SANTRAL_URL%/api/santral/logo-192" -o "%APP_DIR%\kapi_logo.png"
curl.exe -k -f -s -L "%SANTRAL_URL%/api/santral/logo-ico" -o "%APP_DIR%\kapi_santral.ico"
if not exist "%APP_DIR%\kapi_santral.ico" (
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%SANTRAL_URL%/api/santral/logo-ico' -UseBasicParsing -OutFile '%APP_DIR%\kapi_santral.ico'"
)
curl.exe -k -f -s -L "%SANTRAL_URL%/santral-pc-ajan" -o "%APP_DIR%\kapi_santral_pc_agent.ps1"
if not exist "%APP_DIR%\kapi_santral_pc_agent.ps1" (
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%SANTRAL_URL%/santral-pc-ajan' -UseBasicParsing -OutFile '%APP_DIR%\kapi_santral_pc_agent.ps1'"
)
curl.exe -k -f -s -L "%SANTRAL_URL%/santral-kaldir" -o "%APP_DIR%\Kaldir.cmd"
if not exist "%APP_DIR%\Kaldir.cmd" (
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%SANTRAL_URL%/santral-kaldir' -UseBasicParsing -OutFile '%APP_DIR%\Kaldir.cmd'"
)

echo [3/5] Sessiz Acilis Baslaticisi olusturuluyor...
(
echo Set WshShell = CreateObject^("WScript.Shell"^)
echo WshShell.Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File ""%APP_DIR%\kapi_santral_pc_agent.ps1""", 0, False
) > "%APP_DIR%\KAPI_Santral_SilentStart.vbs"

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "KAPI_Santral_PC" /t REG_SZ /d "wscript.exe \"%APP_DIR%\KAPI_Santral_SilentStart.vbs\"" /f >nul

echo [4/5] Denetim Masasi (Programlar ve Ozellikler) ve Logolu Kisayollar kaydediliyor...
reg add "%UNINSTALL_KEY%" /v "DisplayName" /t REG_SZ /d "KAPI IP Santral" /f >nul
reg add "%UNINSTALL_KEY%" /v "DisplayIcon" /t REG_SZ /d "%APP_DIR%\kapi_santral.ico,0" /f >nul
reg add "%UNINSTALL_KEY%" /v "Publisher" /t REG_SZ /d "KAPI Group" /f >nul
reg add "%UNINSTALL_KEY%" /v "DisplayVersion" /t REG_SZ /d "1.2.1" /f >nul
reg add "%UNINSTALL_KEY%" /v "InstallLocation" /t REG_SZ /d "%APP_DIR%" /f >nul
reg add "%UNINSTALL_KEY%" /v "URLInfoAbout" /t REG_SZ /d "%SANTRAL_URL%" /f >nul
reg add "%UNINSTALL_KEY%" /v "UninstallString" /t REG_SZ /d "cmd.exe /c \"%APP_DIR%\Kaldir.cmd\"" /f >nul
reg add "%UNINSTALL_KEY%" /v "QuietUninstallString" /t REG_SZ /d "cmd.exe /c \"%APP_DIR%\Kaldir.cmd\" /quiet" /f >nul
reg add "%UNINSTALL_KEY%" /v "NoModify" /t REG_DWORD /d 1 /f >nul
reg add "%UNINSTALL_KEY%" /v "NoRepair" /t REG_DWORD /d 1 /f >nul
reg add "%UNINSTALL_KEY%" /v "EstimatedSize" /t REG_DWORD /d 4096 /f >nul

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ws = New-Object -ComObject WScript.Shell; $desk = [Environment]::GetFolderPath('Desktop'); Remove-Item (Join-Path $desk 'KAPI Santral.lnk') -Force -ErrorAction SilentlyContinue; $ico = Join-Path $env:LOCALAPPDATA 'KAPI_Santral\kapi_santral.ico'; $targetArgs = '-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "' + $env:LOCALAPPDATA + '\KAPI_Santral\kapi_santral_pc_agent.ps1" -OpenPhoneNow'; foreach ($lnkPath in @((Join-Path $desk 'KAPI IP Santral.lnk'), (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\KAPI IP Santral.lnk'))) { $lnk = $ws.CreateShortcut($lnkPath); $lnk.TargetPath = 'powershell.exe'; $lnk.Arguments = $targetArgs; $lnk.WorkingDirectory = $env:TEMP; $lnk.IconLocation = $ico + ',0'; $lnk.Description = 'KAPI IP Santral - Kurumsal Sesli ve Goruntulu Telefon'; $lnk.Save() }"
ie4uinit.exe -show >nul 2>&1

echo [5/5] KAPI IP Santral simdi baslatiliyor...
start "" wscript.exe "%APP_DIR%\KAPI_Santral_SilentStart.vbs"
powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "%APP_DIR%\kapi_santral_pc_agent.ps1" -OpenPhoneNow

echo.
echo ======================================================================
echo   [OK] KAPI IP SANTRAL KURULUMU TAMAMLANDI!
echo   - Masaustune ve Baslat Menusune resmi logolu "KAPI IP Santral" eklendi.
echo   - Denetim Masasi (Programlar ve Ozellikler) kaydi olusturuldu.
echo ======================================================================
timeout /t 3
exit /b 0
