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

(set #CFG_APPNAME "Tower of Babel Save Disk")
(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."
			)
		)
	))
))

;------------------------------------------------------------------------------------
(procedure P_InstallGame (

	(set #VSAVEDONE 0)
	(set #VDSKNUM 1)

	(until (= 1 #VSAVEDONE)(
		(set #VDSKNUMR (+ 1 #VDSKNUM))
		(working "Examining save disk " #VDSKNUM)

		; determine status of this disk - missing, blank or used
		(set #VT1 (tackon #VDATDIR (cat "Disk." #VDSKNUMR)))
		(set #VSTS 0)
		(if (= 1 (exists #VT1))(
			(set #VT2 (getsum #VT1))
			(if (= 16257030 #VT2)(
				; blank
				(set #VSTS 1)
			)(
				; changed, assume something saved on it
				(set #VSTS 2)
			))
		))

		; set prompt and options for this disk
		(set #VPMT (cat "Save disk " #VDSKNUM " "))

		(if (= 0 #VSTS)(
			(set #VOVR 0)
			(set #VPMT (cat #VPMT "does not exist"))
			(set #VCH0 "Install blank save disk")
			(set #VCH1 "Install existing save disk")
			(set #VCH2 "")
			(set #VDEF 3)
		))

		(if (= 1 #VSTS)(
			(set #VOVR 0)
			(set #VPMT (cat #VPMT "exists and is blank"))
			(set #VCH0 "")
			(set #VCH1 "Overwrite with existing save disk")
			(set #VCH2 "Skip this disk")
			(set #VDEF 2)
		))


		(if (= 2 #VSTS)(
			(set #VOVR 1)
			(set #VPMT (cat #VPMT "exists and contains data"))
			(set #VCH0 "Overwrite with blank save disk")
			(set #VCH1 "Overwrite with existing save disk")
			(set #VCH2 "Skip this disk")
			(set #VDEF 2)
		))

		(set #VCH3 "Finish installation")
		(set #VPMT (cat #VPMT ".\nWhat do you want to do?"))

		(set #VCH
			(askchoice
				(prompt #VPMT)
				(choices #VCH0 #VCH1 #VCH2 #VCH3)
				(default #VDEF)
				(help @askchoice-help)
			)
		)

		; if user is overwriting a used disk, confirm it
		(if (AND (= 1 #VOVR)
				 (OR (= 0 #VCH) (= 1 #VCH)))(
			(if (= 0 (askbool
						(prompt "\n\nTHIS WILL ERASE ALL DATA ON THE SAVE DISK!\n\nProceed?")
						(default 0)
						(help @askbool-help)
					 ))(
				(debug "choice no overwrite")
				(set #VCH 2)
			))
		))

		; do stuff depending on choice
		(set #VT2 -1)
		(set @execute-dir #VDATDIR)

		(if (= 0 #VCH)(
			(working (cat "Creating blank save disk"))
			(set #VT2 (run (cat "TOBMakeSave " #VDSKNUMR)))
		))

		(if (= 1 #VCH)(
			(message "Please insert save disk into drive DF0:")
			(working (cat "Creating blank save disk"))
			(set #VT2 (run (cat "DIC NAME=Disk." #VDSKNUMR " FD=" #VDSKNUMR " LD=" #VDSKNUMR)))
		))

		(if (= 3 #VCH)(
			(set #VSAVEDONE 1)
		))

		; handle errors
		(set @execute-dir #VOLDEXEDIR)

		(if (<> -1 #VT2)(
			(if (= 0 #VT2)(
				(message "\n\nSave disk created!")
			)(
				(abort  "\n\nProblem creating save disk!"
						 "\nError code: " #VT2
						 "\n\nPlease contact the author for further information."
				)
			))
		))

		; move to next disk
		(set #VDSKNUM (+ 1 #VDSKNUM))
		(if (> #VDSKNUM 9) (set #VSAVEDONE 1))
	))
))


;================================================================================
;
(welcome (cat "Welcome to the Tower of Babel Save Disk Installer"))
;

;
; Initialise
;
(set @app-name #CFG_APPNAME)
(set #VOLDEXEDIR @execute-dir)
(set #VDESTDIR @default-dest)
(set #VDATDIR (tackon #VDESTDIR "data/"))

;
; 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 Tower of Babel Save Disk 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
;
(while (<> 2 (exists #VDATDIR))(
	(set #VDESTDIR
		(askdir
			(prompt "Where do you have Tower of Babel installed?\n")
			(help @askdir-help)
			(default @default-dest)
			(disk)
			(newpath)
		)
	)
	(set #VDESTDIR (expandpath #VDESTDIR))
	(set @default-dest #VDESTDIR)
	(set #VDATDIR  (tackon #VDESTDIR "data/"))
))

;
; Install the save disks
;
(P_InstallGame)

;=================================================================================================
;$VER: Tower of Babel Save Disk Installer script R0401.1  1997-2001 John Girvin/Halibut Software 