; Medi-WOL Windows Installer Script
; Erstellt mit Inno Setup 6

#define MyAppName "Medi-WOL"
#define MyAppVersion "1.0.0"
#define MyAppPublisher "medisoftware"
#define MyAppExeName "medi-wol-windows-amd64.exe"
#define MyAppServiceName "Medi-WOL"
#define MyAppServiceDisplayName "Medi-WOL Wake-on-LAN Manager"
#define MyAppServiceDescription "Web-basierter Wake-on-LAN Manager für PC-Verwaltung"

[Setup]
AppId={{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL=https://www.medisoftware.de/
DefaultDirName={autopf}\{#MyAppName}
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
LicenseFile=..\LICENSE
OutputDir=..\dist
OutputBaseFilename=medi-wol-setup
SetupIconFile=medi-wol.ico
UninstallDisplayIcon={app}\{#MyAppExeName}
Compression=lzma
SolidCompression=yes
WizardStyle=modern
PrivilegesRequired=admin
ArchitecturesAllowed=x64
ArchitecturesInstallIn64BitMode=x64

[Languages]
Name: "german"; MessagesFile: "compiler:Languages\German.isl"
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 6.1; Check: not IsAdminInstallMode
Name: "startservice"; Description: "{cm:StartServiceAfterInstall}"; GroupDescription: "{cm:ServiceOptions}"

[Files]
Source: "..\dist\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "nssm.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\web\static\*"; DestDir: "{app}\web\static"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\web\templates\*"; DestDir: "{app}\web\templates"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\LICENSE"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\README.md"; DestDir: "{app}"; Flags: ignoreversion

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon

[Run]
; Installiere Medi-WOL als Windows-Dienst mit NSSM
Filename: "{app}\nssm.exe"; Parameters: "install ""{#MyAppServiceName}"" ""{app}\{#MyAppExeName}"""; StatusMsg: "Installiere Medi-WOL als Windows-Dienst..."; Flags: runhidden
; Setze die Kommandozeilenparameter für den Port
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppParameters ""-port {code:GetPort}"""; Flags: runhidden
; Setze Service-Beschreibung
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" Description ""{#MyAppServiceDescription}"""; Flags: runhidden
; Setze Service-Starttyp auf automatisch
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" Start SERVICE_AUTO_START"; Flags: runhidden
; Setze das Arbeitsverzeichnis
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppDirectory ""{app}"""; Flags: runhidden
; Setze Ausgabe-Umleitung für Debugging
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStdout ""{app}\service.log"""; Flags: runhidden
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStderr ""{app}\service-error.log"""; Flags: runhidden
; Setze Service-Restart bei Fehler
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppRestartDelay 5000"; Flags: runhidden
; Setze Timeout für Service-Start
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStopMethodSkip 0"; Flags: runhidden
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStopMethodConsole 1500"; Flags: runhidden
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStopMethodWindow 1500"; Flags: runhidden
Filename: "{app}\nssm.exe"; Parameters: "set ""{#MyAppServiceName}"" AppStopMethodThreads 1500"; Flags: runhidden
; Service nach Installation starten mit Verzögerung
Filename: "{app}\nssm.exe"; Parameters: "start ""{#MyAppServiceName}"""; StatusMsg: "Starte Medi-WOL Dienst..."; Flags: runhidden waituntilterminated; Check: ShouldStartService
; Windows Firewall-Ausnahmen für den Service hinzufügen
Filename: "netsh.exe"; Parameters: "advfirewall firewall add rule name=""{#MyAppServiceName} - Inbound"" dir=in action=allow program=""{app}\{#MyAppExeName}"" enable=yes"; StatusMsg: "Erstelle Windows Firewall-Ausnahme (Eingehend)..."; Flags: runhidden
Filename: "netsh.exe"; Parameters: "advfirewall firewall add rule name=""{#MyAppServiceName} - Outbound"" dir=out action=allow program=""{app}\{#MyAppExeName}"" enable=yes"; StatusMsg: "Erstelle Windows Firewall-Ausnahme (Ausgehend)..."; Flags: runhidden
Filename: "netsh.exe"; Parameters: "advfirewall firewall add rule name=""{#MyAppServiceName} - Port {code:GetPort}"" dir=in action=allow protocol=TCP localport={code:GetPort} enable=yes"; StatusMsg: "Erstelle Windows Firewall-Port-Regel..."; Flags: runhidden

[UninstallRun]
; Stoppe den Windows-Dienst vor der Deinstallation
Filename: "{app}\nssm.exe"; Parameters: "stop ""{#MyAppServiceName}"""; Flags: runhidden
; Entferne den Windows-Dienst
Filename: "{app}\nssm.exe"; Parameters: "remove ""{#MyAppServiceName}"" confirm"; StatusMsg: "Entferne Medi-WOL Dienst..."; Flags: runhidden
; Entferne Windows Firewall-Regeln
Filename: "netsh.exe"; Parameters: "advfirewall firewall delete rule name=""{#MyAppServiceName} - Inbound"""; Flags: runhidden
Filename: "netsh.exe"; Parameters: "advfirewall firewall delete rule name=""{#MyAppServiceName} - Outbound"""; Flags: runhidden
Filename: "netsh.exe"; Parameters: "advfirewall firewall delete rule name=""{#MyAppServiceName} - Port {code:GetPort}"""; Flags: runhidden

[Code]
var
  PortPage: TInputQueryWizardPage;

procedure InitializeWizard;
begin
  PortPage := CreateInputQueryPage(wpWelcome,
    'Port-Konfiguration',
    'Auf welchem Port soll Medi-WOL laufen?',
    'Bitte geben Sie den Port für den Medi-WOL Web-Server ein:');
  
  PortPage.Add('Port:', False);
  PortPage.Values[0] := '9000';
end;

function GetPort(Param: String): String;
begin
  Result := PortPage.Values[0];
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  PortStr: String;
  Port: Integer;
begin
  Result := True;
  
  if CurPageID = PortPage.ID then
  begin
    PortStr := PortPage.Values[0];
    
    // Einfache Validierung
    if PortStr = '' then
    begin
      MsgBox('Bitte geben Sie einen Port ein.', mbError, MB_OK);
      Result := False;
      Exit;
    end;
    
    // Konvertiere zu Integer
    Port := StrToIntDef(PortStr, 0);
    
    if (Port < 1) or (Port > 65535) then
    begin
      MsgBox('Port muss zwischen 1 und 65535 liegen.', mbError, MB_OK);
      Result := False;
      Exit;
    end;
  end;
end;

function ShouldStartService(): Boolean;
begin
  Result := WizardIsTaskSelected('startservice');
end;



[CustomMessages]
german.CreateDesktopIcon=Desktop-Verknüpfung erstellen
german.CreateQuickLaunchIcon=Quick Launch-Verknüpfung erstellen
german.AdditionalIcons=Zusätzliche Verknüpfungen:
german.ServiceOptions=Service-Optionen:
german.StartServiceAfterInstall=Service nach der Installation starten
german.UninstallProgram=Medi-WOL entfernen

english.ServiceOptions=Service Options:
english.StartServiceAfterInstall=Start service after installation
