;
;$VER: Paradroid90 HD Installer script v1.00 (c) 1999 John Girvin/Halibut Software ;

;------------------------------------------------------------------------------------
; Checks if given program is reachable via the path
; if not abort install

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

;------------------------------------------------------------------------------------
; Set information about disk image

(procedure S_GetImageInfo
(
	(set #PVERTYP -1)
	(if (= 1 (exists (tackon #PDATDIR "Disk.1")))
		(
			; Image exists - get its CRC and set version

			(set #PCRC
				(run (cat "CRC16 " (tackon #PDATDIR "Disk.1") " OFFSET=3942 LENGTH=22528"))
			)

			(if (= #PCRC 52266)
				(set #PVERTYP 1)
				(set #PVERTYP 0)
			)
		)
	)
))


;------------------------------------------------------------------------------------
; Make installation directories

(procedure S_InstallDirs
(
	;
	; Create main dir if required
	;
	(if (<> 2 (exists #PDESTDIR))
		(makedir #PDESTDIR
			(prompt "The directory '" #PDESTDIR "' will now be created")
			(help @makedir-help)
			(infos)
			(confirm)
		)
	)

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

	;
	; Copy disk drawer icon
	;
	(if (= 0 #PUPGRADE)
		(copyfiles
			(prompt "Copying HD drawer icon")
			(help @copyfiles-help)
			(source "Drawer.inf")
			(newname "Paradroid90.info")
			(dest #PROOTDIR)
			(nogauge)
			(optional fail force)
		)
	)
))

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

(procedure S_InstallGame
(
	;
	; See if we need to create the data file
	;
	(if (<> 1 (exists (tackon #PDATDIR "Disk.1")))
		(
			;
			; Get game disk inserted
			;
			(message
				(cat
					"\n\n\nPlease insert your Paradroid90 game disk in drive " #PDEVICE "\n\n"
					"Click 'Proceed' when ready."
				)
			)

			;
			; Copy imager slave to data dir
			;
			(copyfiles
				(prompt "Copying disk image creation slave")
				(help @copyfiles-help)
				(source "ISlave")
				(dest #PDATDIR)
				(nogauge)
				(optional fail force)
			)

			;
			; Make the disk image
			;
			(working "Creating disk image in '" #PDATDIR "'")

			(set @execute-dir #PDATDIR)
			; (run (cat "RawDIC SLAVE=ISlave"))
			(set @execute-dir #POLDEXEDIR)

			;
			; Remove the imager slave
			;
			(delete
				(tackon #PDATDIR "ISlave")
				(prompt "Deleting disk image creation slave")
				(optional force)
			)
		)
	)

	;
	; Check the disk image was created and
	; that we know this version of the game
	;
	(S_GetImageInfo)

	(if (= 0 #PVERTYP)
		(abort
			(cat	"\n*** UNKNOWN VERSION OF GAME! ***\n\n"
					"This version of P90Install does not recognise\n"
					"the version of Paradroid90 you are trying to install.\n\n"
					"Please contact the author of P90Install\n"
					"for further information."
			)
		)
	)

	(if (= -1 #PVERTYP)
		(abort
			(cat	"\n*** DISK IMAGE NOT CREATED! ***\n\n"
					"P90Install was not able to create a disk image file.\n\n"
					"Please contact the author of P90Install\n"
					"for further information."
			)
		)
	)

	(if (= 0 #PUPGRADE)
		(message "\n\nDisk image created successfuly!")
	)
))

;------------------------------------------------------------------------------------
; Make correct version of WHDLoad slave program

(procedure S_InstallSlave
(
	;
	; Create slave from base file and patch
	;
	(working "Creating slave program")

	(set #PSLVPCHNAM (cat "v" #PVERTYP ".wpch"))

	(set #PRCODE
		(run
			(cat "WPatch Slave.base "
					(tackon #PDESTDIR "P90HD")
					" " #PSLVPCHNAM
			)
		)
	)

	(if (<> #PRCODE 0)
		(abort
			(cat "Slave generation failed error " #PRCODE)
		)
	)
))

;------------------------------------------------------------------------------------
; Install the trainer GUI

(procedure S_InstallGUI
(
	;
	; Copy GUI program
	;
	(copyfiles
		(prompt "Copying GUI program")
		(help @copyfiles-help)
		(source "GUI")
		(newname "Paradroid90_HD")
		(dest #PDESTDIR)
		(nogauge)
		(optional fail force)
	)

	;
	; Copy GUI icon
	;
	(if (= 0 #PUPGRADE)
		(copyfiles
			(prompt "Copying GUI icon")
			(help @copyfiles-help)
			(source "GUI.inf")
			(newname "Paradroid90_HD.info")
			(dest #PDESTDIR)
			(nogauge)
			(optional fail force)
		)
	)
))

;------------------------------------------------------------------------------------
(welcome "Welcome to the Paradroid90 HD Installer")
(set @app-name "Paradroid90")
(set #PDEVICE  "DF0:")
(set #PROOTDIR "RAM:")
(set #PDESTDIR "")
(set #PDATDIR  "")
(set #PDEFTRAINERS 0)
(set #PSTARTMISSION 0)
(set #POLDEXEDIR @execute-dir)

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

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

;
; Check programs available
;
(set #PPROGRAM "CRC16")
(S_CheckRun)
(set #PPROGRAM "RawDIC")
(S_CheckRun)
(set #PPROGRAM "WHDLoad")
(S_CheckRun)

;
; Nice friendly welcome message
;
(message "\n\nWelcome to the Paradroid90 HD Installer\n"
		 "(c) 1999 John Girvin/Halibut Software\n\n"
		 "Please read the documentation thoroughly "
		 "before attempting to use this installer!\n\n"
		 "This is the unregistered evaluation version 1.00\n"
		 "Please refer to the documentation for information "
		 "on how to obtain the full, registered version of "
		 "this product!\n\n"
		 "Click 'Proceed' to begin..."
)

;
; Install or upgrade?
;
(set #PUPGRADE
	(askbool
		(prompt "Do you have an existing installation of\n"
				"Paradroid90 ?")
		(help @askchoice-help)
		(choices "Yes" "No")
		(default 0)
	)
)

;
; Get device to install from
;
(if (= 0 #PUPGRADE)
	(
		(set #PDEVICE
			(askchoice
				(prompt "Which drive would you like to install from?")
				(choices "DF0:" "DF1:" "Other...")
				(default 0)
				(help @askchoice-help)
			)
		)

		(select #PDEVICE
			(set #PDEVICE "DF0:")
			(set #PDEVICE "DF1:")
			(set #PDEVICE
				(askstring
					(prompt "Enter drive to install from:")
					(default "DF0:")
					(help @askstring-help)
				)
			)
		)
	)
)

;
; Get directory to install in,
; or directory Paradroid90 is currently in
;
(if (= 0 #PUPGRADE)
	;
	; New installation
	;
	(set #PROOTDIR
		(askdir
			(prompt "Where would you like Paradroid90 installed?\n"
					"(a new drawer called 'Paradroid90' will be created here)"
			)
			(help @askdir-help)
			(default #PROOTDIR)
		)
	)

	;
	; Upgrade existing installation
	;
	(while (<> 2 (exists (tackon #PROOTDIR "Paradroid90")))
		(set #PROOTDIR
			(askdir
				(prompt "Where do you have Paradroid90 installed?\n"
						"(select the directory containing the 'Paradroid90' drawer)"
				)
				(help @askdir-help)
				(default #PROOTDIR)
			)
		)
	)
)
(set #PDESTDIR (tackon #PROOTDIR "Paradroid90/"))
(set #PDATDIR  (tackon #PDESTDIR "data/"))
(set @default-dest #PDESTDIR)

;
; Create the installation directories
;
(S_InstallDirs)

;
; Install the game data
;
(S_InstallGame)

;
; Generate and install the slave
;
(S_InstallSlave)

;
; Install the trainer GUI
;
(S_InstallGUI)
