#define MyAppName "EDMarketConnector"
#define MyAppLongName "Elite Dangerous Market Connector"
#define MyAppVersion "$appver"
#define MyAppPublisher "EDCD"
#define MyAppURL "https://edcd.github.io/"
#define SuppURL "https://github.com/EDCD/EDMarketConnector/"
#define MyAppExeName "EDMarketConnector.exe"

[Setup]
AppId={{5E9AD4D3-0365-41D5-9586-9368745DD109}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#SuppURL}
AppUpdatesURL={#SuppURL}
AppCopyright=Copyright (C) 2015-2019 Jonathan Harris, 2020-2023 EDCD
AllowUNCPath=no
AllowNetworkDrive=no
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
DirExistsWarning=yes
AllowNoIcons=yes
OutputBaseFilename=EDMarketConnector_Installer_{#MyAppVersion}
SetupIconFile=dist.win32\EDMarketConnector.ico
Compression=lzma2/max
SolidCompression=yes
WizardStyle=modern
InfoBeforeFile=dist.win32\Changelog.md
OutputDir=.
LicenseFile=LICENSE
AlwaysShowDirOnReadyPage=yes
UninstallDisplayIcon={app}\{#MyAppExeName}
MinVersion=6.2
ChangesAssociations = yes

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

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

[Files]
Source: "dist.win32\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "dist.win32\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

[Icons]
Name: "{autoprograms}\{#MyAppLongName}"; Filename: "{app}\{#MyAppExeName}"; Comment: "EDMC";
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon;

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

;Check if a WiX-based installation exists. If so, kill it with fire.
[Code]
function EndsWith(SubText, Text: AnsiString): Boolean;
var
  EndStr: string;
begin
  Log('Starting');
  EndStr := Copy(Text, Length(Text) - Length(SubText) + 1, Length(SubText));
  Log(EndStr);
  { Use SameStr, if you need a case-sensitive comparison }
  Result := SameText(SubText, EndStr);
end;

// EDMC didn't keep a consistant GUID during previous history. Due to that, we have to do this tomfoolery.
procedure CurStepChanged(CurStep: TSetupStep);
var
  ResultCode: Integer;
  Uninstall: String;
  OldWiX: Boolean;
  S: AnsiString;
  PowerShellOutputFile: String;
begin
  if (CurStep = ssInstall) then
  begin
  PowerShellOutputFile := ExpandConstant('{tmp}\PowershellOutput.txt');
  // Construct the PowerShell command and capture output to a file
  Exec('powershell.exe', '-NoProfile -ExecutionPolicy Bypass -Command "Get-WmiObject -Class Win32_Product | where Name -eq ''Elite Dangerous Market Connector'' | select-object -expandproperty IdentifyingNumber" | Out-File -Encoding ASCII "' + PowerShellOutputFile + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
    begin
    if LoadStringFromFile(PowerShellOutputFile, S) then
    S:= Trim(S);
    Log(S);
      begin
      OldWiX:=EndsWith('}', S);
      if (OldWiX = True) then
        begin
        MsgBox('Warning: an old version of EDMC is installed! Please close EDMC while we remove the old version!', mbInformation, MB_OK);
        Uninstall := '/x ' + S;
        Exec('MsiExec.exe', Uninstall, '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
        end;
      end;
    end;
  end;
end;

[Registry]
; Create the main registry key under HKCR
Root: HKCR; Subkey: "edmc"; Flags: uninsdeletekey
; Create a default value under the "edmc" key
Root: HKCR; Subkey: "edmc"; ValueType: string; ValueName: ""; ValueData: "{#MyAppName}"; Flags: uninsdeletekey
; Create the "URL Protocol" value under the "edmc" key
Root: HKCR; Subkey: "edmc"; ValueType: string; ValueName: "URL Protocol"; ValueData: ""; Flags: uninsdeletekey
; Create the "DefaultIcon" subkey under the "edmc" key
Root: HKCR; Subkey: "edmc\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\EDMarketConnector.exe,0"; Flags: uninsdeletekey
; Create the "shell" subkey under the "edmc" key
Root: HKCR; Subkey: "edmc\shell"; Flags: uninsdeletekey
; Create the "open" subkey under the "shell" subkey
Root: HKCR; Subkey: "edmc\shell\open"; Flags: uninsdeletekey
; Create the "command" subkey under the "open" subkey
Root: HKCR; Subkey: "edmc\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\EDMarketConnector.exe"" ""%1"""; Flags: uninsdeletekey
; Create the "ddeexec" subkey under the "open" subkey
Root: HKCR; Subkey: "edmc\shell\open\ddeexec"; ValueType: string; ValueName: ""; ValueData: "Open(""%1"")"; Flags: uninsdeletekey

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