@echo off
setlocal EnableExtensions EnableDelayedExpansion
chcp 65001 >nul

title Tamer SEO Analyzer V1.4.2 Kurulum - Kisayol Duzeltildi
set "APP_NAME=Tamer SEO Analyzer"
set "APP_VERSION=1.4.2"
set "INSTALL_DIR=%LOCALAPPDATA%\Programs\Tamer SEO Analyzer"
set "SOURCE_DIR=%~dp0app"
set "STARTMENU=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Tamer SEO Analyzer"

echo ============================================================
echo  Tamer SEO Analyzer V%APP_VERSION% Kurulum
echo  Copyright 2026 tamerserbes.com
echo  Masaustu kisayol olusturma sistemi guncellendi.
echo ============================================================
echo.

if not exist "%SOURCE_DIR%\app.py" (
    echo HATA: Kurulum paketi eksik. app\app.py bulunamadi.
    echo ZIP dosyasini tamamen cikardiginizdan emin olun.
    pause
    exit /b 1
)

where py >nul 2>nul
if not errorlevel 1 (
    set "PY_CMD=py -3"
) else (
    where python >nul 2>nul
    if not errorlevel 1 (
        set "PY_CMD=python"
    ) else (
        echo HATA: Python bulunamadi.
        echo Lutfen Python 3 kurun ve tekrar deneyin.
        echo Indirme: https://www.python.org/downloads/
        pause
        exit /b 1
    )
)

echo Python kontrol edildi: %PY_CMD%
echo Kurulum klasoru: %INSTALL_DIR%
echo.

if exist "%INSTALL_DIR%" (
    echo Onceki kurulum bulundu. Guncelleniyor...
) else (
    mkdir "%INSTALL_DIR%" >nul 2>nul
)

echo Program dosyalari kopyalaniyor...
robocopy "%SOURCE_DIR%" "%INSTALL_DIR%" /E /NFL /NDL /NJH /NJS /NC /NS >nul
if %ERRORLEVEL% GEQ 8 (
    echo HATA: Dosyalar kopyalanamadi.
    pause
    exit /b 1
)

echo Sanal ortam hazirlaniyor...
%PY_CMD% -m venv "%INSTALL_DIR%\.venv"
if not exist "%INSTALL_DIR%\.venv\Scripts\python.exe" (
    echo HATA: Python sanal ortami olusturulamadi.
    pause
    exit /b 1
)

echo Gerekli paketler yukleniyor...
"%INSTALL_DIR%\.venv\Scripts\python.exe" -m pip install --upgrade pip >nul
"%INSTALL_DIR%\.venv\Scripts\pip.exe" install -r "%INSTALL_DIR%\requirements.txt"
if %ERRORLEVEL% NEQ 0 (
    echo HATA: Gerekli Python paketleri yuklenemedi.
    pause
    exit /b 1
)

echo Baslatma dosyasi hazirlaniyor...
(
  echo @echo off
  echo cd /d "%%~dp0"
  echo if exist ".venv\Scripts\pythonw.exe" ^(
  echo     ".venv\Scripts\pythonw.exe" app.py
  echo ^) else ^(
  echo     python app.py
  echo ^)
) > "%INSTALL_DIR%\Tamer SEO Analyzer.bat"

(
  echo @echo off
  echo chcp 65001 ^>nul
  echo title Tamer SEO Analyzer Kaldir
  echo echo Tamer SEO Analyzer kaldirilacak.
  echo echo.
  echo set /p OK=Devam etmek istiyor musunuz? ^(E/H^): 
  echo if /i not "%%OK%%"=="E" exit /b
  echo powershell -NoProfile -ExecutionPolicy Bypass -Command "Remove-Item -LiteralPath ([IO.Path]::Combine([Environment]::GetFolderPath('Desktop'),'Tamer SEO Analyzer.lnk')) -Force -ErrorAction SilentlyContinue; Remove-Item -LiteralPath ([IO.Path]::Combine([Environment]::GetFolderPath('CommonDesktopDirectory'),'Tamer SEO Analyzer.lnk')) -Force -ErrorAction SilentlyContinue"
  echo rmdir /s /q "%%APPDATA%%\Microsoft\Windows\Start Menu\Programs\Tamer SEO Analyzer" ^>nul 2^>nul
  echo cd /d "%%TEMP%%"
  echo rmdir /s /q "%INSTALL_DIR%" ^>nul 2^>nul
  echo echo Program kaldirildi.
  echo pause
) > "%INSTALL_DIR%\Tamer SEO Analyzer Kaldir.bat"

:: Kurulumdan sonra da kullanilabilsin diye manuel kisayol olusturucu kurulum klasorune eklenir.
copy /Y "%~dp0Masaustu_Kisayolu_Olustur.bat" "%INSTALL_DIR%\Masaustu_Kisayolu_Olustur.bat" >nul 2>nul

echo Kisayollar olusturuluyor...
mkdir "%STARTMENU%" >nul 2>nul
set "SHORTCUT_PS=%TEMP%\tamer_seo_create_shortcuts.ps1"
(
  echo param^([string]$InstallDir,[string]$StartMenuDir^)
  echo $ErrorActionPreference = 'Stop'
  echo $Wsh = New-Object -ComObject WScript.Shell
  echo $Desktop = [Environment]::GetFolderPath^('Desktop'^)
  echo if ^([string]::IsNullOrWhiteSpace^($Desktop^)^) { $Desktop = Join-Path $env:USERPROFILE 'Desktop' }
  echo if ^(-not ^(Test-Path -LiteralPath $Desktop^)^) { New-Item -ItemType Directory -Path $Desktop -Force ^| Out-Null }
  echo if ^(-not ^(Test-Path -LiteralPath $StartMenuDir^)^) { New-Item -ItemType Directory -Path $StartMenuDir -Force ^| Out-Null }
  echo $Target = Join-Path $InstallDir 'Tamer SEO Analyzer.bat'
  echo $Icon = Join-Path $InstallDir 'assets\tamer_seo_analyzer.ico'
  echo $DesktopShortcut = Join-Path $Desktop 'Tamer SEO Analyzer.lnk'
  echo $S = $Wsh.CreateShortcut^($DesktopShortcut^)
  echo $S.TargetPath = $Target
  echo $S.WorkingDirectory = $InstallDir
  echo if ^(Test-Path -LiteralPath $Icon^) { $S.IconLocation = $Icon }
  echo $S.Description = 'Tamer SEO Analyzer V1.4.2'
  echo $S.Save^(^)
  echo $MenuShortcut = Join-Path $StartMenuDir 'Tamer SEO Analyzer.lnk'
  echo $M = $Wsh.CreateShortcut^($MenuShortcut^)
  echo $M.TargetPath = $Target
  echo $M.WorkingDirectory = $InstallDir
  echo if ^(Test-Path -LiteralPath $Icon^) { $M.IconLocation = $Icon }
  echo $M.Description = 'Tamer SEO Analyzer V1.4.2'
  echo $M.Save^(^)
  echo $UninstallTarget = Join-Path $InstallDir 'Tamer SEO Analyzer Kaldir.bat'
  echo $UninstallShortcut = Join-Path $StartMenuDir 'Tamer SEO Analyzer Kaldir.lnk'
  echo $U = $Wsh.CreateShortcut^($UninstallShortcut^)
  echo $U.TargetPath = $UninstallTarget
  echo $U.WorkingDirectory = $InstallDir
  echo if ^(Test-Path -LiteralPath $Icon^) { $U.IconLocation = $Icon }
  echo $U.Description = 'Tamer SEO Analyzer kaldirma araci'
  echo $U.Save^(^)
  echo Write-Host "Masaustu kisayolu olusturuldu: $DesktopShortcut"
) > "%SHORTCUT_PS%"

powershell -NoProfile -ExecutionPolicy Bypass -File "%SHORTCUT_PS%" -InstallDir "%INSTALL_DIR%" -StartMenuDir "%STARTMENU%"
if %ERRORLEVEL% NEQ 0 (
    echo UYARI: Otomatik kisayol olusturma sirasinda sorun olustu.
    echo Manuel kisayol icin su dosyayi calistirin:
    echo "%INSTALL_DIR%\Masaustu_Kisayolu_Olustur.bat"
) else (
    echo Masaustu ve Baslat menusu kisayollari olusturuldu.
)

echo.
echo ============================================================
echo  Kurulum tamamlandi.
echo  Masaustune "Tamer SEO Analyzer" kisayolu eklendi.
echo  Kurulum klasoru: %INSTALL_DIR%
echo ============================================================
echo.
set /p RUNNOW=Program simdi acilsin mi? ^(E/H^): 
if /i "%RUNNOW%"=="E" start "" "%INSTALL_DIR%\Tamer SEO Analyzer.bat"

endlocal
exit /b 0
