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

#define MyAppName "@PROJECT_FULL_NAME@"
#define MyAppVersion "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@.@VERSION_TWEAK@"
#define MyAppVersionText "@VERSION_STRING@"
#define MyAppPublisher "Xaymars Technology Workshop"
#define MyAppURL "https://xaymar.com/"
#define MyAppCopyright "@PROJECT_COPYRIGHT_YEARS@ @PROJECT_AUTHORS@"

[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.)

; Application Information
AppId={{DE56A03A-C8A4-474B-83B0-CFD270262D38}}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
AppMutex={#MyAppName}

; Versioning
VersionInfoProductName={#MyAppName}
VersionInfoProductVersion={#MyAppVersion}
VersionInfoVersion={#MyAppVersion}
VersionInfoProductTextVersion={#MyAppVersionText}
VersionInfoTextVersion={#MyAppVersionText}
VersionInfoCompany={#MyAppPublisher}
VersionInfoCopyright={#MyAppCopyright}
VersionInfoDescription={#MyAppName} Setup

; Architecture (Platform is always Windows)
ArchitecturesInstallIn64BitMode=x64 arm64 ia64
ArchitecturesAllowed=@ARCH@

; Wizard Information
WizardStyle=modern
WizardResizable=yes
SetupIconFile="@PROJECT_SOURCE_DIR@/media/icon.ico"

; Other Information
UsePreviousAppDir=yes
DefaultDirName={code:GetDefaultDirectory}
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
LicenseFile="@ISS_SOURCE_DIR@/LICENSE"
OutputDir="@ISS_PACKAGE_DIR@"
OutputBaseFilename=@CMAKE_PACKAGE_NAME@-@_PACKAGE_SUFFIX_OVERRIDE@
Compression=lzma2/ultra64
SolidCompression=yes
LZMAAlgorithm=1

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

[Files]
Source: "@ISS_FILES_DIR@/*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "@PROJECT_SOURCE_DIR@/templates/msvc-redist-helper.exe"; DestDir: "{app}"; DestName: "msvc-redist-helper.exe"; Flags: ignoreversion dontcopy noencryption
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"

[Code]
// ------------------------------------------------------------------------------------------------------------------ //
function FindRegistryKey(): String;
begin
	Result := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting('AppId')}_is1');	
end;

// ------------------------------------------------------------------------------------------------------------------ //
function GetDefaultDirectory(Value: String): String;
var
	sInstallPath: String;
begin
	// 1. Use the path we were given on call.
	sInstallPath := Value;

	// 2. If that was empty, try and find it ourselves from the registry.
	if (sInstallPath = '') then
		RegQueryStringValue(HKA64, FindRegistryKey(), 'InstallLocation', sInstallPath);

	// 2. If empty, try and find the "Local Machine" installation of OBS Studio.
	if (sInstallPath = '') then 
		RegQueryStringValue(HKLM64, 'SOFTWARE\OBS Studio', '', sInstallPath);

	// 3. If empty, try and find the "Current User" installation of OBS Studio.
	if (sInstallPath = '') then 
		RegQueryStringValue(HKCU64, 'SOFTWARE\OBS Studio', '', sInstallPath);

	// 4. If empty, try and find the 32-bit "Local Machine" installation of OBS Studio.
	if (sInstallPath = '') then 
		RegQueryStringValue(HKLM32, 'SOFTWARE\OBS Studio', '', sInstallPath);

	// 5. If empty, try and find the 32-bit "Current User" installation of OBS Studio.
	if (sInstallPath = '') then 
		RegQueryStringValue(HKCU32, 'SOFTWARE\OBS Studio', '', sInstallPath);

	// 6. If empty, just use the default path.
	if (sInstallPath = '') then 
		sInstallPath := ExpandConstant('{commonpf}\obs-studio');
	
	Result := sInstallPath
end;

// ------------------------------------------------------------------------------------------------------------------ //
function GetUninstallerPath(): String;
var
	sRegistryKey: String;
	sUninstallerPath: String;
begin
	sRegistryKey := FindRegistryKey();

	RegQueryStringValue(HKLM64, sRegistryKey, 'UninstallString', sUninstallerPath);

	if (sUninstallerPath = '') then
		RegQueryStringValue(HKCU64, sRegistryKey, 'UninstallString', sUninstallerPath);

	if (sUninstallerPath = '') then
		RegQueryStringValue(HKLM32, sRegistryKey, 'UninstallString', sUninstallerPath);

	if (sUninstallerPath = '') then
		RegQueryStringValue(HKCU32, sRegistryKey, 'UninstallString', sUninstallerPath);

	Result := sUninstallerPath;
end;

// ------------------------------------------------------------------------------------------------------------------ //
function IsUpgrade(): Boolean;
begin
	Result := (GetUninstallerPath() <> '');
end;

// ------------------------------------------------------------------------------------------------------------------ //
function UninstallOldVersion(): Integer;
var
	sUninstallerPath: String;
	iResultCode: Integer;
begin
	Result := 0;
	sUninstallerPath := GetUninstallerPath();
	if (sUninstallerPath <> '') then begin
		sUninstallerPath := RemoveQuotes(sUninstallerPath);
		if Exec(sUninstallerPath, '/VERYSILENT /NORESTART /SUPPRESSMSGBOXES', '', SW_HIDE, ewWaitUntilTerminated, iResultCode) then begin
			Result := iResultCode
		end else begin
			Result := 1
		end;
	end;
end;

// ------------------------------------------------------------------------------------------------------------------ //
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
	iResultCode: Integer;
begin
	// Attempt to remove old version if it exists.
	if (IsUpgrade()) then begin
		UninstallOldVersion();
	end;

	// Also ensure that we have the necessary prerequisites installed to run the program.
    ExtractTemporaryFile('msvc-redist-helper.exe');
    Exec(ExpandConstant('{tmp}\msvc-redist-helper.exe'), '2019', '', SW_HIDE, ewWaitUntilTerminated, iResultCode);
end;
