; ============================================================================
; EMX Beta Environment Variable Installer
; Sets system environment variable EMX_BETA=TRUE
; ============================================================================
;
; This installer:
; - Sets system environment variable EMX_BETA=TRUE
; - Registers in Add/Remove Programs for easy uninstall
; - Uninstall removes the EMX_BETA environment variable
;
; Silent Installation:
;   EMXBetaSetup_v1.0.exe /VERYSILENT /SUPPRESSMSGBOXES
;
; ============================================================================

[Setup]
AppId={{F7A2C8E1-3B4D-4F6A-9E2C-1D8B5A7F3E09}}
AppName=GE Aerospace eMx BETA Configuration
AppVersion=1.0
AppPublisher=GE Aerospace
DefaultDirName={autopf}\EMXBeta
CreateAppDir=no
PrivilegesRequired=admin
OutputDir=Output
OutputBaseFilename=EMXBetaSetup_v1.0
SolidCompression=yes
Compression=lzma2
WizardStyle=modern
SetupIconFile=gea-logo.ico
WizardImageFile=banner.bmp
WizardSmallImageFile=banner-sm.bmp
DisableWelcomePage=no
DisableDirPage=yes
DisableProgramGroupPage=yes
DisableReadyPage=no
DisableFinishedPage=no
Uninstallable=yes
CreateUninstallRegKey=yes

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

[Messages]
WelcomeLabel2=This will configure the EMX_BETA system environment variable on your computer.%n%nThis sets:%n%n    EMX_BETA = TRUE%n%nClick Next to continue.

[Registry]
; Set system environment variable EMX_BETA=TRUE
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: string; ValueName: "EMX_BETA"; ValueData: "TRUE"; Flags: uninsdeletevalue

[Code]
const
  APP_NAME = 'EMX Beta Configuration';

function InitializeSetup: Boolean;
begin
  Result := True;

  if not IsAdmin then
  begin
    MsgBox('This installer requires administrator privileges.' + #13#10 +
           'Please right-click and select "Run as administrator".',
           mbError, MB_OK);
    Result := False;
  end;
end;

// Broadcast WM_SETTINGCHANGE so running processes pick up the new variable
procedure BroadcastEnvironmentChange;
var
  ResultCode: Integer;
begin
  Exec('cmd.exe', '/c setx EMX_BETA_NOTIFY 1 >nul 2>&1 & reg delete "HKCU\Environment" /v EMX_BETA_NOTIFY /f >nul 2>&1',
       '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    // Broadcast environment change to notify running applications
    BroadcastEnvironmentChange;

    if not WizardSilent then
      MsgBox('EMX_BETA environment variable has been set to TRUE.' + #13#10 + #13#10 +
             'You may need to restart applications or log off/on for the change to take effect.',
             mbInformation, MB_OK);
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  ResultCode: Integer;
begin
  if CurUninstallStep = usPostUninstall then
  begin
    // Registry flag is auto-removed by Inno (uninsdeletevalue)
    // Broadcast environment change to notify running applications
    Exec('cmd.exe', '/c setx EMX_BETA_NOTIFY 1 >nul 2>&1 & reg delete "HKCU\Environment" /v EMX_BETA_NOTIFY /f >nul 2>&1',
         '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

    if not UninstallSilent then
      MsgBox('EMX_BETA environment variable has been removed.' + #13#10 + #13#10 +
             'You may need to restart applications or log off/on for the change to take effect.',
             mbInformation, MB_OK);
  end;
end;

function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo,
  MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
begin
  Result := 'EMX Beta Configuration' + NewLine +
            NewLine +
            'The following system environment variable will be set:' + NewLine +
            NewLine +
            Space + 'Name:  EMX_BETA' + NewLine +
            Space + 'Value: TRUE' + NewLine +
            NewLine +
            'This variable will be available system-wide after installation.' + NewLine +
            'A restart of applications or logoff/logon may be required.';
end;
