;********************************************************************
;Expert HD configuration script (asks the user for a unit number)
;********************************************************************

;Version 5.04
;August 28, 1992

;Important: Before shipping, replace all references to ":" with ":".

;********************************************************************

;SetHD -- place hard drive information (e.g. C=PCC) in the CrossPC tooltypes
;harddisk -- e.g. PCC
;pcdir -- directory of CrossPC icon

(procedure SetHD
	(
		;If the user has configured a hard drive during this session,
		;add an appropriate tooltype.

		(if
			(not
				(= harddisk "")
			)
			(
				;Grab the last character of harddisk and place it in hdchar
				(set hdchar
					(substr harddisk
						(-
							(strlen harddisk)
							1
						)
						1
					)
				)

				(if
					(not
						(exists
							(tackon pcdir "CrossPC.info")
						)
					)
					(message "Warning: Can't find CrossPC's icon. CrossPC will not be configured.")
				)

				(tooltype
					(dest (tackon pcdir "CrossPC"))
					(noposition)
					(settooltype hdchar harddisk)
				)
			)
		)
	)
)

;********************************************************************

;MountHD -- mount the thing and place junk into s:user-startup
;harddisk -- e.g. PCC

(procedure MountHD
	(
		;If the user has configured a hard drive during this session,
		;mount etc.

		(if
			(not
				(= harddisk "")
			)
			(
				;Mount the hard drive the user just configured.

				(run
					(cat @execute-dir
						(cat "c/mount >nil: devs:DOSDrivers/" harddisk)
					)
				)

				;Install into s:user-startup

				(startup
					(cat "CrossDOSV5+ - mounting hard drive "
						(cat harddisk ":")
					)
					(prompt "About to add a command in s:user-startup to automatically "
					        "mount the hard drive you configured..."
					)
					(help "If you elect not to add the mount command to your user-startup, "
					      "you may mount the drive simply by clicking on its icon."
					)
					(command
						(cat prefix
							(cat "Mount >nil: DEVS:DOSDrivers/" harddisk)
						)
					)
				)
			)
		)
	)
)

;********************** Configure Hard Disk **************************

(procedure ConfigureHardDisk
	(

		;Disallow pretend mode
		(if @pretend
			(
				(message "Sorry, you can't run the hard drive configuration in "
				         "\"pretend\" mode. Exiting..."
				)
				(exit
					(quiet)
				)
			)
		)

		(if
			(= 0
				(exists "l:CrossDOSFileSystem")
			)
			(
				(message "CrossDOS V5 is not currently installed. It must "
				         "be installed before you can configure a hard drive. "
				         "Switching to CrossDOS installation..."
				)
				(InstallCrossDOS)
			)
		)

		(set opt
			(askchoice
				(choices "Configure on an Amiga Partition"
				         "Configure a 100% MS-DOS Hard Disk"
				)
				(prompt "Please select an option. * Hit HELP for details!!! *")
				(help "If you wish to create a simulated MS-DOS hard drive "
				      "on one of your Amiga hard drive partitions (note: "
				      "the MS-DOS information will completely wipe out any Amiga "
				      "files on that partition) select \"Configure on an Amiga "
				      "Partition\". If you want to dedicate an entire PHYSICAL "
				      "hard drive (or Syquest or Floptical or...) to storing "
				      "MS-DOS information, select \"Configure a 100% MS-DOS Hard "
				      "Disk\". Note that the configuration utility does not "
				      "support very old controllers such as the Commodore A2090. "
				      "In that case you must configure a MountList \"by hand\"."
				)
			)
		)

		;Ask for drive prefix

		(set driveprefix
			(askstring
				(default "PC")
				(prompt "Please enter a prefix to be assigned to the MS-DOS "
				        "device you wish to configure.  A 'C' will be added "
						"to the end of the device name for you.  Please choose "
						"a device name that is not already in use.  "
						"\nPress HELP for details."
				)
				(help "You must enter a prefix (which may be any normal "
				      "text, without spaces or any special characters) which will "
				      "be used to access the MS-DOS drive/partition from AmigaDOS. A letter will be "
				      "added by the configuration utility "
				      "to form the complete drive name, as accessed from AmigaDOS. "
				      "The relationship between the AmigaDOS drive name and the "
				      "MS-DOS drive name (accessed by CrossPC or by a real IBM) is very "
				      "simple. The last letter of the AmigaDOS name is the MS-DOS name. "
				      "For example, PCC: refers to MS-DOS drive C:. The prefix to "
				      "enter now would be PC (the default)."
				)
			)
		)

		;Ask for unit number

		(set unitnumber
			(asknumber
				(default 0)
				(prompt "Enter the unit number of the hard drive you want to "
				        "configure."
				)
				(help "Usually the unit number ranges from 0-6 and corresponds "
				      "to the SCSI device number. It is also possible with most "
				      "hard drive controllers to specify the Logical Unit Number (LUN), "
				      "which is used by some Adaptec controllers, and the board number "
				      "(if you have multiple SCSI adapters from the same manufacturer). "
				      "You should consult the documentation of your controller "
				      "for details."
				)
			)
		)

		;Create a temporary directory

		(makedir "ram:CrossDOSTemp")

		(set MountListDir "ram:CrossDOSTemp")
		(makedir MountListDir)

		;Run the ConfigDev program

		(if
			(= opt 0)
			(set drivetype "AMIGA")
			(set drivetype "MSDOS")
		)
		(set runargs
			(cat
				drivetype
				" -fL:CrossDOSFileSystem"
			)
		)
		(set runargs
			(cat
				runargs
				(cat
					" -t"
					MountListDir
				)
			)
		)
		(set runargs
			(cat
				runargs
				(cat
					" -p"
					driveprefix
				)
			)
		)

		;Tack on unit number (e.g. -u0)
		(set runargs
			(cat
				runargs
				(cat
					" -u"
					unitnumber
				)
			)
		)

		(run
			(cat
				(cat @execute-dir "c/ConfigDisk ")
				runargs
			)
		)

		;Create icons for the files in MountListDir and copy to DEVS:DOSDrivers
		(set success 0)
		(foreach MountListDir "#?"
			(
				(set success 1)
				(set okflag 1)

				;Check for "floptical" - ignore

				(if
					(= @each-name "floptical")
					(set okflag 0)
				)

				;Check whether the user already has a mountfile of the same name
				(if okflag
					(if
						(exists
							(cat
								"devs:DOSDrivers/"
								@each-name
							)
						)
						(
							(set okflag
								(askbool
									(prompt "A file called \"" @each-name
									        "\" already exists in your DEVS:DOSDrivers "
									        "directory."
									)
									(choices "Overwrite"
									         "Cancel"
									)
									(help "You may elect to overwrite the mountfile "
									      "that currently exists, or abort the whole "
									      "procedure."
									)
								)
							)
						)
					)
				)

				(if okflag
					(
						(copyfiles
							(source
								(cat
									(cat
										MountListDir
										"/"
									)
									@each-name
								)
							)
							(dest "devs:DOSDrivers")
							(infos)	;just in case the DOSDrivers directory needs to be created.
							(newname @each-name)
						)
						(copyfiles
							(source ":PC0.info")
							(dest "devs:DOSDrivers")
							(newname (cat @each-name ".info"))
						)

						;Determine what default tool to use (c:Mount or
						;c:CDMount).

						;Check to see whether the user has a c:CDMount. If so, set "prefix"
						;to "CD".

						(if
							(exists "c:CDMount")
							(set prefix "CD")
						)

						(tooltype
							(dest
								(cat
									"devs:DOSDrivers/"
									@each-name
								)
							)
							(noposition)
							(setdefaulttool
								(cat
									(cat "c:" prefix)
									"Mount"
								)
							)
							;Delete the "filesystem=" entry
							;Case-sensitive!!! (yet another installer bug)
							(settooltype "FILESYSTEM")
						)
						(set harddisk @each-name)
					)
				)
			)
		)

		;Delete the file created by ConfigDisk
		(if success
			(
				(delete "ram:CrossDOSTemp/floptical")
				(delete
					(cat
						"ram:CrossDOSTemp/"
						harddisk
					)
				)

				;Ask whether he wants to add this drive to CrossPC's tooltypes. etc

				(MountHD)
				(if
					(or
						(= 1
							(exists "sys:CrossPC")
						)
						(= 1
							(exists "sys:CrossPC/CrossPC")
						)
					)
					(
						(if
							;File?
							(= 1
								(exists "sys:CrossPC")
							)
							(
								(set pcdir "sys:")
								(SetHD)
							)
							(
								(set pcdir "sys:CrossPC")
								(SetHD)
							)
						)
					)
					(
						;Ask the user whether he wants to set the CrossPC tooltype
						(if
							(askbool
								(prompt "If you have already installed CrossPC *and* "
								        "you wish to make the hard drive you configured "
								        "accessible to CrossPC, select \"Yes\"."
								)
								(help "If you select \"yes\", the installer will ask you "
								      "where CrossPC has been installed. The installer "
								      "will proceed to add a tooltype to the CrossPC icon."
								)
							)
							(
								(set notok 1)
								(while notok
									(
										(set pcdir
											(askdir
												(prompt "Select the directory in which you "
												        "installed CrossPC."
												)
												(help "The installer will proceed to add a tooltype "
												      "to the CrossPC icon."
												)
												(default "sys:")
											)
										)
										(if
											(= 1
												(exists
													(tackon pcdir "CrossPC")
												)
											)
											(
												(SetHD)
												(set notok 0)
											)
											(
												(message "CrossPC is not in the directory you selected.")
											)
										)
									)
								)
							)
						)
					)
				)
				(message "Hard drive configuration is completed."
					"\n\n############  WARNING!!!  ############"
					"\nDO NOT use our Format command or the Amiga format command "
					"to format " harddisk ":.  Instead, you must treat the "
					"hard disk you just configured as an unformatted MS-DOS hard disk.  "
					"This means you must use the FDISK and FORMAT commands "
					"provided by MS-DOS to partition and format the hard disk.  "
					"(Consult your MS-DOS manual about the use of these commands.)  "
					"Failure to heed this warning may cause unpredictable results."
				)
			)
			(
				;else (not successful)
				(message "Hard drive configuration was not successful. "
				         "If the configuration window didn't appear, it means (most likely) "
				         "that the unit number you specified doesn't exist."
				)
			)
		)

		;Delete the temporary directory created by this script
		(delete "ram:CrossDOSTemp")
	)
)

;***************************************************************************
;***************************** MAIN ****************************************
;***************************************************************************

;Force user level to 1 or 2. This is unfortuntely required because otherwise
;we can't present a menu of choices to the user.
(if
	(= @user-level 0)
	(
		(user 1)
		(set @user-level 0)
	)
)


;Test whether we can access :l/CrossDOSFileSystem. If not, the user is
;not running from the product disk - abort.

(if
	(not
		(exists ":l/CrossDOSFileSystem")
	)
	(
		(message "You must run the installation program from the "
		         "CrossDOS disk. Exiting..."
		)
		(exit
			(quiet)
		)
	)
)

;Initialize certain variables
(set pcdir "")
(set harddisk "")

(set @execute-dir
	(expandpath ":")
)

;Check to see whether the user has a c:CDMount. If so, set "prefix"
;to "CD".

(if
	(exists "c:CDMount")
	(set prefix "CD")
)

(ConfigureHardDisk)
(exit
	(quiet)
)

