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

[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.)
AppID={{FB5C2B83-31B1-48B3-B9B0-8B7D26816ABF}
AppName=5DT data glove plugin for Scol x64
AppVersion=1.2
AppVerName=5DT data glove plugin for Scol 1.2
AppPublisher=I-Maginer
AppPublisherURL=http://www.openspace3d.com
AppSupportURL=http://www.openspace3d.com
AppUpdatesURL=http://www.openspace3d.com
DefaultDirName={code:GetScolInstallDir}
DefaultGroupName=OpenSpace3D
OutputDir=release
OutputBaseFilename=scol_glove
SetupIconFile=../../install_scol_voyager/scol.ico
Compression=lzma/Max
SolidCompression=true
InternalCompressLevel=Max
VersionInfoVersion=1.2
VersionInfoCompany=I-Maginer
VersionInfoDescription=5DT data glove plugin for Scol x64
VersionInfoCopyright=I-Maginer 2011
MinVersion=0,5.01.2600
AppCopyright=I-Maginer 2011
UsePreviousAppDir=false
AppendDefaultGroupName=true
DisableDirPage=yes
ArchitecturesInstallIn64BitMode=x64
ArchitecturesAllowed=x64
SignTool=i-maginer
;sign setup i-maginer "C:\Program Files (x86)\Windows Kits\8.1\bin\x86\signtool" sign /a /t http://timestamp.globalsign.com/scripts/timestamp.dll $f

[Languages]
Name: english; MessagesFile: compiler:Default.isl
Name: french; MessagesFile: compiler:Languages\French.isl

[Tasks]

;TODO TEST if OS3D is present:
[Files]
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
Source: redist\*; DestDir: {tmp}\glove_redist\; Flags: ignoreversion
; TODO OS3D plugIT:
Source: ..\..\..\scol_sdk\x64\bin\Release\Plugins\glove.dll; DestDir: {app}\plugins; Flags: ignoreversion
Source: ..\..\..\scol_applications\openspace3d\os3dplugins\input\glove\*; DestDir: {code:GetScolLockedDir}\tools\os3dplugins\input\glove; Flags: ignoreversion recursesubdirs createallsubdirs; Excludes: .svn
; Source: ..\..\..\..\scol_applications\scolvoyager\locked\*; DestDir: {app}\Partition_LockedApp\locked; Flags: ignoreversion createallsubdirs overwritereadonly recursesubdirs; Excludes: .svn

[Icons]
Name: Scol Voy@ger\{cm:UninstallProgram,x64 5DT data glove plugin}; Filename: {uninstallexe}

[Run]
Filename: msiexec; Parameters: /i {tmp}\glove_redist\5DTDataGloveSeries_v3.0.msi; StatusMsg: Installing 5DT Data Glove SDK...

[Registry]

[CustomMessages]
french.NoScolInstalled=L'installation de Scol Voy@ger n'a pas été trouvée. Installez le Scol Voy@ger depuis www.scolring.org puis réessayez.
english.NoScolInstalled=The Scol Voy@ger installation can not be found. Install the Scol Voy@ger from www.scolring.org and retry.
french.BadScolInstalled=La version trouvée du Scol Voy@ger est obselète. Installez la dernière version du Scol Voy@ger depuis www.scolring.org puis réessayez.
english.BadScolInstalled=You must install the last version of Scol Voy@ger from www.scolring.org.

[Code]
//use http://www.sherlocksoftware.org/page.php?id=51 for http download
#include ReadReg(HKEY_LOCAL_MACHINE,'Software\Sherlock Software\InnoTools\Downloader','ScriptPath','')

#include "../../common/common_type.iss"

var
	InternalMsgs: array of TInternalMsg;
var
	NeedVoyager: Boolean;
var
  ScolUserPartition: String;
var
  ScolLockedPartition: String;

#include "../../common/common_code.iss"


function VoyagerIsNeeded(): Boolean;
begin
	Result := NeedVoyager;
end;


function GetScolUserDir(Default: String): String;
begin
  Result := ScolUserPartition;
end;

function GetScolLockedDir(Default: String): String;
begin
  Result := ScolLockedPartition;
end;


///////////////////////////////
// Scol install directory
function GetScolInstallDir(Default: String): String;
var
  Value: String;
begin
	Value := GetScolDir(ExpandConstant('{pf}') + '\Scol Voyager');
		if (CheckScolVer(Value) = False) then begin
			NeedVoyager := True;
		end
		else begin
			NeedVoyager := False;
		end;
		
  ScolUserPartition := GetScolUserPartitionDir(Value, ExpandConstant('{userdocs}') + '\Scol Voyager\Partition_LocalUsr');
  ScolLockedPartition := GetScolLockedPartitionDir(Value, Value + '\Partition_LockedApp');		
	Result := Value;
end;


var
  ResultCode: Integer;

procedure CurStepChanged(CurStep: TSetupStep);
var
		Uninstall: String;
		UsmIni: String;
		UsmFile: String;
		ArrUsmIni: array of String;
begin
	if (CurStep = ssInstall) then begin
		// Kill Scol Voyager and wait for it to terminate
		if Exec(ExpandConstant('tskill'), 'scol', '', SW_HIDE, ewNoWait, ResultCode) OR Exec(ExpandConstant('taskkill'), '/IM ' + GetScolExe() + ' /F', '', SW_HIDE, ewNoWait, ResultCode) then begin
			// handle success if necessary; ResultCode contains the exit code
			//MsgBox('ok', mbInformation, MB_OK);
		end
		else begin
			// handle failure if necessary; ResultCode contains the error code
			//MsgBox('pas ok', mbInformation, MB_OK);
		end;
		if RegQueryStringValue(HKLM, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{FB5C2B83-31B1-48B3-B9B0-8B7D26816ABF}_is1', 'UninstallString', Uninstall) then begin
		  Exec(RemoveQuotes(Uninstall), ' /SILENT', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
		end;
	end
	else begin
		if (CurStep = ssDone) then begin
			UsmFile := ExpandConstant('{app}\usm.ini');
			// update usm.ini
			LoadStringFromFile(UsmFile, UsmIni);

			if FileExists (UsmFile)= true then begin
				FileCopy (UsmFile, ExpandConstant('{app}\usm.bak'), false);
			end;

			// load file in array
			SetArrayLength(ArrUsmIni, 0);
			Strings2Array(UsmIni, ArrUsmIni);
			if InstallPluginDll(ArrUsmIni, 'glove.dll', '') then begin
				SaveStringsToFile(UsmFile, ArrUsmIni, false);
				//MsgBox(UsmFile, mbInformation, MB_OK);
			end
			else begin
				//MsgBox('KO', mbInformation, MB_OK);
			end;
		end;
	end;
end;


procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
	if (CurUninstallStep = usUninstall) then
	begin
		// Kill Scol Voyager and wait for it to terminate
		if Exec(ExpandConstant('tskill'), 'scol', '', SW_HIDE, ewNoWait, ResultCode) OR Exec(ExpandConstant('taskkill'), '/IM ' + GetScolExe() + ' /F', '', SW_HIDE, ewNoWait, ResultCode) then
		begin
			// handle success if necessary; ResultCode contains the exit code
			//MsgBox('ok', mbInformation, MB_OK);
		end
		else begin
			// handle failure if necessary; ResultCode contains the error code
			//MsgBox('pas ok', mbInformation, MB_OK);
		end;
	end;
end;


procedure InitializeWizard();
begin
	if NeedVoyager = True then begin
	  ITD_Init();
	  ITD_AddFile('http://www.scolring.org/rsc/scol_plugin.exe', ExpandConstant('{tmp}\scolplug.exe'));
	  ITD_AddMirror('http://www.openspace3d.com/rsc/scol_plugin.exe', ExpandConstant('{tmp}\scolplug.exe'));
	  ITD_DownloadAfter(wpReady);
	end;
end;