;================================================================================
; CONFIGURATION SECTION

(set #CFG_APPNAME "Hard Drivin'")
(set #CFG_APPGUI  "Hard Drivin'")
(set #CFG_APPSLV  "HDHD")
(set #CFG_APPVER  "R0601.1")
(set #CFG_APPCOPY "2001")

;================================================================================

;------------------------------------------------------------------------------------
; Checks if given program is reachable via the path, if not abort install
;
; Entry:	#VP1 = name of program to search for

(procedure P_CheckRun (
	(if (<> 0 (run (cat "Which " #VP1)))(
	    (abort
			(cat
				"Could not find the program\n\n"
				"'" #VP1 "'\n\n"
				"which is required to perform the installation!\n\n"
				"Please install the '" #VP1 "' program ensuring that"
				" it is accessible on the path, then try the installation again."
			)
		)
	))
))


;------------------------------------------------------------------------------------
; Create installation directories if required

(procedure P_InstallDirs (
	;
	; Create data dir if required
	;
	(if (<> 2 (exists #VDATDIR))(
		(makedir #VDATDIR
			(prompt "The directory '" #VDATDIR "' will now be created")
			(help @makedir-help)
			(confirm)
		)
	))
))


;------------------------------------------------------------------------------------
; Determine and set information about version of game being installed
;

(procedure P_SetVersionInfo (
	; if the file exists, set version information
	(if (<= #VVERINFA 0)(
		(if (= 1 (exists #VDATFILE))(
			; determine version from CRC
			(working "Determining game version")

			(set #VT1 (getsum #VDATFILE))

			(set #VVERINFA 0)
			(if (= #VT1 100828108) (set #VVERINFA 1))
		))
	))
))


;------------------------------------------------------------------------------------
; Make game data

(procedure P_InstallGame (
	;
	; Set path to game data file
	;
	(set #VDATFILE (tackon #VDATDIR "HardDrivin"))

	;
	; Install data files if not found or unknown versions
	;
	(P_SetVersionInfo)
	(if (<= #VVERINFA 0)(
		;
		; Tell user what is happening
		;
		(message
			(cat
				"\n\n\nThe installer will now create the game data files\n"
				"Please insert your game disk in drive DF0:\n\n"
				"Click 'Proceed' when ready."
			)
		)

		;
		; Call the imager slave
		;
		(working "Creating data files in '" #VDATDIR "'")

		(copyfiles
			(prompt "Copying game files to '" #VDATDIR "'")
			(help @copyfiles-help)
			(source  "DF0:")
			(pattern "~(c|l|s|system-configuration)")
			(dest #VDATDIR)
			(optional fail force)
		)
	))

	;
	; Check the main data file was created and
	; that we know this version of the game
	;
	(P_SetVersionInfo)

	(if (= 0 #VVERINFA)
		(abort
			(cat	"\n*** UNKNOWN VERSION OF GAME! ***\n\n"
					"The installer does not recognise the version of the\n"
					"game that you are trying to install.\n\n"
					"Please contact the author for further information\n"
			)
		)
	)

	(if (= -1 #VVERINFA)
		(abort
			(cat	"\n*** DATA FILES NOT CREATED! ***\n\n"
					"The installer could not extract the game data files.\n\n"
					"Please contact the author for further information\n"
			)
		)
	)

	;
	; install default hiscore file if not found
	;
	(set #VT1 (tackon #VDATDIR "hiscore.dat"))
	(if (<> 1 (exists #VT1))(
		(copyfiles
			(prompt "Copying default high scores")
			(help @copyfiles-help)
			(source "hiscore.dat")
			(dest #VDATDIR)
			(nogauge)
			(optional fail force)
		)
	))
))

;------------------------------------------------------------------------------------
; Install WHDLoad slave program

(procedure P_InstallSlave (

	(working "Installing slave program")

	; copy slave program
	(set #VT1 "Slave")
	(if (>= #VVERINFA 0) (set #VT1 (cat #VT1 #VVERINFA)) )
	(if (>= #VVERINFB 0) (set #VT1 (cat #VT1 #VVERINFB)) )
	(if (>= #VVERINFC 0) (set #VT1 (cat #VT1 #VVERINFC)) )

	(copyfiles
		(prompt "Copying slave program")
		(help @copyfiles-help)
		(source #VT1)
		(newname #CFG_APPSLV)
		(dest #VDESTDIR)
		(nogauge)
		(optional fail force)
	)

	(copyfiles
		(prompt "Copying OSEmu module")
		(help @copyfiles-help)
		(source "OSEmu.400")
		(dest #VDATDIR)
		(nogauge)
		(optional fail force)
	)

	; copy GUI program if present
    (if (= 1 (exists "GUI"))(
		(copyfiles
			(prompt "Copying GUI program")
			(help @copyfiles-help)
			(source  "GUI")
			(newname #CFG_APPGUI)
			(dest #VDESTDIR)
			(nogauge)
			(optional fail force)
		)
	))

	; see if the user wants or needs to install an icon

	(set #VT1 (cat #CFG_APPGUI ".info"))
	(if (= 0 (exists (tackon #VDESTDIR #VT1)))(
		; no icon exists, quietly copy one in
		(set #VT2 1)
	)(
		; icon exists, ask user if they want to overwrite it
		(set #VT2
			(askbool
				(prompt (cat "Do you want to replace the \"" #CFG_APPGUI "\" icon (recommended) ?"))
				(default 1)
				(help @askchoice-help)
			)
		)
	))

	; install the icon if required
	(if (= 1 #VT2)(
		; get choice of icon format
		(set #VT3
			(askchoice
				(prompt "Which type of icon would you like to install?")
				(choices "Standard" "NewIcon" "OS3.5+ Coloricon")
				(help @askchoice-help)
			)
		)
		(if (= 0 #VT3) (set #VT3 ".oi"))
		(if (= 1 #VT3) (set #VT3 ".ni"))
		(if (= 2 #VT3) (set #VT3 ".ci"))

		; install the icon
		(copyfiles
			(prompt "Copying icon")
			(help @copyfiles-help)
			(source (cat "Icon" #VT3))
			(newname #VT1)
			(dest #VDESTDIR)
			(nogauge)
			(optional fail force)
		)
	))
))

;------------------------------------------------------------------------------------
(welcome (cat "Welcome to the " #CFG_APPNAME " HD Installer"))

;
; Initialise
;
(set @app-name #CFG_APPNAME)
(set #VOLDEXEDIR @execute-dir)
(set #VVERINFA -1)
(set #VVERINFB -1)
(set #VVERINFC -1)
(set #VDESTDIR @default-dest)

;
; Check settings
;
(if	(<> @user-level 2)
	(abort "You must select 'Expert' user level")
)

(if	(< @installer-version 2818051)
	(abort "This product requires at least version 43.3 of the Installer program")
)

;
; Check required programs are available
;
(set #VP1 "WHDLoad")
(P_CheckRun)

;
; Welcome message
;
(message "\n\n\nWelcome to the " #CFG_APPNAME " HD Installer " #CFG_APPVER "\n\n"
		 "© " #CFG_APPCOPY " Halibut Software\n\n"
		 "Please read the documentation thoroughly "
		 "before attempting to use this installer!\n\n"
		 "Click 'Proceed' to begin..."
)

;
; Get directory to install in
;
(set #VDESTDIR
	(askdir
		(prompt "Where would you like the game installed?\n"
				"If you have an existing installation select that directory,\n"
				"otherwise create / select the directory to install the game into."
		)
		(help @askdir-help)
		(default @default-dest)
		(disk)
		(newpath)
	)
)
(set #VDESTDIR (expandpath #VDESTDIR))
(set @default-dest #VDESTDIR)
(set #VDATDIR  (tackon #VDESTDIR "data/"))

;
; Create the installation directories
;
(P_InstallDirs)

;
; Install the game data
;
(P_InstallGame)

;
; Install the slave
;
(P_InstallSlave)

;====================================================================
;$VER: Hard Drivin' Installer script R0601.1 © 2001 Halibut Software 