; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
; Languages pack download: https://jrsoftware.org/files/istrans/


[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.)
ArchitecturesInstallIn64BitMode=x64os
AppId={{APP_ID}}
AppVersion={{APP_VERSION}}
AppName={{DISPLAY_NAME}}
AppVerName={{DISPLAY_NAME}}
AppPublisher={{PUBLISHER_NAME}}
AppPublisherURL={{PUBLISHER_URL}}
AppSupportURL={{PUBLISHER_URL}}
AppUpdatesURL={{PUBLISHER_URL}}
DefaultDirName={autopf}\{{DISPLAY_NAME}}
UninstallDisplayIcon={app}\{{EXECUTABLE_NAME}}
ChangesAssociations=yes
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=.
OutputBaseFilename={{OUTPUT_BASE_FILENAME}}
SetupIconFile={{SETUP_ICON_FILE}}
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
Name: "arabic"; MessagesFile: "compiler:Languages\Arabic.isl"
Name: "armenian"; MessagesFile: "compiler:Languages\Armenian.isl"
Name: "brazilianportuguese"; MessagesFile: "compiler:Languages\BrazilianPortuguese.isl"
Name: "bulgarian"; MessagesFile: "compiler:Languages\Bulgarian.isl"
Name: "catalan"; MessagesFile: "compiler:Languages\Catalan.isl"
Name: "chinesesimplified"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"
Name: "corsican"; MessagesFile: "compiler:Languages\Corsican.isl"
Name: "czech"; MessagesFile: "compiler:Languages\Czech.isl"
Name: "danish"; MessagesFile: "compiler:Languages\Danish.isl"
Name: "dutch"; MessagesFile: "compiler:Languages\Dutch.isl"
Name: "finnish"; MessagesFile: "compiler:Languages\Finnish.isl"
Name: "french"; MessagesFile: "compiler:Languages\French.isl"
Name: "german"; MessagesFile: "compiler:Languages\German.isl"
Name: "hebrew"; MessagesFile: "compiler:Languages\Hebrew.isl"
Name: "hungarian"; MessagesFile: "compiler:Languages\Hungarian.isl"
Name: "italian"; MessagesFile: "compiler:Languages\Italian.isl"
Name: "japanese"; MessagesFile: "compiler:Languages\Japanese.isl"
Name: "korean"; MessagesFile: "compiler:Languages\Korean.isl"
Name: "norwegian"; MessagesFile: "compiler:Languages\Norwegian.isl"
Name: "polish"; MessagesFile: "compiler:Languages\Polish.isl"
Name: "portuguese"; MessagesFile: "compiler:Languages\Portuguese.isl"
Name: "russian"; MessagesFile: "compiler:Languages\Russian.isl"
Name: "slovak"; MessagesFile: "compiler:Languages\Slovak.isl"
Name: "slovenian"; MessagesFile: "compiler:Languages\Slovenian.isl"
Name: "spanish"; MessagesFile: "compiler:Languages\Spanish.isl"
Name: "swedish"; MessagesFile: "compiler:Languages\Swedish.isl"
Name: "tamil"; MessagesFile: "compiler:Languages\Tamil.isl"
Name: "turkish"; MessagesFile: "compiler:Languages\Turkish.isl"
Name: "ukrainian"; MessagesFile: "compiler:Languages\Ukrainian.isl"

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

[Files]
Source: "{{SOURCE_DIR}}\\*"; DestDir: "{app}"; Excludes: "\.sentry-native\,*.pdb,*.lib,*.exp,*.log"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Registry]


[Icons]
Name: "{autoprograms}\\{{DISPLAY_NAME}}"; Filename: "{app}\\{{EXECUTABLE_NAME}}"
Name: "{autodesktop}\\{{DISPLAY_NAME}}"; Filename: "{app}\\{{EXECUTABLE_NAME}}"; Tasks: desktopicon

[UninstallDelete]
; Only delete user data generated at runtime, not the entire app directory
; The [Files] section automatically handles deletion of installed files
Type: filesandordirs; Name: "{app}\data"
Type: filesandordirs; Name: "{app}\.sentry-native"


[InstallDelete]
Type: filesandordirs; Name: "{app}\unins000.dat"
Type: filesandordirs; Name: "{app}\unins000.exe"

[Run]
Filename: "{app}\\{{EXECUTABLE_NAME}}"; Description: "{cm:LaunchProgram,{{DISPLAY_NAME}}}"; Flags: nowait postinstall skipifsilent

[Code]
var
  PreserveUserData: Boolean;

function GetLocalAppDataPath(): string;
var
  AppNameNoSpace: string;
begin
  AppNameNoSpace := '{{DISPLAY_NAME}}';
  StringChangeEx(AppNameNoSpace, ' ', '', True);
  Result := ExpandConstant('{userappdata}\' + AppNameNoSpace);
end;

function InitializeUninstall(): Boolean;
var
  LocalAppDataPath: string;
  MsgText: string;
  MsgTitle: string;
begin
  Result := True;
  LocalAppDataPath := GetLocalAppDataPath();

  if DirExists(LocalAppDataPath) then
  begin
    // Set message text based on installation language
    case ActiveLanguage of
      'english':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Uninstall';
          MsgText := 'Do you want to preserve user data and settings?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'chinesesimplified':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - 卸载';
          MsgText := '是否保留用户数据和设置?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'chinesetraditional':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - 卸載';
          MsgText := '是否保留使用者資料和設定?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'japanese':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - アンインストール';
          MsgText := 'ユーザーデータと設定を保持しますか?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'korean':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - 제거';
          MsgText := '사용자 데이터 및 설정을 유지하시겠습니까?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'spanish':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Desinstalar';
          MsgText := '¿Desea conservar los datos y la configuración del usuario?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'french':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Désinstallation';
          MsgText := 'Voulez-vous conserver les données et les paramètres utilisateur?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'german':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Deinstallation';
          MsgText := 'Möchten Sie Benutzerdaten und Einstellungen behalten?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'russian':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Удаление';
          MsgText := 'Вы хотите сохранить данные и параметры пользователя?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
      'portuguese':
        begin
          MsgTitle := '{{DISPLAY_NAME}} - Desinstalar';
          MsgText := 'Deseja preservar dados e configurações do usuário?' + #13#10#13#10 + 
                     LocalAppDataPath;
        end;
    else
      begin
        MsgTitle := '{{DISPLAY_NAME}} - Uninstall';
        MsgText := 'Do you want to preserve user data and settings?' + #13#10#13#10 + 
                   LocalAppDataPath;
      end;
    end;
    
    { Default is to preserve (IDYES button) }
    PreserveUserData := MsgBox(MsgText, mbConfirmation, MB_YESNO or MB_DEFBUTTON1) = IDYES;
  end else
  begin
    PreserveUserData := True;
  end;
end;

procedure DeleteNamedSubdirsRecursive(const BaseDir, TargetName: string);
var
  FindRec: TFindRec;
  CurrentPath: string;
begin
  if FindFirst(AddBackslash(BaseDir) + '*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') and
           ((FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
        begin
          CurrentPath := AddBackslash(BaseDir) + FindRec.Name;
          if CompareText(FindRec.Name, TargetName) = 0 then
          begin
            DelTree(CurrentPath, True, True, True);
          end
          else
          begin
            DeleteNamedSubdirsRecursive(CurrentPath, TargetName);
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  LocalAppDataPath: string;
  AppPath: string;
begin
  if CurUninstallStep = usUninstall then
  begin
    AppPath := ExpandConstant('{app}');
    DeleteNamedSubdirsRecursive(AppPath, '{{DISPLAY_NAME}}');

    { Only delete user data if user did not choose to preserve it }
    if not PreserveUserData then
    begin
      LocalAppDataPath := GetLocalAppDataPath();
      if DirExists(LocalAppDataPath) then
      begin
        DelTree(LocalAppDataPath, True, True, True);
      end;
    end;
  end;
end;

function DirPathEndsWithApp(Path, AppName: String): Boolean;
begin
  Result := (CompareText(ExtractFileName(RemoveBackslashUnlessRoot(Path)), AppName) = 0);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  SelectedPath: string;
  AppName: string;
begin
  Result := True;
  if CurPageID = wpSelectDir then
  begin
    SelectedPath := RemoveBackslashUnlessRoot(WizardForm.DirEdit.Text);
    AppName := '{{DISPLAY_NAME}}';

    // Ensure custom path ends with app folder name to avoid installing into arbitrary folders.
    if not DirPathEndsWithApp(SelectedPath, AppName) then
    begin
      WizardForm.DirEdit.Text := AddBackslash(SelectedPath) + AppName;
    end;
  end;
end;

