{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

You may retrieve the latest version of this file at the FBA Creator Home Page,
located at http://www.fbacreator.com

Known Issues:
-----------------------------------------------------------------------------}
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvBaseDlg, JvBrowseFolder, JvComponentBase,
  JvCreateProcess, mwLineStream, StrLib, ShellApi, Unit3, ExtCtrls, Resources,
  ComCtrls;

type
  TFrmPocketPCInstall = class(TForm)
    btnCancel: TButton;
    btnBuild: TButton;
    dlgSelectDestDir: TJvBrowseForFolderDialog;
    createcab: TJvCreateProcess;
    createsetup: TJvCreateProcess;
    dlgSelectIcon: TOpenDialog;
    tabcontrol: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    inipath: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    imgIcon: TImage;
    txtNewIcon: TLabel;
    txtPackageDirFinal: TLabel;
    Label7: TLabel;
    txtAppName: TEdit;
    txtPackageDir: TEdit;
    Button1: TButton;
    txtCompanyName: TEdit;
    txtEula: TMemo;
    txtReadMe: TMemo;
    GroupBox2: TGroupBox;
    radioWindowsInstaller: TRadioButton;
    radioCabPocketPC: TRadioButton;
    cmbshortcut: TComboBox;
    TabSheet2: TTabSheet;
    GroupBox1: TGroupBox;
    Button2: TButton;
    Button3: TButton;
    txtInf: TMemo;
    GroupBox3: TGroupBox;
    Button4: TButton;
    Button5: TButton;
    txtIni: TMemo;
    txtInfBack: TMemo;
    txtIniBack: TMemo;
    buffer: TMemo;
    GroupBox4: TGroupBox;
    radioDestPocketPC: TRadioButton;
    radioDestSmartphone: TRadioButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnCancelClick(Sender: TObject);
    procedure btnBuildClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure createcabTerminate(Sender: TObject; ExitCode: Cardinal);
    function BuildInfIniFileOld(nameinf: String; nameini: String; external_resfiles_list: TStringList; include_dll: Boolean): Boolean;
    function BuildInfIniFile(nameinf: String; nameini: String; external_resfiles_list: TStringList; include_dll: Boolean): Boolean;
    procedure createsetupTerminate(Sender: TObject; ExitCode: Cardinal);
    procedure CleanPackageDir(path: String; keeparm: Boolean; keepexe: Boolean);
    procedure imgIconClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmPocketPCInstall: TFrmPocketPCInstall;
  creatingCabCode, creatingSetupCode: Integer;

resourcestring
  DestPackageDirNotSelected= 'Please choose a destination directory for your package.';
  ApplicationNameNotSelected= 'Please choose a name for your application.';
  UnableToBuildInfIniFile='Aborted. Unable to build .inf or .ini file!';
  PackageBuild='Package build successfully!';
  PackageBuildFailed='Failed to create setup package!';
  UnableToBuildDatFile='Aborted. Unable to build .dat file!';
  FailedToCreateCabFile='Failed to create .cab file!';
  CompanyNameNotSelected='Please write your company name.';
  Unable_To_Create_Package_Directory='Unable to create package directory';

implementation

uses Unit1;

{$R *.dfm}

// ripulisce la dir dai file temporanei
procedure TFrmPocketPCInstall.CleanPackageDir(path: String; keeparm: Boolean; keepexe: Boolean);
var
str: String;
begin
  // verifica che non sia la dir dell'applicativo
  // per non cancellare i nostri files...
  if UpperCase(FPath(path) )= UpperCase(FExtractFilePath(Application.ExeName)) then exit;

  str := path + 'fba.dat';
  if FileExists(str) then DeleteFile(str);
  str := path + 'fba.ini';
  if FileExists(str) then DeleteFile(str);
  str := path + 'fba.exe';
  if FileExists(str) then DeleteFile(str);
  str := path + 'setup.inf';
  if FileExists(str) then DeleteFile(str);
  if keeparm = false then begin
    str := path + 'setup.arm.cab';
    if FileExists(str) then DeleteFile(str);
  end;
  if keepexe = false then begin
    str := path + 'setup.exe';
    if FileExists(str) then DeleteFile(str);
  end;
  str := path + 'setup.arm.dat';
  if FileExists(str) then DeleteFile(str);
  str := path + 'eula.txt';
  if FileExists(str) then DeleteFile(str);
  str := path + 'setup.ini';
  if FileExists(str) then DeleteFile(str);
  str := path + 'ezsetup.exe';
  if FileExists(str) then DeleteFile(str);
  str := path + 'readme.txt';
  if FileExists(str) then DeleteFile(str);
  str := path + 'cabwiz.exe';
  if FileExists(str) then DeleteFile(str);
  str := path + 'cabwiz.ddf';
  if FileExists(str) then DeleteFile(str);
  str := path + 'makecab.exe';
  if FileExists(str) then DeleteFile(str);
  str := path + GetSetupDllName(txtAppName.Text);
  if FileExists(str) then DeleteFile(str);

  // elimina i file di risorse esterne dalla dir specificata
  MainForm.ClearExtResourceFromDir(path);
end;

function TFrmPocketPCInstall.BuildInfIniFileOld(nameinf: String; nameini: String;
  external_resfiles_list: TStringList; include_dll: Boolean): Boolean;
var
ini: TLineStream;
dll,justdestfile: String;
a: Integer;
begin
  try
    // Recupera il nome unico (si spera ;) del setup.dll
    dll := GetSetupDllName(txtAppName.Text);

    // ini file
    ini := TLineStream.Create(nameini, fmCreate);
    with ini do begin
      WriteLine('[CEAppManager]');
      WriteLine('Version	= 1.0');
      WriteLine('Component = '+txtAppName.Text);
      WriteLine('');
      WriteLine('['+txtAppName.Text+']');
      WriteLine('Description = '+txtAppName.Text);
      WriteLine('Uninstall = '+txtAppName.Text);
      WriteLine('CabFiles = setup.ARM.CAB');
      FlushMemory;
    end;
    ini.Free;


    // inf file
    ini := TLineStream.Create(nameinf, fmCreate);
    with ini do begin
      WriteLine('[CEStrings]');
      WriteLine('InstallDir=%CE1%\%AppName%');
      WriteLine('AppName="'+txtAppName.Text+'"');
      WriteLine('');
      WriteLine('[Strings]');
      WriteLine('CompanyName="'+txtCompanyName.Text+'"');
      WriteLine('');
      WriteLine('[Version]');
      WriteLine('Signature="$Chicago$"');
      WriteLine('CESignature="$Windows CE$"');
      WriteLine('Provider=%CompanyName%');
      WriteLine('');
      WriteLine('[Shortcuts]');
      WriteLine('%AppName%,0,"fba.exe"');
      WriteLine('');
      WriteLine('[SourceDisksNames.Arm]');
      WriteLine('1=,"ARM FILES",,"'+txtPackageDirFinal.Caption+'"');
      WriteLine('2=,"ARM FILES",,"'+FExtractFilePath(Application.ExeName)+'"');
      WriteLine('');
      WriteLine('[SourceDisksFiles.Arm]');
      WriteLine('fba.exe=1');
      WriteLine('fba.dat=1');
      WriteLine('fba.ini=1');

      // risorse esterne
      for a:=0 to external_resfiles_list.Count-1 do begin

        justdestfile := ExtractFileName(external_resfiles_list.Strings[a]);

        // se è una dll e inizia con x86, non includere
        if (Length(justdestfile) > 3) and (UpperCase(Copy(justdestfile, Length(justdestfile)-3, 4))='.DLL') and (UpperCase(Copy(justdestfile, 1, 4)) = 'X86_') then
          continue;

        WriteLine(justdestfile+'=1');
      end;

      if include_dll then
        WriteLine(dll+'=1');
      WriteLine('');
      WriteLine('[Files.Arm]');
      WriteLine('fba.exe');
      WriteLine('fba.dat');
      WriteLine('fba.ini');

      // risorse esterne
      for a:=0 to external_resfiles_list.Count-1 do begin

        justdestfile := ExtractFileName(external_resfiles_list.Strings[a]);

        // se è una dll e inizia con x86, non includere
        if (Length(justdestfile) > 3) and (UpperCase(Copy(justdestfile, Length(justdestfile)-3, 4))='.DLL') and (UpperCase(Copy(justdestfile, 1, 4)) = 'X86_') then
          continue;

        WriteLine(justdestfile);
      end;

      if include_dll then
        WriteLine(dll);
      WriteLine('');
      WriteLine('[DefaultInstall]');
      if include_dll then
        WriteLine('CESETUPDLL='+dll);
      WriteLine('CEShortcuts=Shortcuts');
      WriteLine('');
      WriteLine('[DefaultInstall.Arm]');
      WriteLine('CopyFiles=Files.Arm');
      WriteLine('');
      WriteLine('[DestinationDirs]');
      //WriteLine('Shortcuts=,%CE2%\Start Menu');
      if cmbshortcut.ItemIndex = 0 then begin
        WriteLine('Shortcuts=,%CE17%');
      end else if cmbshortcut.ItemIndex = 1 then begin
        WriteLine('Shortcuts=,%CE11%');
      end else if cmbshortcut.ItemIndex = 2 then begin
        WriteLine('Shortcuts=,%CE14%');
      end;
      WriteLine('Files.Arm=,%InstallDir%');
      WriteLine('');
      WriteLine('[CEDevice]');
      WriteLine('ProcessorType=0');
      WriteLine('VersionMin=3.0');
      WriteLine('VersionMax=100.0');
      WriteLine('BuildMax=0xC0000000');
      WriteLine('');
      WriteLine('[CEDevice.Arm]');
      WriteLine('ProcessorType=2577');
      FlushMemory;
    end;
    ini.Free;
    Result:=true;
  except
    Result := false;
  end;
end;

function TFrmPocketPCInstall.BuildInfIniFile(nameinf: String; nameini: String;
  external_resfiles_list: TStringList; include_dll: Boolean): Boolean;
var
dll,justdestfile,temp: String;
a: Integer;
begin
  try
    // Recupera il nome unico (si spera ;) del setup.dll
    dll := GetSetupDllName(txtAppName.Text);

    // ini file
    buffer.Text := txtIni.Text;
    buffer.Text := Replace(buffer.Text, '<APPNAME>', txtAppName.Text);
    buffer.Lines.SaveToFile(nameini);


    // inf file
    buffer.Text := txtInf.Text;
    buffer.Text := Replace(buffer.Text, '<APPNAME>', txtAppName.Text);
    buffer.Text := Replace(buffer.Text, '<COMPANYNAME>', txtCompanyName.Text);
    buffer.Text := Replace(buffer.Text, '<FINALDIR>', txtPackageDirFinal.Caption);
    buffer.Text := Replace(buffer.Text, '<FBAEDITORPATH>', FExtractFilePath(Application.ExeName));

    temp := '';
    // risorse esterne
    for a:=0 to external_resfiles_list.Count-1 do begin

        justdestfile := ExtractFileName(external_resfiles_list.Strings[a]);

        // se è una dll e inizia con x86, non includere
        if (Length(justdestfile) > 3) and (UpperCase(Copy(justdestfile, Length(justdestfile)-3, 4))='.DLL') and (UpperCase(Copy(justdestfile, 1, 4)) = 'X86_') then
          continue;

        temp := temp + justdestfile+'=1' + Chr(13) + Chr(10);

    end;

    if include_dll then
      temp := temp + dll+'=1' + Chr(13) + Chr(10);

    buffer.Text := Replace(buffer.Text, '<EXTERNALRESOURCES>', temp);


    // risorse esterne
    temp := '';
    for a:=0 to external_resfiles_list.Count-1 do begin

        justdestfile := ExtractFileName(external_resfiles_list.Strings[a]);

        // se è una dll e inizia con x86, non includere
        if (Length(justdestfile) > 3) and (UpperCase(Copy(justdestfile, Length(justdestfile)-3, 4))='.DLL') and (UpperCase(Copy(justdestfile, 1, 4)) = 'X86_') then
          continue;

        temp := temp + justdestfile + Chr(13) + Chr(10);
    end;

    if include_dll then
      temp := temp + dll + Chr(13) + Chr(10);

    buffer.Text := Replace(buffer.Text, '<EXTERNALRESOURCESFILES>', temp);

    buffer.Text := Replace(buffer.Text, '<CESETUPDLL>', 'CESETUPDLL='+dll);

    temp := '';
    if cmbshortcut.ItemIndex = 0 then begin
        temp := 'Shortcuts=,%CE17%';
    end else if cmbshortcut.ItemIndex = 1 then begin
        temp := 'Shortcuts=,%CE11%';
    end else if cmbshortcut.ItemIndex = 2 then begin
        temp := 'Shortcuts=,%CE14%';
    end;

    buffer.Text := Replace(buffer.Text, '<SHORTCUTS>', temp);

    buffer.Lines.SaveToFile(nameinf);

    Result:=true;
  except
    Result := false;
  end;

end;


procedure TFrmPocketPCInstall.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
//  Action:=caFree;
end;

procedure TFrmPocketPCInstall.btnCancelClick(Sender: TObject);
begin
  self.Close;
end;

procedure TFrmPocketPCInstall.btnBuildClick(Sender: TObject);
var
  datfile, infpath, build_inipath, setupstr, relsetupstr, cabpath: String;
  f: TLineStream;
  extreslist: TStringList;
  eula, readme: String;
begin

  if Length(txtAppName.Text) = 0 then begin
    ShowMessage(ApplicationNameNotSelected);
    exit;
  end;

  if Length(txtCompanyName.Text) = 0 then begin
    ShowMessage(CompanyNameNotSelected);
    exit;
  end;

  if Length(txtPackageDir.Text) = 0 then begin
    ShowMessage(DestPackageDirNotSelected);
    exit;
  end;

  txtPackageDirFinal.Caption := txtPackageDir.Text+MakeValidPath(txtAppName.Text)+'\';
  if DirectoryExists(txtPackageDirFinal.Caption) = false then begin
    try
      MkDir(txtPackageDirFinal.Caption);
    except
      ShowMessage(Unable_To_Create_Package_Directory);
      exit;
    end;
  end;

  // PRIMA FASE COSTRUZIONE FILE .CAB //////////////////
  // crea il file .dat
  datfile := txtPackageDirFinal.Caption+'fba.dat';
  if MainForm.BuildDatFromResourcesList(datfile, datfile)<>3 then begin
    ShowMessage(UnableToBuildDatFile);
    CleanPackageDir(txtPackageDirFinal.Caption, false, false);
    exit;
  end;

  // copia le risorse esterne all'interno della directory destinazione
  if MainForm.CopyExtResourcesToDir(txtPackageDirFinal.Caption, 1) <> 1 then begin
    ShowMessage('Error. Unable to copy external resources file');
    CleanPackageDir(txtPackageDirFinal.Caption, false, false);
    exit;
  end;

  // il file .cab finale
  cabpath := txtPackageDirFinal.Caption+'setup.ARM.CAB';

  // Crea file .inf e .ini
  infpath := txtPackageDirFinal.Caption+'setup.inf';
  build_inipath := txtPackageDirFinal.Caption+'setup.ini';
  extreslist := MainForm.GetExtResourceFilesList( txtPackageDirFinal.Caption );
  //
  if BuildInfIniFile(infpath, build_inipath, extreslist, true) = false then begin
    ShowMessage(UnableToBuildInfIniFile);
    CleanPackageDir(txtPackageDirFinal.Caption, false, false);
    exit;
  end;
  extreslist.Free;

  // copia la dll di setup, nella dir del package
  CopyRes('PPCSETUPDLL', txtPackageDirFinal.Caption+GetSetupDllName(txtAppName.Text), false);

  // copia l'arm nella dir del package
  // copia il loader li dentro
  {$IFDEF DEBUG_COPY_LAUNCHER_FROM_DIR}
    CopyFile(PChar(DEBUG_VC_ARMRELEASE_LOADER_DIR+LauncherVCBuildName(1,'')), PChar(txtPackageDirFinal.Caption+'fba.exe'), false);
  {$ELSE}
    CopyRes(LauncherResName(1, ''), txtPackageDirFinal.Caption+'fba.exe', true);
  {$ENDIF}

  // applica registrazione
  ERegisterExeFile(PChar(txtPackageDirFinal.Caption+'fba.exe'));

  // se non è la versione vga, rimuovi risorsa HI_RES_AWARE
  if SETTINGS[SETTINGS_VGA]<>'1' then begin
    KillResource(txtPackageDirFinal.Caption+'fba.exe', 'CEUX', 'HI_RES_AWARE',1040);
  end;

  // se abbiamo selezionato di cambiare l'icona, cambiala!
  if Length(txtNewIcon.Caption) > 0 then begin
    if ChangeIconExe(Pchar(txtPackageDirFinal.Caption+'fba.exe'), PChar(txtNewIcon.Caption)) = false then begin
      //CleanPackageDir(txtPackageDirFinal.Caption);
      //exit;
      // se non riusciamo a copiare l'icona, ignora l'errore e crea l'exe con l'icona default
      {$IFDEF DEBUG_COPY_LAUNCHER_FROM_DIR}
        CopyFile(PChar(DEBUG_VC_ARMRELEASE_LOADER_DIR+LauncherVCBuildName(1,'')), PChar(txtPackageDirFinal.Caption+'fba.exe'), false);
      {$ELSE}
        CopyRes(LauncherResName(1, ''), txtPackageDirFinal.Caption+'fba.exe', true);
      {$ENDIF}
    end;
  end;

  // crea file .ini con i settaggi del progetto
  MainForm.BuildLaunchIniFile(txtPackageDirFinal.Caption+'fba.ini', false, 0);
  //if FileExists(inipath.Caption) then begin
  //  CopyFile(PChar(inipath.Caption),PChar(txtPackageDirFinal.Caption+'fba.ini'), false);
  //end else begin
  //  f := TLineStream.create(txtPackageDir.Text+'fba.ini', fmCreate);
  //  f.Free;
  //end;

  btnBuild.Enabled := false;

  // elimina file .cab se già presente
  if FileExists(cabpath) then begin
    DeleteFile(cabpath);
  end;

  // estrai cabwiz.exe e cabwiz.ddf e makecab.exe nella directory finale
  // estrai cabwiz o cabwizsp se selezioniamo pocketpc o smartphone
  if self.radioDestSmartphone.Checked then begin
    CopyRes('CABWIZSP', txtPackageDirFinal.Caption+'cabwiz.exe', false);
  end else begin
    CopyRes('CABWIZ', txtPackageDirFinal.Caption+'cabwiz.exe', false);
  end;
  CopyRes('MAKECAB', txtPackageDirFinal.Caption+'makecab.exe', false);
  CopyRes('CABWIZDDF', txtPackageDirFinal.Caption+'cabwiz.ddf', false);

  // dobbiamo avviare cabwiz per creare il cab
  //ShellExecute(0, 'open', PChar(txtPackageDirFinal.Caption+'cabwiz.exe'),PChar('"'+infpath+'" /cpu "ARM"'), PChar(txtPackageDirFinal.Caption), 1);
  WinExec(PChar(txtPackageDirFinal.Caption+'cabwiz.exe'+' "'+infpath+'" /cpu "ARM"'), SW_HIDE);

  //creatingCabCode := -99999;
  //createcab.ApplicationName := ''+cabwizpath.Caption+'';
  //createcab.CommandLine := '"'+infpath+'" /cpu "ARM"';
  //createcab.Run;

  //while creatingCabCode = -99999 do begin
  //  Application.ProcessMessages;
  //end;

  //if creatingCabCode <> 0 then begin
  //  ShowMessage(FailedToCreateCabFile);
  //  btnBuild.Enabled:=true;
  //  exit;
  //end;

  // se il .cab c'è, tutto è andato per il verso giusto!
  if FileExists(cabpath) = False then begin
    btnBuild.Enabled := true;
    ShowMessage(FailedToCreateCabFile);
    CleanPackageDir(txtPackageDirFinal.Caption, false, false);
    exit;
  end;

  // se abbiamo richiesto solo il cab, finiamo qui
  if radioCabPocketPC.Checked then begin
    ShowMessage(PackageBuild+chr(13)+'Created in '+txtPackageDirFinal.Caption);
    CleanPackageDir(txtPackageDirFinal.Caption, true, false);
    self.Close;
    exit;
  end;

  // SECONDA FASE, COSTRUZIONE SETUP //////////////////
  setupstr := '-l english';
  relsetupstr := '-l english';
  
  //ezsetup -l english -i setupF.ini -r readme.txt -e eula.txt -o numberdropper_full.exe
  // attenzione: quel cazzo di ezsetup vuole i parametri in quest'ordine!!
  // (scoperto dopo ore spulciando i sorgenti di ezsetup -_-)
  // aggiornamento: me ne fotto perchè ho patchato ezsetup

  setupstr := setupstr + ' -i "'+build_inipath+'"';
  relsetupstr := relsetupstr + ' -i setup.ini';

  // elimina eula e readme se esistono
  if FileExists(txtPackageDirFinal.Caption+'eula.txt') then begin
    DeleteFile(txtPackageDirFinal.Caption+'eula.txt');
  end;
  if FileExists(txtPackageDirFinal.Caption+'readme.txt') then begin
    DeleteFile(txtPackageDirFinal.Caption+'readme.txt');
  end;

  // salva readme
  readme := txtREadMe.Text;
  readme := Replace(readme, '%COMPANYNAME%', txtCompanyName.Text);
  readme := Replace(readme, '%APPNAME%', txtAppName.Text);

  f := TLineStream.create(txtPackageDirFinal.Caption+'readme.txt', fmCreate);
    f.WriteLine(readme);
    f.FlushMemory;
  f.Free;
  setupstr := setupstr + ' -r "'+txtPackageDirFinal.Caption+'readme.txt"';
  relsetupstr := relsetupstr + ' -r readme.txt';

  // salva file eula
  eula := txtEula.Text;
  eula := Replace(eula, '%COMPANYNAME%', txtCompanyName.Text);
  eula := Replace(eula, '%APPNAME%', txtAppName.Text);

  f := TLineStream.create(txtPackageDirFinal.Caption+'eula.txt', fmCreate);
    f.WriteLine(eula);
    f.FlushMemory;
  f.Free;

  setupstr := setupstr + ' -e "'+txtPackageDirFinal.Caption+'eula.txt"';
  relsetupstr := relsetupstr + ' -e eula.txt';


  // cancella exe finale, se presente
  if FileExists(txtPackageDirFinal.Caption+'setup.exe') then begin
    DeleteFile(txtPackageDirFinal.Caption+'setup.exe');
  end;

  //ezsetup -l english -i setupF.ini -r readme.txt -e eula.txt -o numberdropper_full.exe
  setupstr := setupstr + ' -o "'+txtPackageDirFinal.Caption+'setup.exe"';
  relsetupstr := relsetupstr + ' -o setup.exe';

  // copia ezsetup nella dir finale
  CopyRes('EZSETUP',txtPackageDirFinal.Caption+'ezsetup.exe', false);

  // lancia creatore setup
  //txtReadMe.Text := 'ezsetup.exe'+' '+setupstr;
  //WinExec(PChar(txtPackageDirFinal.Caption+'ezsetup.exe'+' '+setupstr), SW_HIDE);

  creatingsetupCode := -99999;
  createsetup.CurrentDirectory := txtPackageDirFinal.Caption;
  createsetup.ApplicationName := txtPackageDirFinal.Caption+'ezsetup.exe';
  createsetup.CommandLine := setupstr;
  createsetup.Run;

  while creatingsetupCode = -99999 do begin
    Application.ProcessMessages;
  end;

  // file exe creato, tutto ok
  if FileExists(txtPackageDirFinal.Caption+'setup.exe') then begin
    CleanPackageDir(txtPackageDirFinal.Caption, false, true);
    ShowMessage(PackageBuild+chr(13)+'Created in '+txtPackageDirFinal.Caption);
    self.Close;
  end else begin
    CleanPackageDir(txtPackageDirFinal.Caption, false, false);
    ShowMessage(PackageBuildFailed);
  end;

end;

procedure TFrmPocketPCInstall.Button1Click(Sender: TObject);
begin
  if dlgSelectDestDir.Execute then txtPackageDir.Text := FPath(dlgSelectDestDir.Directory);
end;

procedure TFrmPocketPCInstall.createcabTerminate(Sender: TObject;
  ExitCode: Cardinal);
begin
  creatingCabCode := ExitCode;
end;

procedure TFrmPocketPCInstall.createsetupTerminate(Sender: TObject;
  ExitCode: Cardinal);
begin
  creatingSetupCode := ExitCode;
end;

procedure TFrmPocketPCInstall.imgIconClick(Sender: TObject);
begin
  if dlgSelectIcon.Execute = false then exit;
  try
    imgIcon.Picture.LoadFromFile(dlgSelectIcon.FileName);
    txtNewIcon.Caption := dlgSelectIcon.FileName;
  except
    txtNewIcon.Caption := '';
  end;
end;

procedure TFrmPocketPCInstall.FormCreate(Sender: TObject);
var
curdir: String;
begin
  // usa icona di default
  txtNewIcon.Caption := '';

  // carica templates dal filesystem
  tabcontrol.ActivePageIndex:=0;
  curdir := FPath( ExtractFilePath(Application.ExeName) );
  // se esiste carica dal file, se non esiste salva il default sul file
  // e assegna nel textbox
  if FileExists(curdir + 'arm_template_inf.dat') then begin
    txtInf.Lines.LoadFromFile(curdir + 'arm_template_inf.dat');
  end else begin
    txtInfBack.Lines.SaveToFile(curdir + 'arm_template_inf.dat');
    txtInf.Text := txtInfBack.Text;
  end;
  if FileExists(curdir + 'arm_template_ini.dat') then begin
    txtIni.Lines.LoadFromFile(curdir + 'arm_template_ini.dat');
  end else begin
    txtIniBack.Lines.SaveToFile(curdir + 'arm_template_ini.dat');
    txtIni.Text := txtIniBack.Text;
  end;

end;

procedure TFrmPocketPCInstall.Button3Click(Sender: TObject);
begin
  if MessageDlg('Restore default template?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
      txtInf.Text := txtInfBack.Text;
  end;

end;

procedure TFrmPocketPCInstall.Button5Click(Sender: TObject);
begin
  if MessageDlg('Restore default template?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
      txtIni.Text := txtIniBack.Text;
  end;
end;

end.
