; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "SiaDrive"
#define MyAppVersion "v@SIADRIVE_VERSION@"
#define MyAppPublisher "SiaExtensions"
#define MyAppURL "https://github.com/SiaExtensions/SiaDrive"
#define MyAppExeName "siadrive.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{C783A865-35A2-4740-A075-48F61C126505}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DisableProgramGroupPage=yes
LicenseFile=.\LICENSE
OutputBaseFilename=SiaDrive_{#MyAppVersion}_@CMAKE_BUILD_TYPE@_x64
Compression=lzma
SolidCompression=yes
ArchitecturesAllowed=x64
ArchitecturesInstallIn64BitMode=x64

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

[Components]   
Name: "main"; Description: "SiaDrive Files"; Types: full compact custom; Flags: fixed
Name: "vc_runtime"; Description: "Visual C++ 2015 Redistributable"; Types: full custom
Name: "dokany"; Description: "Dokany v1.0.3"; Types: full custom
Name: "sia"; Description: "Sia v@SIA_VERSION@"; Types: full custom

[InstallDelete]
Type: filesandordirs; Name: "{app}\htdocs"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; Components: main

[Files]
Source: ".\dist\@CMAKE_BUILD_TYPE@\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: main
Source: ".\3rd_party\Dokan_x64.msi"; Destdir: "{tmp}"; Flags: deleteafterinstall overwritereadonly; Components: dokany
Source: ".\3rd_party\vc_redist.x64.exe"; Destdir: "{tmp}"; Flags: deleteafterinstall overwritereadonly; Components: vc_runtime
Source: ".\3rd_party\Sia\*"; Destdir: "{app}\sia"; Flags: recursesubdirs createallsubdirs overwritereadonly; Components: sia
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Components: main
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon; Components: main

[Run]
Filename: "{tmp}\vc_redist.x64.exe"; Parameters: "/install /passive /norestart"; Flags: 64bit waituntilterminated; Components: vc_runtime 
Filename: "msiexec.exe"; Parameters: "/i {tmp}\Dokan_x64.msi /passive /norestart"; Flags: 64bit waituntilterminated; Components: dokany
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent; Components: main 

[Code]
var
  Page: TInputQueryWizardPage;
  ApiPort, HostPort, RpcPort: String;

procedure InitializeWizard;
begin
  ApiPort := '9980';
  RpcPort := '9981';
  HostPort := '9982';

  RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'ApiPort', ApiPort);
  RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'RpcPort', RpcPort);
  RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'HostPort', HostPort);

  Page := CreateInputQueryPage(wpSelectTasks,
    'Sia Settings', 'Server (siad) Connectivity',
    'Change the API port, Host port and/or RPC port, then click Next. Only modify these if absolutely necessary.');

  Page.Add('API Port:', False);
  Page.Add('RPC Port:', False);
  Page.Add('Host Port:', False);

  Page.Values[0] := ApiPort;
  Page.Values[1] := RpcPort;
  Page.Values[2] := HostPort;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
  var
    I: Longint;
begin
  if CurPageID = Page.ID then begin
    ApiPort := Page.Values[0];
    RpcPort := Page.Values[1];
    HostPort := Page.Values[2];

    I := StrToInt(ApiPort);
    if (I < 1024) or (I > 49151) then begin
      MsgBox('Please enter a valid API port (1024–49151)', mbError, MB_OK);
      Result := False;
    end else begin
      I := StrToInt(RpcPort);
      if (I < 1024) or (I > 49151) then begin
        MsgBox('Please enter a valid RPC port (1024–49151)', mbError, MB_OK);
        Result := False;
      end else begin
        I := StrToInt(HostPort);
        if (I < 1024) or (I > 49151) then begin
          MsgBox('Please enter a valid Host port (1024–49151)', mbError, MB_OK);
          Result := False;
        end else begin
          RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'ApiPort', ApiPort);
          RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'RpcPort', RpcPort);
          RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\SiaExtensions\SiaDrive', 'HostPort', HostPort);
          Result := true;
        end;
      end;
    end;
  end else
    Result := True;
end;