@echo off
setlocal
rem ============================================================
rem  SovrGPT fuer Excel - Installation
rem  Doppelklick genuegt. Keine Administratorrechte noetig.
rem  Es wird nichts installiert und nichts an Windows veraendert
rem  ausser einem Eintrag im eigenen Benutzerprofil.
rem ============================================================
title SovrGPT fuer Excel - Installation
echo.
echo   SovrGPT fuer Excel wird eingerichtet ...
echo.

powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "$ErrorActionPreference='Stop';" ^
  "try {" ^
  "  $dir = Join-Path $env:LOCALAPPDATA 'SovrGPT\Excel';" ^
  "  if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null };" ^
  "  $m = Join-Path $dir 'sovrgpt-excel-manifest.xml';" ^
  "  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;" ^
  "  Invoke-WebRequest -Uri 'https://sovrgpt.com/xl/manifest.xml' -OutFile $m -UseBasicParsing;" ^
  "  if (-not (Select-String -Path $m -Pattern '<OfficeApp' -Quiet)) { throw 'Die geladene Datei ist kein Add-in-Manifest.' };" ^
  "  $dev = 'HKCU:\Software\Microsoft\Office\16.0\WEF\Developer';" ^
  "  if (-not (Test-Path $dev)) { New-Item -Path $dev -Force | Out-Null };" ^
  "  $id = 'd420d2ae-ff0d-4598-a530-20f20c612f8a';" ^
  "  foreach ($x in (Get-ItemProperty -Path $dev).PSObject.Properties) {" ^
  "    if ($x.Name -notlike 'PS*' -and $x.Name -ne $id -and ('' + $x.Value) -like '*SovrGPT*') { Remove-ItemProperty -Path $dev -Name $x.Name -Force }" ^
  "  };" ^
  "  New-ItemProperty -Path $dev -Name $id -Value $m -PropertyType String -Force | Out-Null;" ^
  "  $back = (Get-ItemProperty -Path $dev).$id;" ^
  "  if ($back -ne $m) { throw 'Der Eintrag konnte nicht geschrieben werden.' };" ^
  "  Write-Host '   Fertig.' -ForegroundColor Green;" ^
  "  Write-Host ('   Manifest: ' + $m);" ^
  "} catch {" ^
  "  Write-Host '   FEHLGESCHLAGEN:' -ForegroundColor Red;" ^
  "  Write-Host ('   ' + $_.Exception.Message);" ^
  "  exit 1;" ^
  "}"

if errorlevel 1 goto :fehler

echo.
echo   ------------------------------------------------------------
echo    Noch drei Schritte:
echo.
echo    1. Excel VOLLSTAENDIG schliessen (alle Fenster) und neu
echo       oeffnen.
echo.
echo    2. WICHTIG - diesen Schritt uebersieht man leicht:
echo       In Excel auf "Start" - "Add-Ins" klicken, dort
echo       "SovrGPT fuer Excel" auswaehlen und "Hinzufuegen"
echo       druecken. Das ist nur EINMAL noetig.
echo.
echo    3. Fertig. Der Knopf "Assistent oeffnen" steht ab jetzt
echo       dauerhaft im Register "Start" in der Gruppe "SovrGPT".
echo.
echo    Ohne Schritt 2 passiert nichts sichtbares - diese Datei
echo    macht das Add-in fuer Excel nur verfuegbar, eingeblendet
echo    wird es erst durch das Hinzufuegen.
echo   ------------------------------------------------------------
echo.
echo   Zum Entfernen: SovrGPT-fuer-Excel-entfernen.cmd ausfuehren.
echo.
pause
exit /b 0

:fehler
echo.
echo   Die Einrichtung ist fehlgeschlagen. Haeufigste Ursachen:
echo     - keine Internetverbindung zu sovrgpt.com
echo     - eine Unternehmensrichtlinie blockiert Add-ins
echo.
echo   Bitte diese Meldung an die IT weitergeben.
echo.
pause
exit /b 1
