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

(set #CFG_APPNAME "Rockstar Ate My Hamster")
(set #CFG_APPSLV  "RAMHHD")
(set #CFG_APPGUI  "Rockstar Ate My Hamster")
(set #CFG_APPVER  "R0401.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"
				"'" #VPROGRAM "'\n\n"
				"which is required to perform the installation!\n\n"
				"Please install the '" #VPROGRAM "' 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
				(run (cat "CRC16 \"" #VDATFILE "\" OFFSET=0 LENGTH=5120"))
			)

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


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

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

	;
	; 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 data file")
			(help @copyfiles-help)
			(source "DF0:RSTAR")
			(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 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)
	)

	; see if the user wants to replace their 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 "\" program icon (recommended) ?"))
				(default 1)
				(help @askchoice-help)
			)
		)
	))

	; do we have a slave icon?
    (if (= 1 (exists "Slave.inf"))(
		; copy slave icon, renaming to proper name along the way
		(if (= 1 #VT2)(
			(copyfiles
				(prompt "Copying slave program icon")
				(help @copyfiles-help)
				(source "Slave.inf")
				(newname #VT1)
				(dest #VDESTDIR)
				(nogauge)
				(optional nofail force)
			)
		))
    )(
		; no slave icon so we must have a GUI to install
		(copyfiles
			(prompt "Copying GUI program")
			(help @copyfiles-help)
			(source  "GUI")
			(newname #CFG_APPGUI)
			(dest #VDESTDIR)
			(nogauge)
			(optional fail force)
		)

		(if (= 1 #VT2)(
			(copyfiles
				(prompt "Copying GUI program icon")
				(help @copyfiles-help)
				(source  "GUI.inf")
				(newname #VT1)
				(dest #VDESTDIR)
				(nogauge)
				(optional nofail 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 "CRC16")
(P_CheckRun)
(set #VP1 "WHDLoad")
(P_CheckRun)

;
; Welcome message
;
(message "\n\nWelcome to the " #CFG_APPNAME " HD Installer"
		 "\n© " #CFG_APPCOPY " John Girvin/Halibut Software\n\n"
		 "Please read the documentation thoroughly "
		 "before attempting to use this installer!\n\n"
		 "This is release " #CFG_APPVER "\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: Rockstar Ate My Hamster Installer script R0401.1 © 2001 John Girvin/Halibut Software ;================================================================================
