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

(set #CFG_APPNAME "Tower of Babel")
(set #CFG_APPSLV  "TOBHD")
(set #CFG_APPGUI  "Tower of Babel")
(set #CFG_APPVER  "R0401.1")
(set #CFG_APPCOPY "1997-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))
			(debug #VT1)

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

;------------------------------------------------------------------------------------
; Make game data
;
(procedure P_InstallGame (
	;
	; Set path to game data file
	;
	(set #VDATFILE (tackon #VDATDIR "Disk.1"))

	;
	; 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"
				"Plese insert your game disk into drive DF0:\n\n"
				"Click 'Proceed' when ready."
			)
		)

		;
		; Call the disk image
		;
		(working (cat "Creating data files in '" #VDATDIR "'"))

		(set @execute-dir #VDATDIR)
		(run (cat "DIC NAME=Disk.1 FD=1 LD=1"))
		(set @execute-dir #VOLDEXEDIR)
	))

	;
	; 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 save disk installer
	;
	(copyfiles
		(prompt "Installing save disk installer")
		(help @copyfiles-help)
		(source "InstallSD")
		(newname "Install Save Disks")
		(dest #VDESTDIR)
		(nogauge)
		(optional fail force)
	)
	(copyfiles
		(prompt "Installing save disk installer")
		(help @copyfiles-help)
		(source "InstallSD.inf")
		(newname "Install Save Disks.info")
		(dest #VDESTDIR)
		(nogauge)
		(optional fail force)
	)
	(copyfiles
		(prompt "Installing save disk installer")
		(help @copyfiles-help)
		(source "TOBMakeSave")
		(dest #VDATDIR)
		(nogauge)
		(optional fail force)
	)

	(message "\n\nYou can install save disks containing"
			 "\nlevels or games you have saved using the"
			 "\n\n'Install Save Disks'"
			 "\n\nicon located in the game directory."
    )

	;
	; Clean up previous installations
	;
	(working "Cleaning up previous installation")

	(set #VT1 (tackon #VDATDIR "main"))
	(if (= 1 (exists #VT1)) (delete #VT1 (optional fail force)))
))

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

(procedure P_InstallSlave (
	(working "Installing slave program")

	; first clean up previous installations
	(working "Cleaning up previous installation")

	(set #VT1 (tackon #VDESTDIR "WHDLoad"))
	(if (= 1 (exists #VT1)) (delete #VT1 (optional nofail force)))

    (set #VT1 (tackon #VDESTDIR "TOBHD.info"))
    (set #VT2 (tackon #VDESTDIR "Tower_of_Babel.info"))
	(if (= 1 (exists #VT2))(
		(if (= 1 (exists #VT1)) (delete #VT1 (optional nofail force)) )
	)(
		(if (= 1 (exists #VT1)) (rename #VT1 #VT2) )
	))

    (set #VT1 (tackon #VDESTDIR "Tower_of_Babel.info"))
    (set #VT2 (tackon #VDESTDIR (cat #CFG_APPGUI ".info")))
	(if (= 1 (exists #VT2))(
		(if (= 1 (exists #VT1)) (delete #VT1 (optional nofail force)) )
	)(
		(if (= 1 (exists #VT1)) (rename #VT1 #VT2) )
	))

	; 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 "DIC")
(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: Tower of Babel Installer script R0401.1 © 1997-2001 John Girvin/Halibut Software 