;
;$VER: Zeewolf 2 HD Installer script v3.01 (c) 1996-99 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 " #ZPROGRAM)))
	    (abort
			(cat
				"Could not find the program\n\n"
				"'" #ZPROGRAM "'\n\n"
				"required to perform the installation!\n\n"
				"Please install the '" #ZPROGRAM "' program ensuring that\n"
				"it is accessible on the path, then try the installation again."
			)
		)
	)
))

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

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

			(set #ZCRC
				(run (cat "CRC16 " (tackon #ZDATDIR "Disk.1") " LENGTH=5632"))
			)

			(if (= #ZCRC 24632)
				(set #ZVERTYP 1)
				(set #ZVERTYP 0)
			)
		)
	)
))

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

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

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

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

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

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

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

			(set @execute-dir #ZDATDIR)
			(run (cat "DIC DEVICE=" #ZDEVICE " SIZE=901120 FD=1 LD=1"))
			(set @execute-dir #ZOLDEXEDIR)
		)
	)

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

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

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

	(if (= 0 #ZUPGRADE)
		(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 #ZSLVPCHNAM (cat "v" #ZVERTYP ".wpch"))

	(set #ZRCODE
		(run
			(cat "WPatch Slave.base "
					(tackon #ZDESTDIR "Z2HD")
					" " #ZSLVPCHNAM
			)
		)
	)

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

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

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

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

;------------------------------------------------------------------------------------
(welcome "Welcome to the Zeewolf 2 HD Installer")
(set @app-name "Zeewolf")
(set #ZDEVICE  "DF0:")
(set #ZROOTDIR "RAM:")
(set #ZDESTDIR "")
(set #ZDATDIR  "")
(set #ZDEFTRAINERS 0)
(set #ZSTARTMISSION 0)
(set #ZOLDEXEDIR @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 #ZPROGRAM "CRC16")
(S_CheckRun)
(set #ZPROGRAM "DIC")
(S_CheckRun)
(set #ZPROGRAM "WHDLoad")
(S_CheckRun)

;
; Nice friendly welcome message
;
(message "\n\nWelcome to the Zeewolf 2 HD Installer\n"
		 "(c) 1996-99 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 3.01\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 #ZUPGRADE
	(askbool
		(prompt "Do you have an existing installation of\n"
				"Zeewolf 2 v3.00+ ?")
		(help @askchoice-help)
		(choices "Yes" "No")
		(default 0)
	)
)
(debug #ZUPGRADE)

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

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

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

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

;
; 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)
