; This is version
; 1.16 by ginetto
; Maintainer: Bernd Nowak
;
; History
; Original version released to the 66mod beta group
; 1.01
; Added the addon directory
; 1.02
; Disable the Uninst function !!!!
; 1.03
; Fixed some stuff with readme, screenshot.bmp and rpy only working when setup tested directly from within IStool.
; It has been mandatory to include them and to unpack them temporary
; Fixed the INI section for non-existing ini files. I have only checked if INI file is there or not and in case
; an ini file has not been found the ini file regardless of user choice has been created.
; 1.04
; Added an empty line to the ini file(s)
; Fixed the replay problem
; 1.05
; Fixed the Screenshot.bmp request. If a screenshot is found it will be
; added but if no is found a noscreenshot.bmp will be used
; Added SetupWindowTitle and SetupAppTitle in the Messages section
; 1.06
; Changed some minor infos for the setup.exe file which is generated. Removed GPLPS from
; the copyright stuff to avoid some problems there.
; Maybe I do the same for the GPLPS small and left side bmp.
; 1.07
; Changed the selection page ordering and changed gp.ini to unselected
; Added a screenshot to show where you find the information of Author & Version which you can use in the
; install.ini file.
; 1.08
; Added Msgbox when reached 64 limit in either gp.ini or 67season.ini
; Could be discussed what to write there
; 1.09
; If the trackdir exists a message will be displayed
; 1.10
; Added a registry query for the default GPL directory. Value is queried from
; 1st:	HKLM SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\gpl.exe\path
; 2nd:	HKLM SOFTWARE\Sierra OnLine\Setup\GPL\Directory
; 3rd:	HKLM SOFTWARE\GPLPS\TrackInstaller\GPLPath
; After the installer has run it will update only HKLM SOFTWARE\GPLPS\TrackInstaller\GPLPath
; 1.11
; Added a function call to check for GPL.exe to make sure that the rest is working ok.
; Added some additional checks and error messages for compiling
; Added the files needed to run without installing the whole Inno Setup. You can
; now use the BuildTrack.cmd under Windows 2000/XP/Vista for compiling
; Fixed a stupid german/english thing where I have named a key dialogtitel in the install.ini
; file where it should have been dialogTitle.
; 1.12
; Added a demo install.ini file to make usage a little bit easier.
; Hopefully corrected the readme dialogtitel = dialogtitle
; 1.13
; Changed the no GP and no season67 checkbox behavior
; When user select no update to gp and season67 he will be warned but can proceed
; Adjusted readme.txt of the alternative track installer itself and this file to remove hints to get Inno Setup itself. It should run without any additional software installed
; 1.14
; Updates of Inno files
; 1.15
; Added flag entry
; 1.16
; Added trackShortName entry
; Replaced noscreenshot, WizardLeft Image, WizardSmallImage bitmaps and trackinstaller icon
;


#define trackdir ReadIni(".\track\install.ini","installinfo", "trackDirectory")
#define dialogtitle ReadIni(".\track\install.ini","installinfo", "dialogTitle")
#define replayfilename ReadIni(".\track\install.ini","installinfo", "replayfilename")
#define name ReadIni(".\track\install.ini","installinfo", "name")
#define shortname ReadIni(".\track\install.ini","installinfo", "shortname")
#define month ReadIni(".\track\install.ini","installinfo", "month")
#define day ReadIni(".\track\install.ini","installinfo", "day")
#define numberOfLaps ReadIni(".\track\install.ini","installinfo", "numberOfLaps")
#define startingGrid ReadIni(".\track\install.ini","installinfo", "startingGrid")
#define flag ReadIni(".\track\install.ini","installinfo", "flag")
#define trackShortName ReadIni(".\track\install.ini","installinfo", "trackShortName")
#define author ReadIni(".\track\install.ini","installinfo", "author")
#define version ReadIni(".\track\install.ini","installinfo", "version")
#if "" == version
#define version "1.00"
#endif
#if "" == author
#define author "GPL Track Installer"
#endif
#define screenshot "false"
#if fileexists(".\track\screenshot.bmp")
#define screenshot "true"
#endif

; Additional checks and messages to help
#if "" == trackDir
#error You must fill in a value for trackDirectory in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == dialogtitle
#error You must fill in a value for dialogtitle in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == name
#error You must fill in a value for name in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == shortname
#error You must fill in a value for shortname in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == month
#error You must fill in a value for month in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == day
#error You must fill in a value for day in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == numberOfLaps
#error You must fill in a value for numberOfLaps in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == StartingGrid
#error You must fill in a value for StartingGrid in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == flag
#error You must fill in a value for flag in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif

#if "" == trackShortName
#error You must fill in a value for trackShortName in the .\track\INSTALL.INI file before compiling. This is mandatory !
#endif



[Setup]
OutputBaseFilename=gpltrackinstall{#trackdir}v{#version}
AppCopyright={#author}
AppName={#dialogtitle}
AppVerName=1.0
; DefaultDirName=C:\Sierra\GPL
DefaultDirName={code:GetDefaultPathRegistry|C:\Sierra\GPL}
UsePreviousAppDir=false
DirExistsWarning=no
AllowUNCPath=false
AppendDefaultDirName=false
DisableProgramGroupPage=true
UsePreviousGroup=false
ShowLanguageDialog=no
SetupIconFile=trackinstaller.ico
WizardImageFile=WizardLeftImage.bmp
WizardSmallImageFile=WizardSmallImage.bmp
Compression=lzma/ultra
Uninstallable=false
VersionInfoVersion={#version}
VersionInfoCompany={#author}
VersionInfoDescription={#dialogtitle}
VersionInfoTextVersion={#dialogtitle}
VersionInfoCopyright={#author}

[Files]
Source: track\readme.txt; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion isreadme
Source: track\maxrace.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\minrace.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\pass1.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\pass2.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\pit.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\race.lp; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\record.ini; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
Source: track\track.ini; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
#ifexist "track\dat"
Source: track\dat; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion; DestName: {#trackdir}.dat
#endif
#ifexist "track\*.dat"
Source: track\{#trackdir}.dat; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
#endif
;#ifexist "track\rpy"
; Source: track\rpy; DestDir: {app}\replay; Flags: overwritereadonly ignoreversion; DestName: {#replayfilename}.rpy <= Silly bug ! 27.10.2007 Bernd Nowak
;Source: track\rpy; DestDir: {app}\replay; Flags: overwritereadonly ignoreversion; DestName: {#replayfilename}
;#endif
#ifexist "track\rpy"
Source: track\rpy; DestDir: {tmp}; Flags: dontcopy; DestName: {#replayfilename}
#endif
#ifexist "track\*.rpy"
Source: track\*.rpy; DestDir: {app}\replay; Flags: overwritereadonly ignoreversion
#endif
#ifexist "track\*.jpg"
Source: track\{#trackdir}.jpg; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion
#endif
Source: track\addons\*.*; DestDir: {app}\tracks\{#trackdir}; Flags: overwritereadonly ignoreversion skipifsourcedoesntexist
; Temporary files
#ifexist "track\screenshot.bmp"
Source: track\screenshot.bmp; DestDir: {tmp}; Flags: dontcopy
#else
Source: noscreenshot.bmp; DestDir: {tmp}; Flags: dontcopy
#endif

[Registry]
; #define PapyKey "gpl.exe"
; Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{#PapyKey}; ValueType: string; ValueData: {app}\gpl.exe; Flags: createvalueifdoesntexist noerror; Components:
; Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{#PapyKey}; ValueType: string; ValueName: Path; ValueData: {app}; Flags: createvalueifdoesntexist noerror; Components:
; Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{#PapyKey}; ValueType: string; ValueData: {app}\gpl.exe; Flags: deletevalue noerror; Components:
; Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{#PapyKey}; ValueType: string; ValueName: Path; ValueData: {app}; Flags: deletevalue noerror; Components:
Root: HKLM; Subkey: SOFTWARE\GPLPS\TrackInstaller; ValueType: string; ValueName: GPLPath; ValueData: {app}; Flags: deletevalue noerror; Components: 

[CustomMessages]
trackinstallchoices_Caption=Please choose your options
trackinstallchoices_Description=Adjust the setup and read the notes
trackinstallchoices_LUpdateGPINI_Caption0=Update GP.INI (Tracks for World Championship Mode)
trackinstallchoices_LUpdate67SEASONINI_Caption0=Update 67SEASON.INI (Tracks for Single Race Mode)
trackinstallchoices_LInstallREPLAY_Caption0=Install a Replay
trackinstallchoices_UpdateGPINI_Caption0=UpdateGPINI
trackinstallchoices_Update67SEASONINI_Caption0=Update67SEASONINI
trackinstallchoices_InstallREPLAY_Caption0=InstallREPLAY
trackinstallchoices_ReadMeMemo_Line0=ReadMeMemo
checkgpl=No GPL.exe found in your destination directory ! Please select a valid destination directory


[Code]
var
	TrackBitmap: TBitmapImage;
	LUpdateGPINI: TLabel;
	LUpdate67SEASONINI: TLabel;
	LInstallREPLAY: TLabel;
	UpdateGPINI: TCheckBox;
	Update67SEASONINI: TCheckBox;
	InstallREPLAY: TCheckBox;
	ReadMeMemo: TMemo;


function GetDefaultPathRegistry(DefaultPath: string): string;
begin
	if not RegQueryStringValue(HKLM,'SOFTWARE\GPLPS\TrackInstaller','GPLPath',Result) then
	begin
		if not RegQueryStringValue(HKLM,'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\gpl.exe','Path',Result) then
		begin
			if not RegQueryStringValue(HKLM,'SOFTWARE\Sierra OnLine\Setup\GPL','Directory',Result) then
			begin
				Result := ExpandConstant(DefaultPath);
			end;
		end;
	end;
end;

function InitializeSetup(): Boolean;
begin
	// Msgbox('Screenshot: {#screenshot}',mbInformation,MB_OK);
	if '{#screenshot}' = 'true' then
	begin
		ExtractTemporaryFile('screenshot.bmp');
	end
	else
	begin
		ExtractTemporaryFile('noscreenshot.bmp');
	end
	ExtractTemporaryFile('readme.txt');
	if '{#replayfilename}' <> '' then
	begin
		ExtractTemporaryFile('{#replayfilename}');
	end
	result :=true;
end;

function checkfilegpini():Boolean;
begin
	if FileExists(ExpandConstant('{app}')+'\seasons\gp.ini') then
	begin
		result := true;
		end
	else begin
		result :=false;
	end
end;

function checkfile67seasonini():Boolean;
begin
	if FileExists(ExpandConstant('{app}')+'\seasons\67season.ini') then
	begin
		result := true;
		end
	else begin
		result :=false;
	end
end;

function checknumeventsgpini():Boolean;

var
	checknumevents : LongInt;
	seasondir : String;

begin
	result := false;
	seasondir := ExpandConstant('{app}')+'\seasons\gp.ini';
	if checkfilegpini then
	begin
		checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir);
			if checknumevents > 64 then
			begin
				result := false;
			end
			else begin
				result :=true;
			end
		end
	else begin
		result := false;
	end
end;

function checknumevents67seasonini():Boolean;

var
	checknumevents : LongInt;
	seasondir : String;

begin
	result := false;
	seasondir := ExpandConstant('{app}')+'\seasons\67season.ini';
	if checkfile67seasonini then
	begin
		checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir);
			if checknumevents > 64 then
			begin
				result := false;
			end
			else begin
				result :=true;
			end
		end
	else begin
		result := false;
	end
end;

function checktrackgpini():Boolean;
var
	checknumevents,i : LongInt;
	seasondir : String;

begin
	result :=false;
	seasondir := ExpandConstant('{app}')+'\seasons\gp.ini';
	if checknumeventsgpini then
	begin
		checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir);
		for i := 0 to checknumevents do begin
			// MsgBox('Event : Event'+IntToStr(i),mbInformation,MB_OK);
			if '{#trackdir}' = GetIniString('Event'+IntToStr(i), 'trackDirectory', 'falsch',seasondir) then
			begin
			result :=true;
			// MsgBox('Event : Event'+IntToStr(i)+' is: {#trackdir}',mbInformation,MB_OK);
			exit;
			end;
		end;
	end;
end;

function checktrack67seasonini():Boolean;
var
	checknumevents,i : LongInt;
	seasondir : String;

begin
	result :=false;
	seasondir := ExpandConstant('{app}')+'\seasons\67season.ini';
	if checknumeventsgpini then
	begin
		checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir);
		for i := 0 to checknumevents do begin
			// MsgBox('Event : Event'+IntToStr(i),mbInformation,MB_OK);
			if '{#trackdir}' = GetIniString('Event'+IntToStr(i), 'trackDirectory', 'falsch',seasondir) then
			begin
			result :=true;
			// MsgBox('Event : Event'+IntToStr(i)+' is: {#trackdir}',mbInformation,MB_OK);
			exit;
			end;
		end;
	end;
end;

function eventsokgpini():Boolean;

var
	seasondir : String;
begin
	result :=false;
	seasondir := ExpandConstant('{app}')+'\seasons\gp.ini';
	if GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir) < 64 then result :=true;
end;

function eventsok67seasonini():Boolean;

var
	seasondir : String;

begin
	result :=false;
	seasondir := ExpandConstant('{app}')+'\seasons\67season.ini';
	if GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir) < 64 then result :=true;
end;

function replays():Boolean;

begin
	result :=false;
	if FileExists(ExpandConstant('{tmp}\{#replayfilename}')) then result :=true;
end;

function writeiniheader(ini:string):Boolean;

var
	seasondir :string;
begin
	result :=true;
	seasondir := ExpandConstant('{app}')+'\seasons\';
	SetIniString('Season', 'name', 'GP', seasondir + ini);
	SetIniInt('Season', 'year', 1967, seasondir + ini);
	SetIniInt('Season', 'numEvents', 1 , seasondir + ini);
end;

function writeini(event, ini:string):Boolean;

var
	seasondir :string;
begin
	result :=true;
	seasondir := ExpandConstant('{app}')+'\seasons\';
	SetIniString(event, 'name', '{#name}', seasondir + ini);
	SetIniString(event, 'shortname', '{#shortname}', seasondir + ini);
	SetIniString(event, 'trackdirectory', '{#trackdir}', seasondir + ini);
	SetIniString(event, 'flag', '{#flag}', seasondir + ini);
	SetIniString(event, 'trackShortName', '{#trackShortName}', seasondir + ini);
	SetIniString(event, 'month', '{#month}', seasondir + ini);
	SetIniString(event, 'day', '{#day}', seasondir + ini);
	SetIniString(event, 'numberOfLaps', '{#numberOfLaps}', seasondir + ini);
	SetIniString(event, 'startingGrid', '{#startingGrid}' + #13#10, seasondir + ini);


end;

function trackinstallchoices_CreatePage(PreviousPageId: Integer): Integer;
var
	Page: TWizardPage;
	ReadMeLines: String;
begin
	Page := CreateCustomPage(
	PreviousPageId,
	ExpandConstant('{cm:trackinstallchoices_Caption}'),
	ExpandConstant('{cm:trackinstallchoices_Description}')
	);

	{ TrackBitmap }
	TrackBitmap := TBitmapImage.Create(Page);
	with TrackBitmap do
	begin
		Parent := Page.Surface;
		Left := ScaleX(8);
		Top := ScaleY(5);
		Width := ScaleX(193);
		Height := ScaleY(145);
		// AutoSize := True;
		Stretch := True;
		if '{#screenshot}' = 'true' then
		begin
			Bitmap.LoadFromFile(ExpandConstant('{tmp}\screenshot.bmp'));
		end
		else
		begin
			Bitmap.LoadFromFile(ExpandConstant('{tmp}\noscreenshot.bmp'));
		end
	end;

	{ LUpdateGP }
	LUpdateGPINI := TLabel.Create(Page);
	with LUpdateGPINI do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_LUpdateGPINI_Caption0}');
		Left := ScaleX(25);
		Top := ScaleY(213);
		Width := ScaleX(260);
		Height := ScaleY(13);
	end;

	{ LUpdate67SEASONINI }
	LUpdate67SEASONINI := TLabel.Create(Page);
	with LUpdate67SEASONINI do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_LUpdate67SEASONINI_Caption0}');
		Left := ScaleX(25);
		Top := ScaleY(157);
		Width := ScaleX(260);
		Height := ScaleY(13);
	end;

	{ LInstallREPLAY }
	LInstallREPLAY := TLabel.Create(Page);
	with LInstallREPLAY do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_LInstallREPLAY_Caption0}');
		Left := ScaleX(25);
		Top := ScaleY(185);
		Width := ScaleX(260);
		Height := ScaleY(13);
	end;

	{ UpdateGPINI }
	UpdateGPINI := TCheckBox.Create(Page);
	with UpdateGPINI do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_UpdateGPINI_Caption0}');
		Left := ScaleX(8);
		Top := ScaleY(211);
		Width := ScaleX(17);
		Height := ScaleY(17);
		Checked := False;
		State := cbChecked;
		TabOrder := 0;
	end;

	{ Update67SEASONINI }
	Update67SEASONINI := TCheckBox.Create(Page);
	with Update67SEASONINI do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_Update67SEASONINI_Caption0}');
		Left := ScaleX(8);
		Top := ScaleY(155);
		Width := ScaleX(17);
		Height := ScaleY(17);
		Checked := True;
		State := cbChecked;
		TabOrder := 1;
	end;

	{ InstallREPLAY }
	InstallREPLAY := TCheckBox.Create(Page);
	with InstallREPLAY do
	begin
		Parent := Page.Surface;
		Caption := ExpandConstant('{cm:trackinstallchoices_InstallREPLAY_Caption0}');
		Left := ScaleX(8);
		Top := ScaleY(183);
		Width := ScaleX(17);
		Height := ScaleY(17);
		Checked := True;
		State := cbChecked;
		TabOrder := 2;
	end;

	{ ReadMeMemo }
	ReadMeMemo := TMemo.Create(Page);
	with ReadMeMemo do
	begin
		Parent := Page.Surface;
		Left := ScaleX(208);
		Top := ScaleY(5);
		Width := ScaleX(201);
		Height := ScaleY(145);
		TabStop := False;
		Font.Height := ScaleY(-9);
		// Lines.Add(ExpandConstant('{cm:trackinstallchoices_ReadMeMemo_Line0}'));
		ReadOnly := True;
		ScrollBars := ssVertical;
		TabOrder := 3;
		WantReturns := False;
		// Fill Memo with readme
		LoadStringFromFile(ExpandConstant('{tmp}\readme.txt'),ReadMeLines);
		Lines.Add(ReadMeLines);
		WordWrap := true;
	end;
	Result := Page.ID;
end;


function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo,
  MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
	S: String;
begin
	{ Fill the 'Ready Memo' with the normal settings and the custom settings }
	S := '';
	S := S + 'Selected GPL Directory:' + Newline;
	S := S + Space + WizardDirValue + NewLine + Newline;
	S := S + 'Adjust 67SEASON.INI: ';
	if Update67SEASONINI.Checked then S := S + 'Yes' else S := S + 'No';
	S := S + Newline + Newline;
	S := S + 'Install Replay: ';
	if InstallREPLAY.Checked then S := S + 'Yes' else S := S + 'No';
	S := S + Newline + Newline;
	S := S + 'Adjust GP.INI: ';
	if UpdateGPINI.Checked then S := S + 'Yes' else S := S + 'No';
	S := S + Newline + Newline;
	Result := S;
end;

function checktrackdir():boolean;
var
	trackdir : String;

begin
	result :=false;
	trackdir := ExpandConstant('{app}')+'\tracks\{#trackdir}';
	// Check if trackdir exist
	if DirExists(trackdir) then
	begin
		if msgbox('It looks like track has been installed because the installer found this trackdir:' + #13 + Trackdir + #13 + 'depending on your choosen GPL directory !' + #13 + 'Do you want to adjust your GPL directory ?',mbConfirmation, MB_YESNO or MB_DEFBUTTON1)= IDYES then
		begin
		result:=true;
		end;
	end;
end;


procedure CurStepChanged(CurStep: TSetupStep);
var
	seasondir:string;
	checknumevents,i :LongInt;

begin
case CurStep of
	ssPostInstall:
	begin
		seasondir := ExpandConstant('{app}')+'\seasons\';
		// MsgBox('Postinstall',mbInformation,MB_OK);
		if UpdateGPINI.Checked and checkfilegpini then
		begin
			// MsgBox('GP.INI checked',mbInformation,MB_OK);
			// Now it can be that we have a max of 64 but the trackdir is there
			// We will check now
			if checktrackgpini then
			begin
				checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir+'gp.ini');
				for i := 0 to checknumevents do
				begin
					if '{#trackdir}' = GetIniString('Event'+IntToStr(i), 'trackDirectory', 'falsch',seasondir+'gp.ini') then writeini('Event'+IntToStr(i),'gp.ini');
				end;
			end
			else
			begin
				// We must add it to the last
				i := GetIniInt('Season','numEvents',65,1,64,seasondir+'gp.ini') + 1;
				// Now we write the new numevent
				SetIniInt('Season','numEvents',i,seasondir+'gp.ini');
				// Now we add the new event
				writeini('Event'+IntToStr(i),'gp.ini');
			end;
		end
		if Update67SEASONINI.Checked and checkfile67seasonini then
		begin
			// MsgBox('67SEASON.INI checked',mbInformation,MB_OK);
			// Now it can be that we have a max of 64 but the trackdir is there
			// We will check now
			if checktrack67seasonini then
			begin
				checknumevents := GetIniInt('Season', 'numEvents', 65, 0, 64,seasondir+'67season.ini');
				for i := 0 to checknumevents do begin
					if '{#trackdir}' = GetIniString('Event'+IntToStr(i), 'trackDirectory', 'falsch',seasondir+'67season.ini') then writeini('Event'+IntToStr(i),'67season.ini');
				end;
			end
			else
			begin
				// We must add it to the last
				i := GetIniInt('Season','numEvents',65,1,64,seasondir+'67season.ini') + 1;
				// Now we write the new numevent
				SetIniInt('Season','numEvents',i,seasondir+'67season.ini');
				// Now we add the new event
				writeini('Event'+IntToStr(i),'67season.ini');
			end;
		end;
		// We need to check the inifiles
		// GP.INI
		if not checkfilegpini and UpdateGPINI.Checked then
		begin
			//Header
			if not Direxists(seasondir) then createdir(seasondir);
			writeiniheader('gp.ini');
			//Eventinfo itself
			writeini('Event1','gp.ini');
		end;
		// 67SEASON.INI
		if not checkfile67seasonini and Update67SEASONINI.Checked then
		begin
			//Header
			if not Direxists(seasondir) then createdir(seasondir);
			writeiniheader('67season.ini');
			//Eventinfo itself
			writeini('Event1','67season.ini');
		end;
		// Now we check the replay switch
		if InstallREPLAY.Checked and replays then
		begin
			FileCopy(ExpandConstant('{tmp}\{#replayfilename}'),ExpandConstant('{app}\replay\{#replayfilename}'),false)
		end;
	end;
	end;
end;

function CheckGPL(Path: String):Boolean;
begin
Result:= FileExists(Path + '\GPL.EXE');
	if not Result then  MsgBox(ExpandConstant('{cm:checkgpl}'), mbInformation, MB_OK)
end;


function NextButtonClick(CurPageID: Integer): boolean;
begin

result:=true;
	if CurPageID=wpSelectDir then
		begin
		// Check for GPL.exe in selected directory
		if not CheckGPL(WizardDirValue) then
		begin
			result:=false;
			exit;
		end;
		// if trackdir is existing make a small warning
		if checktrackdir then
		begin
			result:=false;
			exit;
		end;
		// ExtractTemporaryFile('screenshot.bmp');
		// Reset to defaults !
		// GP.INI
		LUpdateGPINI.Caption := ExpandConstant('{cm:trackinstallchoices_LUpdateGPINI_Caption0}');
		LUpdateGPINI.Color := -16777201;
		UpdateGPINI.Checked := false;
		UpdateGPINI.enabled := true;
		// 67SEASON.INI
		LUpdate67SEASONINI.Caption := ExpandConstant('{cm:trackinstallchoices_LUpdate67SEASONINI_Caption0}');
		LUpdate67SEASONINI.Color := -16777201;
		Update67SEASONINI.Checked := true;
		Update67SEASONINI.enabled := true;
		// GP.INI
		// First is GP.INI there ?
		if checkfilegpini then begin
			// We have the file
			// now let's check for the 64 Track Limit
			if not eventsokgpini and not checktrackgpini then
			begin
				if MsgBox('You have reached 64 max tracks in gp.ini !' + #13#10 + 'Are you using the new tracklimit of 256 ?' + #13#10 + 'If you are unsure accept No because GPL will not work with more then 64 tracks on unpatched GPL.exe', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES then
				begin
					// user clicked Yes
					LUpdateGPINI.Caption :='Reached max 64 tracks in gp.ini (Tracks for World Championship Mode)'+#13+'GPL Patch must be installed';
					LUpdateGPINI.Color := 65535;
					UpdateGPINI.Checked := true;
					UpdateGPINI.enabled := true;
				end
				else
				begin
					// user clicked No
					LUpdateGPINI.Caption :='Reached max 64 tracks in gp.ini (Tracks for World Championship Mode)'+#13+'Please adjust (GEM+)';
					LUpdateGPINI.Color := 65535;
					UpdateGPINI.Checked := false;
					UpdateGPINI.enabled := false;
				end;
			end;
		end;
		// 67SEASON.INI
		// First is 67SEASON.INI there ?
		if checkfile67seasonini then begin
			// We have the file
			// now let's check for the 64 Track Limit
			if not eventsok67seasonini and not checktrack67seasonini then
			begin
				if MsgBox('You have reached 64 max tracks in 67season.ini !' + #13#10 + 'Are you using the new tracklimit of 256 ?' + #13#10 + 'If you are unsure accept No because GPL will not work with more then 64 tracks on unpatched GPL.exe', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES then
				begin
				// user clicked yes
					LUpdate67SEASONINI.Caption :='Reached max 64 tracks in 67season.ini (Tracks for Single Race Mode)'+#13+'GPL Patch must be installed';
					LUpdate67SEASONINI.Color := 65535;
					Update67SEASONINI.Checked := true;
					Update67SEASONINI.enabled := true;
				end
				else
				begin
					// User clicked no
					LUpdate67SEASONINI.Caption :='Reached max 64 tracks in 67season.ini (Tracks for Single Race Mode)'+#13+'Please adjust (GEM+)';
					LUpdate67SEASONINI.Color := 65535;
					Update67SEASONINI.Checked := false;
					Update67SEASONINI.enabled := false;
				end;
			end;
		end;
		// Check the track Folder for replay
		if not replays then
			begin
				LInstallReplay.Caption :='No replay available';
				InstallReplay.Checked := false;
				InstallReplay.enabled := false;
		end;
    end;
	if CurPageID=100 then
	begin

		if not UpdateGPINI.Checked and not UPDATE67SEASONINI.Checked then
		begin
				if msgbox('Please adjust your selection. With no update for GP.INI and no update for 67season.ini Track can be installed but you will not be able to use it unless you enable it via GEM. Is this really what you want to do?',mbCriticalError,MB_YESNO or MB_DEFBUTTON2) = IDYES then result := true else result := false ;
		end;
	end;
end;

procedure InitializeWizard();
begin
  trackinstallchoices_CreatePage(wpSelectDir);
end;



[Messages]
SetupWindowTitle={#dialogtitle}
SetupAppTitle={#dialogtitle}
WelcomeLabel1={#dialogtitle}
WelcomeLabel2=This will install/update GPL Track: {#dialogtitle} on your computer.%n%nIt is recommended that you close all other applications before continuing.
WizardSelectDir=Select your GPL directory (usually C:\Sierra\GPL)
SelectDirLabel3=The GPL Track Installer will add/update {#dialogtitle} to the following selected GPL Installation.
SelectDirBrowseLabel=To continue, click Next. If you would like to select a different GPL Installation, click Browse.
SelectDirDesc=Where is your GPL Base Directory ?
