;********************************************************************
;Installation script for CrossDOS V7 Gold
;********************************************************************

;$VER: Install 7.00 (1.12.97)

;Bugs in Installer V1.22:

;1. The "run" command does not accept a relative path.
;2. The tooltype delete feature is case-sensitive.

;mycopylib is used because of installer limitations. It uses the following global
;strings as input:
;mysource - source
;mydest - destination
;mynewname - new name

(procedure mycopylib
	(	(set cpyfiles 0)
		;form destination file
		(set destfile (tackon mydest mynewname) )
		(if (= 0 (exists destfile) ) 
			(set cpyfiles 1)
			(	;the destfile exists, so we must check the versions
				(if (exists mysource)
					(	(set sourcevernum (getversion mysource))
						(set sourcever (/ sourcevernum 65536))
						(set sourcerev (- sourcevernum (* sourcever 65536)))
						(set destvernum (getversion destfile))
						(set destver (/ destvernum 65536))
						(set destrev (- destvernum (* destver 65536)))
						(if (or (or (not ks20) (> sourcever destver) )
								(and (= sourcever destver) (>= sourcerev destrev) )
							)
							(set cpyfiles 1)
						)
					)
				)
			)
		)
		(if (= cpyfiles 0)
			;Handle a version conflict
			(if (= @user-level 2)
				(if (askbool
						(prompt ("The version of \"%s" mynewname)
								 "\" currently on your system appears to be "
								 "newer than the CrossDOS version. Select \"Skip\" to "
								 "preserve the version currently on your system, "
								 "or \"Overwrite\" to install the CrossDOS version "
								 "regardless."
						)
						(choices "Overwrite"
								 "Skip"
						)
						(help "This message should only appear if you are "
							  "running Workbench 2.1 or later. If you are not, "
							  "select Overwrite to copy the CrossDOS file anyway."
						)
					)
					(set cpyfiles 1)
				)
			)
		)
	)
	(if (= cpyfiles 1)
		; check if icon already there.  If so don't copy icon.
		(if (not (exists ("%s.info" (tackon mydest mynewname)) (noreq)) )
			(copyfiles (source mysource) (dest mydest) (newname mynewname) (infos) )
			;else
			(copyfiles (source mysource) (dest mydest) (newname mynewname) )
		)
	)
)

;********************** CrossDOS UNinstallation **************************
;Enter with "deletedirstr" set to the name of the directory to terminate
;and deletepat with the amigados wildcards.
(procedure deletedir
	(if (= 2 (exists deletedirstr) )
		(	(foreach deletedirstr deletepat
				(delete (tackon deletedirstr @each-name) )
			)
			(delete (tackon deletedirstr ".info") )
			(delete deletedirstr)
			(delete ("%s.info" deletedirstr) )
		)
	)
)

(procedure DeleteOldCD
  (	;misc
	(delete "sys:wbstartup/mountmf")
	(delete "sys:wbstartup/mountmf.info")

;sys:system
	(delete "sys:system/diskchg")
	(delete "sys:system/diskchk")
	(delete "sys:system/IntlTrans")
	(delete "sys:system/kill")
	(delete "sys:system/mdiskcopy")
	(delete	"sys:system/mformat")
	(delete "sys:system/txfilter")

	(delete "sys:system/diskchg.info")
	(delete "sys:system/diskchk.info")
	(delete "sys:system/IntlTrans.info")
	(delete "sys:system/kill.info")
	(delete "sys:system/mdiskcopy.info")
	(delete	"sys:system/mformat.info")
	(delete "sys:system/txfilter.info")

	(delete "sys:system/Atari_S9.MFormat.info")
	(delete "sys:system/Atari_D9.MFormat.info")
	(delete "sys:system/MSDOS_D9.MFormat.info")
	(delete "sys:system/MSDOS_S9.MFormat.info")
	(delete "sys:system/MSDOS_QD9.MFormat.info")
	(delete "sys:system/MSDOS_Q18.MFormat.info")

	(delete "sys:system/DiskChg.di0.info")
	(delete "sys:system/DiskChg.di1.info")
	(delete "sys:system/DiskChg.di2.info")
	(delete "sys:system/DiskChg.di3.info")

	(delete "sys:system/Kill.di0.info")
	(delete "sys:system/Kill.di1.info")
	(delete "sys:system/Kill.di2.info")
	(delete "sys:system/Kill.di3.info")

	(delete "sys:system/TxFilter.di0.info")
	(delete "sys:system/TxFilter.di1.info")
	(delete "sys:system/TxFilter.di2.info")
	(delete "sys:system/TxFilter.di3.info")

;sys:crossdos
	(delete "sys:crossdos/diskchg")
	(delete "sys:crossdos/diskchk")
	(delete "sys:crossdos/IntlTrans")
	(delete "sys:crossdos/kill")
	(delete "sys:crossdos/mdiskcopy")
	(delete	"sys:crossdos/mformat")
	(delete "sys:crossdos/txfilter")

	(delete "sys:crossdos/diskchg.info")
	(delete "sys:crossdos/diskchk.info")
	(delete "sys:crossdos/IntlTrans.info")
	(delete "sys:crossdos/kill.info")
	(delete "sys:crossdos/mdiskcopy.info")
	(delete	"sys:crossdos/mformat.info")
	(delete "sys:crossdos/txfilter.info")

	(delete "sys:crossdos/Atari_S9.MFormat.info")
	(delete "sys:crossdos/Atari_D9.MFormat.info")
	(delete "sys:crossdos/MSDOS_D9.MFormat.info")
	(delete "sys:crossdos/MSDOS_S9.MFormat.info")
	(delete "sys:crossdos/MSDOS_QD9.MFormat.info")
	(delete "sys:crossdos/MSDOS_Q18.MFormat.info")

	(delete "sys:crossdos/DiskChg.di0.info")
	(delete "sys:crossdos/DiskChg.di1.info")
	(delete "sys:crossdos/DiskChg.di2.info")
	(delete "sys:crossdos/DiskChg.di3.info")

	(delete "sys:crossdos/Kill.di0.info")
	(delete "sys:crossdos/Kill.di1.info")
	(delete "sys:crossdos/Kill.di2.info")
	(delete "sys:crossdos/Kill.di3.info")

	(delete "sys:crossdos/TxFilter.di0.info")
	(delete "sys:crossdos/TxFilter.di1.info")
	(delete "sys:crossdos/TxFilter.di2.info")
	(delete "sys:crossdos/TxFilter.di3.info")

	(delete "sys:crossdos/.info")
	(delete "sys:crossdos")

	(if (not (exists "sys:crossdos") )
		(delete "sys:crossdos.info")
	)

;CrossDOS V5+
	(delete "l:MSDOSFileSystem")
	(delete "l:CrossDOSFileSystem")
	(delete "c:TDPatch12")
	(delete "c:TDPatch13")
	(delete "sys:system/CDFormat")
	(delete "sys:system/CDFormat.info")
	(delete "sys:system/CDDiskcopy")
	(delete "sys:system/CDDiskcopy.info")
	(delete "c:CDMount")
	(delete "c:CDMount.info")

	(delete "sys:system/CrossDOS")
	(delete "sys:system/CrossDOS.info")
	(delete "sys:system/DiskChg")
	(delete "sys:system/DiskChg.info")
	(delete "devs:dosdrivers/pc0")
	(delete "devs:dosdrivers/pc1")
	(delete "devs:dosdrivers/pc2")
	(delete "devs:dosdrivers/pc3")
	(delete "devs:dosdrivers/pc0.info")
	(delete "devs:dosdrivers/pc1.info")
	(delete "devs:dosdrivers/pc2.info")
	(delete "devs:dosdrivers/pc3.info")
	(set deletedirstr "l:FileSystem_Trans")
	(set deletepat "#?.crossdos")
	(deletedir)
  )
)


(procedure UnInstallCrossDOS
	(if (askbool
			(prompt "About to UNinstall CrossDOS: This function will try to "
					"remove CrossDOS completely from your system. "
					"Are you sure you want to uninstall CrossDOS?"
			)
			(help "This function will try to remove CrossDOS from your system "
				  "to the extent that it is possible to do so safely. Some parts "
				  "of CrossDOS \(such as replacement Mount, Format, and Diskcopy "
				  "commands\) will not be removed, because doing so might "
				  "disrupt the normal functioning of the system. Only in very "
				  "extreme cases should you find it neccessary to revert to the "
				  "to the original \(Commodore-supplied\) commands."
			)
		)
		(	(DeleteOldCD)

			;Comment-out the main mount command from the user-startup
			(if (exists "s:user-startup")
				(	(startup "CrossDOSV5+ - automatically mount all devices"
						(help "This procedure only affects the main mount command placed "
							  "in the user-startup by the CrossDOS installation. After uninstallation, "
							  "the command will still be present in exactly the same form, "
							  "but it will be preceded by a \";\" character, thereby nullifying its "
							  "effects."
						)
						(prompt ("This step will disable the CrossDOS Mount commmand in the "
									"user-startup that normally mounts all devices in the "
									"devs:DOSDrivers directory. (Other mount statements are unaffected). "
									"If you wish to preserve this "
									"functionality, you may choose to skip this part."
								)
						)
						(command ";Mount >nil: devs:DOSDrivers/~(#?.info)" )
					)
				)
			)
			(message "Finished UnInstalling CrossDOS. If you have manually moved "
					 "parts of CrossDOS from their normal location, those files "
					 "are still on your disk."
			)
		)
	)
)

;********************** CrossDOS installation **************************
(procedure ModifyStartup
	(startup "CrossDOSV5+ - automatically mount all devices"
		(prompt "About to install a command in s:user-startup to automatically "
				"start CrossDOS when you boot your system..."
		)
		(help "Normally you want CrossDOS to be automatically invoked every "
			  "time you start your system."
		)
		(command "Mount >nil: devs:DOSDrivers/~(#?.info)")
	)
)

(procedure InstallCrossDOS
	(	(message "About to install CrossDOS...")

		;Delete an old installation of CrossDOS
		(if (exists "l:msdosfilesystem")
			(set old 1)	;if old
			(set old 0)	;if not old
		)
		(if old
			(	(message "About to delete a previous CrossDOS installation...")
				(DeleteOldCD)
			)
			(	(delete "sys:system/CDFormat")
				(delete "sys:system/CDFormat.info")
				(delete "sys:system/CDDiskcopy")
				(delete "sys:system/CDDiskcopy.info")
				(delete "c:CDMount")
				(delete "c:CDMount.info")
			)
		)

		;Replace old MountMF command
		(if (exists "sys:mountmf")
			(copyfiles (source ":c/MountMF") (dest "sys:") )
		)
		(if (exists "sys:crossdos/mountmf")
			(copyfiles (source ":c/MountMF") (dest "sys:CrossDOS") )
		)
		(if (exists "c:mountmf")
			(copyfiles (source ":c/MountMF") (dest "c:") )
		)

		;Create certain directories if they don't exist
		(makedir "l:FileSystem_Trans" (infos) )
		(makedir "sys:system" (infos) )

		;The purpose of this is to create an icon for the devs: directory if needed.
		(makedir (getassign "devs") (infos) )
		(makedir "devs:DOSDrivers" (infos) )

		;Install mfm.device
		(set mysource "devs/mfm.device")
		(set mydest "devs:")
		(set mynewname "mfm.device")
		(mycopylib)

		;Install Mount command
		(set mysource ":c/Mount")
		(set mydest "c:")
		(set mynewname "Mount")
		(mycopylib)

		;Install CrossDOSFileSystem
		(set mysource ":l/CrossDOSFileSystem")
		(set mydest "l:")
		(set mynewname "CrossDOSFileSystem")
		(mycopylib)

		;Copy devs:dosdrivers
		(foreach ":" "PC?"
			(	(copyfiles (source (":%s" @each-name) ) (dest "devs:DOSDrivers") )
				(if (not (exists ("devs:DOSDrivers/%s.info" @each-name) (noreq)) )
					(	(copyfiles (source (":%s.info" @each-name) ) (dest "devs:DOSDrivers") )
						(tooltype (dest ("devs:DOSDrivers/%s" @each-name) )
							(noposition)
							(setdefaulttool "c:Mount")
							;Delete the "filesystem=" entry
							;Case-sensitive!!! (yet another installer bug)
							(settooltype "FILESYSTEM")
						)
					)
				)
			)
		)

		;Install Format
		(set mysource ":system/Format")
		(set mydest "sys:system")
		(set mynewname "Format")
		(mycopylib)

		;Install DiskCopy
		(set mysource ":system/DiskCopy" )
		(set mydest "sys:system")
		(set mynewname "DiskCopy")
		(mycopylib)

		;Install the CrossDOS utility
		(copyfiles (source ":system/CrossDOS") (dest "sys:system") (newname "CrossDOS") )
		(copyfiles (source ":system/CrossDOS.info") (dest "sys:system") )
		(tooltype (dest "sys:system/CrossDOS") (noposition) )

		;Copy FileSystem_Trans
		(copyfiles (source ":l/FileSystem_Trans") (dest "l:FileSystem_Trans") (all) (optional nofail))

		;Install QuickPrep
		(set mysource ":system/QuickPrep")
		(set mydest "sys:system")
		(set mynewname "QuickPrep")
		(mycopylib)

		;Last copy operation -- copy DiskChg to sys:system
		(copyfiles (source ":system/DiskChg") (dest "sys:system") (infos) )	;required!!!
		(tooltype (dest "sys:system/DiskChg") (noposition) )

		;Enter mount command in s:user-startup. Previously we did this whenever
		;installing on a new CD user's system. Now we don't care whether he has
		;an old CD, but we do avoid inserting it into the u-s if the user is
		;running Workbench 2.1 or higher, unless we are in expert mode and
		;the user overrides our wisdom.

		(if (and (= @user-level 2) wb21)
			(	(if (askbool
						(prompt "If you select \"Install\", a command will be "
								"inserted into your s:user-startup file to mount "
								"all devices in the DOSDrivers drawer. Since you are "
								"running Workbench 2.1 or above, however, this command should "
								"already be there, unless you have extensively customized your "
								"system. Select \"Skip\" to avoid adding a "
								"redundant command \(which won't hurt, but will slow down "
								"booting\)."
						)
						(choices "Install" "Skip")
						(help "In most cases you should select \"Skip\".")
					)
					(	(user 0)
						(ModifyStartup)
						(user 2)
					)
				)
			)
			(	(if (not wb21) (ModifyStartup) ) )
		)

		;Mount the stuff now so the user can use it immediately
		;This 'run' is safe because c: is an explicit path
		(run "c:Mount >nil: devs:DOSDrivers/~(#?.info)")

		(message "CrossDOS is now installed.")
	)
)


;********************** Configure Hard Disk **************************
(procedure ConfigureHardDisk
	(	(if (= 0 (exists "l:CrossDOSFileSystem") )
			(	(message "CrossDOS 7 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"
						 "Configure a Disk File"
				)
				(prompt "Please select an option. * Hit HELP for details!!! *")
				(help "   \"Configure on an Amiga Partition\"\n"
					  "if you want to configure one of your Amiga partitions "
					  "for use as an MS-DOS hard drive image.\n"
					  "   \"Configure a 100% MS-DOS Hard Disk\".\n"
					  "if you want to configure an entire PHYSICAL hard drive "
					  "(such as a SyQuest or Bernoulli etc).\n"
					  "   \"Configure a Disk File\"\n"
					  "if you want to use a file as an MS-DOS disk image (such "
					  "as JLink, MakeAB, PC-Task and AtOnce disk files)."
				)
			)
		)

		;Run the ConfigDev program
		(if (= opt 0) (set drivetype "PARTITION") )
		(if (= opt 1) (set drivetype "DISK") )
		(if (= opt 2)
			(	(set drivetype "FILE") 
					;Install filedisk.device
				(set mysource "devs/filedisk.device")
				(set mydest "devs:")
				(set mynewname "filedisk.device")
				(mycopylib)
			)
		)

		(run ("%sConfigDisk %s" @execute-dir drivetype))
	)
)

;***************************************************************************
; Get the version number from a file
(procedure getVersion_File
	(set version (getversion verfile))
	(if (= 0 version)
		(set version defver)
		(	(set ver (/ version 65536))
			(set version ("%ld" (+ (* 100 ver) (BITAND version $FFFF))) )
		)
	)
)

;********************** Product Registration **************************
(procedure Register
	(run ("%sRegistration %sRegistrationInfo" @execute-dir @execute-dir) )
)

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

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

;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 disklabel (expandpath ":") )
(set @execute-dir disklabel)

;get Kickstart version number
(set ksver (/ (getversion ) 65536 ) )

;set ks20 variable to 1 if running Kickstart 2.04 or above
(set ks20 (> ksver 36) )

;get Workbench version number
(set wbver (/  (getversion "libs:version.library" ) 65536 ) )

;set wb21 variable to 1 if running Workbench 2.1 or above
(set wb21 (> wbver 37) )

; Check whether 2.04 or above if not quit 
(if (NOT ks20 )
	(	(message "CrossDOS only works on machines with Kickstarts above 2.04. "
				"Exiting.."
		)
		(exit (quiet) )
	)
)

;Check whether the disk label is "CrossDOS:"; if not, try to relabel the
;disk.
;The rename (relabel in this case) command doesn't seem to work...another
;installer bug?

;(if (not (= disklabel "CrossDOS:") )
;	(	(rename (disklabel) ("CrossDOS") (disk) ) )
;)

(run (":c/serfile :READ.ME >ENV:CIN"))
(set verfile ":Read.ME")
(getVersion_File)	; return in 'version'
(message ("The CrossDOS product disk is at:\n%s.%s\n" (substr version 0 1) (substr version 1))
			"Your Customer Identification Number (CIN) is:\n\n" (getenv "CIN") "\n"
			"(Please record the CIN somewhere\nto use if you need technical support)"
)

(if (= (exists "L:CrossDOSFileSystem" (noreq)) 1)
	(if (earlier "L:CrossDOSFileSystem" "l/CrossDOSFileSystem")
		(if	(askbool
				(prompt "We have detected that an earlier release of CrossDOS is installed. "
					"We suggest that all you do is install the new version of CrossDOS "
					"and REBOOT your Amiga to have the new release take effect "
					"for all your current CrossDOS configured devices."
				)
				(help "If your system is setup correctly "
					"under the previous version of CrossDOS, "
					"(ie. all your CrossDOS devices working properly) "
					"then you must install the new version of CrossDOS "
					"and REBOOT to activate the new features.\n\n"
					"There is no need to reconfigure any of the CrossDOS devices.\n\n"
					"If you need to configure new CrossDOS devices, run the Install "
					"program again after installing CrossDOS and a REBOOT of your Amiga."
				)
				(choices "Install CrossDOS" "Cancel Install")
			)
			((InstallCrossDOS) (exit))
			(exit (quiet))
		)
	)
)

(while 1
	(	;Attempt to determine what the default menu options should be
		(set defmenu 0)
		(if (not (exists "L:CrossDOSFileSystem") )
			(set defmenu (bitor defmenu %00000101) )
		)

		;Ask the user what he wants to install or do.

		(set mainopt
			(askoptions
				(prompt "Please select all functions that you wish to perform")
				(choices "Install CrossDOS"
						 "Configure Hard Disk"
						 "Fill out Product Registration"
						 "Uninstall CrossDOS"
				)
				(default defmenu)
				(help "The \"Install CrossDOS\" option installs the "
					  "software. The \"Configure MS-DOS Hard Disk\" option will run ConfigDisk, "
					  "which allows you to set up a drive or partition for "
					  "CrossDOS. The \"Fill out Product Registration\" option will "
					  "prompt you for your name and address, and print it to PRT: (the "
					  "default printer). Using the registration software eliminates "
					  "the difficulties we sometimes have reading hand-written registration cards. "
					  "Use the \"Uninstall CrossDOS\" option to remove CrossDOS."
				)
			)
		)

		(if (= mainopt 0)
			(	(message "You didn\'t select anything. Exiting...")
				(exit (quiet) )
			)
		)

		(if	(NOT (and (in mainopt 3) (BITAND mainopt %00000111) ) )
			(	(if (in mainopt 0) (InstallCrossDOS) )
				(if (in mainopt 1) (ConfigureHardDisk) )
				(if (in mainopt 2) (Register) )
				(if (in mainopt 3) (UninstallCrossDOS) )
				(exit (quiet) )
			)
			(	(message "The UNinstall option cannot be selected in conjunction "
						 "with any other options. Perhaps you accidentally "
						 "selected it."
				)
			)
		)
	)
)
