#Requires -Version 5.1
<#
.SYNOPSIS
    Installe Cyclope — Filtre de confidentialité.
    Pas d'élévation UAC requise (installation dans %LOCALAPPDATA%).
#>
param(
    [switch]$NoStartup    # Ne pas créer la tâche de démarrage automatique
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

$AppName    = "Cyclope"
$InstallDir = "$env:LOCALAPPDATA\Cyclope"
$ScriptDir  = Split-Path -Parent $MyInvocation.MyCommand.Path

Write-Host "==> Installation de $AppName"

# 1. Créer les dossiers
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
New-Item -ItemType Directory -Force -Path "$env:APPDATA\Cyclope" | Out-Null

# 2. Copier le binaire (autoporté — aucune DLL à déployer)
$src = Join-Path $ScriptDir "cyclope.exe"
if (Test-Path $src) {
    Copy-Item -Path $src -Destination $InstallDir -Force
    Write-Host "   OK cyclope.exe"
} else {
    Write-Error "cyclope.exe introuvable dans $ScriptDir — exécuter build.ps1 d'abord"
}

# 3. Raccourci Bureau
$desktop  = [Environment]::GetFolderPath("Desktop")
$wsh = New-Object -ComObject WScript.Shell
$lnk = $wsh.CreateShortcut("$desktop\Cyclope.lnk")
$lnk.TargetPath       = "$InstallDir\cyclope.exe"
$lnk.WorkingDirectory = $InstallDir
$lnk.Description      = "Cyclope — Filtre de confidentialite"
$lnk.IconLocation     = "$InstallDir\cyclope.exe,0"
$lnk.Save()
Write-Host "   OK Raccourci Bureau"

# 4. Raccourci Menu Demarrer
$startMenu = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs"
New-Item -ItemType Directory -Force -Path "$startMenu\Cyclope" | Out-Null
$lnk2 = $wsh.CreateShortcut("$startMenu\Cyclope\Cyclope.lnk")
$lnk2.TargetPath       = "$InstallDir\cyclope.exe"
$lnk2.WorkingDirectory = $InstallDir
$lnk2.Description      = "Cyclope — Filtre de confidentialite"
$lnk2.IconLocation     = "$InstallDir\cyclope.exe,0"
$lnk2.Save()
Write-Host "   OK Menu Demarrer"

# 5. Task Scheduler — demarrage automatique SANS UAC
if (-not $NoStartup) {
    $action   = New-ScheduledTaskAction -Execute "$InstallDir\cyclope.exe" -WorkingDirectory $InstallDir
    $trigger  = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
    $settings = New-ScheduledTaskSettingsSet `
        -AllowStartIfOnBatteries `
        -DontStopIfGoingOnBatteries `
        -ExecutionTimeLimit (New-TimeSpan -Hours 0) `
        -MultipleInstances IgnoreNew

    Register-ScheduledTask `
        -TaskName $AppName `
        -TaskPath "\Privateer\" `
        -Action $action `
        -Trigger $trigger `
        -Settings $settings `
        -RunLevel Limited `
        -Force | Out-Null
    Write-Host "   OK Tache planifiee (demarrage automatique)"
}

# 6. Demarrer l'application si pas deja en cours
$proc = Get-Process "cyclope" -ErrorAction SilentlyContinue
if (-not $proc) {
    Start-Process -FilePath "$InstallDir\cyclope.exe" -WorkingDirectory $InstallDir
    Write-Host "   OK Cyclope demarre"
}

Write-Host ""
Write-Host "Installation terminee dans : $InstallDir"
Write-Host "  Config : $env:APPDATA\Cyclope\config.toml"
Write-Host "  Logs   : $env:LOCALAPPDATA\Cyclope\cyclope.log"
Write-Host ""
