; $VER: MuInstall 47.8 (19.7.2025)
; Script to install the MuLib version 47.8
; © 2000-2025 THOR - Software
;
; Based on MMULib_Install 1.1 by Simon N Goodwin
; (02.06.2000)
;
; The installer software is copyrighted © 1993 Amiga Inc.
; Amiga is a registered trademark of Amiga
;
; Installer 43.8 is copyrighted © Hyperion
; Installer binaries supplied with kind permission of Hyperion
;

;==Procedures==================================================================
;
; Some useful procedures follow...
;

;*************************************************************************
;** TERMINATE								**
;** called upon termination, removes assigns etc,etc,			**
;*************************************************************************

(procedure TERMINATE 
	(makeassign "..MU..")
)

;*************************************************************************
;** CAT_OLD								**
;** concatenate #name to #oldfiles if #target #name exists		**
;*************************************************************************

(procedure P_CAT_OLD #name (

	(if (exists (tackon #target #name))
		(set #oldfiles	(cat #oldfiles #name "\n"))
	)

))

;*************************************************************************
;** FIND_CPU								**
;** find the CPU type, set #cpu accordingly				**
;*************************************************************************

(procedure P_FIND_CPU (

	(set #cpu "68000")

	(if (= (run ("..MU..:Install/TestCPU 68020") (safe)) 0)
		(set #cpu "68020")
	)
	(if (= (run ("..MU..:Install/TestCPU 68030") (safe)) 0)
		(set #cpu "68030")
	)
	(if (= (run ("..MU..:Install/TestCPU 68040") (safe)) 0)
		(set #cpu "68040")
	)
	(if (= (run ("..MU..:Install/TestCPU 68060") (safe)) 0)
		(set #cpu "68060")
	)
))

;*************************************************************************
;** COPY_OLD								**
;** copy file #name from #target to #stash_old				**
;*************************************************************************

(procedure P_COPY_OLD #name (

	(if (exists (tackon #target #name))
		(
			(if (exists (tackon #stash_old #name))
				(delete (tackon #stash_old #name))
			)
			(copyfiles	(source (tackon #target #name))
					(dest (tackon #stash_old #name))
			)
		)
	)
))


;*************************************************************************
;** P_COPYCPU								**
;** copy the cpu library #name to #proc-dest				**
;*************************************************************************

(procedure P_COPYCPU #name (

	(copyfiles	(prompt	(#cpu-copy #name))
			(help #copy-proclib-help)
			(source ("..MU..:Libs/%s.library" #name))
			(dest #proc-dest)
	)
))

;*************************************************************************
;** P_PATCH_REPLACE							**
;** replace the command #rplc by #dest in the startup-sequence		**
;** found in #startup-path and #user-path				**
;*************************************************************************

(procedure P_PATCH_REPLACE (

	(
		(if (exists "t:__tempfile__")
			(run ("C:Delete t:__tempfile__ QUIET") (safe))
		)

		(run ("..MU..:Install/SED FROM \"%s\" TO T:__tempfile__ MATCH \"({}|{#[~;]+[ \\t]}#[~; \\t][/:]|{}#[~; \\t][/:]|{#[~;]+[ \\t]})%s{|[ \\t;]#?}\" CHANGE \"{1}%s{2}\""
			#startup-path #rplc #dest))

		(if (exists "t:__tempfile__")
			(run ("C:Copy t:__tempfile__ TO \"%s\"" #startup-path))
			(run ("C:Delete t:__tempfile__") (safe))
		)

		(if (<> #user-path #startup-path)
			(if (exists #user-path)
				(
					(run ("..MU..:Install/SED FROM \"%s\" TO T:__tempfile__ MATCH \"({}|{#[~;]+[ \\t]}#[~; \\t][/:]|{}#[~; \\t][/:]|{#[~;]+[ \\t]})%s{|[ \\t;]#?}\" CHANGE \"{1}%s{2}\""
						#user-path #rplc #dest))

					(if (exists "t:__tempfile__")
						(run ("C:Copy t:__tempfile__ TO \"%s\"" #user-path))
						(run ("C:Delete t:__tempfile__") (safe))
					)
				)
			)
		)
	)
))

;*************************************************************************
;** P_PATCH_REMOVE							**
;** remove the command #rplc by adding a semicolon in the 		**
;** startup-sequence found in #startup-path and #user-path		**
;*************************************************************************

(procedure P_PATCH_REMOVE (

	(
		(if (exists "t:__tempfile__")
			(run ("C:Delete t:__tempfile__") (safe))
		)

		(run ("..MU..:Install/SED FROM \"%s\" TO T:__tempfile__ MATCH \"{(|#[~;]+[ \\t]#[~; \\t][/:]|#[~; \\t][/:]|#[~;]+[ \\t])%s(|[ \\t;]#?)}\" CHANGE \";{1}\""
			#startup-path #rplc))

		(if (exists "t:__tempfile__")
			(run ("C:Copy t:__tempfile__ TO \"%s\"" #startup-path))
			(run ("C:Delete t:__tempfile__") (safe))
		)

		(if (<> #user-path #startup-path)
			(if (exists #user-path)
				(

					(run ("..MU..:Install/SED FROM \"%s\" TO T:__tempfile__ MATCH \"{(|#[~;]+[ \\t]#[~; \\t][/:]|#[~; \\t][/:]|#[~;]+[ \\t])%s(|[ \\t;]#?)}\" CHANGE \";{1}\""
						#user-path #rplc))

					(if (exists "t:__tempfile__")
						(run ("C:Copy t:__tempfile__ TO \"%s\"" #user-path))
						(run ("C:Delete t:__tempfile__") (safe))
					)
				)
			)
		)
	)
))

;==Strings=====================================================================
;
; In the following, we initialize the english strings we might need
; in a localized version, only the following section must be translated
;

(set #default_lang 1)

; Generic answers
(set #yes "Yes")
(set #no  "No")

; A 68000 or 68010 has been detected, bail out
(set #senseless
(cat "Installation of the MuLib package on a 68000/68010 system does "
     "not make much sense. Abort installation?\n"
))

; Help message for the above
(set #senseless-help
(cat "Even though the mmu.library will run on a 68000 or 68010 based "
     "systems, installation on these systems does not make much sense "
     "because they do not provide a MMU that is handled and programmed "
     "by this library. Therefore, almost all functions of the mmu.library "
     "will not be available for programs and most of the MuTools will "
     "not work on your system.\n"
)) 

;User selected abortion of the installation.
(set #canceled
(cat "You aborted the installation process."
))

; Destination disk
(set #which-disk
(cat "On which disk should the MuLib be installed?"
))

; Destination disk help
(set #which-disk-help
(cat "\nThis section lets you choose in which disk the "
     "MuLib files should be installed. These should normally go in the same "
     "location as the Workbench files, i.e. the boot partition or -disk.\n\n"
     @askdir-help
))

; Important assigns are missing
(set #no-assign
(cat "\nThe destination has to be a system disk. The drawers S,DEVS,LIBS "
     "and C must exist there.\n\n"
))

; Directory where old files should be kept
(set #old-name
(cat "OLD"
))

; Move old files?
(set #move-old
(cat "\n\nDo you want to backup the following files "
     "to the \"%s\" drawer?\n\n%s" 
))

(set #move-old-help
(cat "\nInstalling the MuLib will replace or update some current Workbench "
     "and system files. "
     "In order to preserve any changes you have previously made to these "
     "files, the installation process will copy them to the \"%s\" drawer "
     "where they can be found later on."
))

(set #move-old-copy
(cat "Copy"
))

(set #move-old-skip
(cat "Skip"
))

; Ask for the source of the installation
(set #ask-mu
(cat "Please locate the MuLib source directory:"
))

(set #ask-mu-help
(cat "Please specify here the directory where you've unpacked the "
     "archive to. The right directory contains \"Install\", \"Fixes\" and \"MuTools\". "
     "The default I selected for you should work in (almost) all cases.\n\n"
     @askdir-help
))

(set #where-multiview
(cat "Please pick your prefered AmigaGuide® viewer:"
))

(set #where-multiview-help
(cat "The installer procedure needs to know the path to MultiView or any "
     "other AmigaGuide® browser to show an introductory text to the MuLib "
     "and it's installation. Please use the requester to select it.\n"
     @askfile-help
))

; Ask user for P5 illnesses
(set #ask-p5
(cat "Do you use a Phase 5 accelerator board?"
))

; Ask for ACA-Boards
(set #ask-aca
(cat "Do you have an older ACA (Individual Computer) board?"
))

; Ask for GVP-Boards
(set #ask-gvp
(cat "Do you have a GVP 040 or 060 accelerator board?"
))

(set #ask-p5-help
(cat "Unfortunately, Phase 5 choose not to make their boards compatible "
     "to the CBM AutoConfig (tm) standard. This means that some additional "
     "steps have to be performed to install the mmu.library on these boards.\n"
     "Typically, this installation script is able to figure this out itself, "
     "but it might require some help of you to ensure that everything is correct."
))

(set #ask-aca-help
(cat "Some older ACA boards are not fully compatible to the CBM AutoConfig (tm) "
     "standard. This means that some additional steps have to be performed to "
     "install the mmu.library on these boards.\n"
     "Typically, this installation script is able to figure this out itself, "
     "but it might require some help of you to ensure that everything is correct."
))

(set #ask-gvp-help
(cat "Some versions of 040 and 060 based GVP boards cannot run burst "
     "accesses safely to Zorro-II memory. This means that caching has to "
     "be turned off for the Zorro-II area.\n"
     "Typically, this installation script is able to figure this out itself, "
     "but it might require some help of you to ensure that everything is correct."
))

; Installation of the mmu.library
(set #copy-mmulib
(cat "The mmu.library will be copied to the LIBS: drawer."
))

(set #ask-copy-mmulib
(cat "Install the mmu.library?"
))

(set #ask-mmulib-dest
(cat "Please select the destination for the "
     "mmu.library."
))

(set #copy-mmulib-help
(cat "The mmu.library is used by other parts of the MuLib package "
     "to extend your Amiga's operating system, so that it uses " 
     "memory management to make it more robust and efficient.\n\n"
     "This library is the essential component needed by all "
     "other parts this MuLib package. The other parts are "
     "optional and can be selected at will, once the library is"
     "available.\n"
     "It will occupy approximately 40K of space on your "
     "SYS: partition, and it should go to a location where "
     "the system will be able to find libraries. The recommendation "
     "is to copy it to the LIBS: directory."
))

; Update of SetPatch strings
(set #ask-patch-setpatch
(cat "Do you want to update SetPatch to determinate and load the correct "
     "processor libraries?"
))

(set #ask-patch-setpatch-help
(cat "The Commodore version of SetPatch will load 68040.library if "
     "it detects a 68040 or later chip; it will not try to load a "
     "processor driver for the 68020 or 68030, and it does not distinguish "
     "between the 68040 and the 68060 processor. Therefore, a 68040 dummy "
     "library is required for 68060 based boards.\n"
     "The updated SetPatch will, instead, load the 680x0.library which in "
     "turn will be able to initialize the correct processor driver.\n"
     "This allows to setup a system in a way that will work regardless of the "
     "available processor, without the need to install or remove processor "
     "libraries."
))

(set #ask-lib0x0-dest
(cat "Where to install the 680x0.library?"
))

(set #ask-lib0x0-dest-help
(cat "The new SetPatch release requires the 680x0.library to detect "
     "the installed processor and to boot the correct processor "
     "driver.\n"
     "The library should go into the LIBS: assign since it is required "
     "very early on bootstrap - namely by SetPatch."
))

(set #ask-setpatch
(cat "Please locate the SetPatch command:"
))

(set #setpatch-is-patched
(cat "The version of SetPatch you are using has been updated already."
))

(set #setpatch-unknown
(cat "The version of SetPatch you are using is unknown to the installation "
     "script and cannot be updated, sorry. Please try to download the 43.6 "
     "edition, or buy Os 3.5 to get the 44.xx editions."
))

(set #copy-new-setpatch
(cat "Installing the new release of SetPatch."
))

(set #copy-lib0x0
(cat "Installing the 680x0.library."
))

; Installation of the mmu.library based processor libraries

(set #which-proclibs
(cat "Replace which processor libraries by MuLib specific libs:"
))

(set #copy-proclib-help
(cat "The MuLib archive comes with new mmu.library based processor "
     "libraries that make use of the functions of the mmu.library "
     "to construct the MMU setup. This has the advantage that these "
     "libraries are typically shorter and use less memory than the "
     "generic libraries. The installation of these libraries is, "
     "however, optional. The mmu.library will work happily on top of "
     "any processor library - this setup might just waste some memory, "
     "but might be easier to setup for exotic or not so well behaived "
     "boards.\n"
     "These libraries should go to the LIBS: directory where they can be "
     "found by either SetPatch or the 680x0.library."
))

(set #ask-proclib
(cat "Replace the processor library by a MuLib specific lib?"
))

(set #ask-proclib-dest
(cat "Where to install the processor driver libraries:"
))

(set #cpu-copy
(cat "Installing the %s.library..."
))

(set #build-config
(cat "Build now the MMU-Configuration file?"
))

(set #build-config-help
(cat "If you installed the MuLib specific processor libraries, "
     "you need to generate a configuration file for the mmu.library "
     "which is required to build the proper MMU setup. This setup file "
     "will be generated automatically by the installation procedure. "
))

(set #overwrite-config
(cat "Overwrite the existing MMU-Configuration file?"
))

(set #ask-config-dest
(cat "Where to install the MMU-Configuration file:"
))

(set #ask-config-help
(cat #build-config-help
     "It should go into the ENVARC: directory. The only alternative "
     "location would be the DEVS: assign.\n"
     "Note that the mmu.library is run very early in your setup, namely "
     "by SetPatch. This requires that the directory you pick now is really "
     "part of the ENVARC: or DEVS: assigns at this stage of the boot up "
     "process."
))

(set #config-copy
(cat "Now installing the MMU-Configuration..."
))

(set #where-startup
(cat "Please locate the active Startup-Sequence:"
))

(set #where-startup-help
(cat "Please click on the file that acts as Startup-Sequence in your "
     "system. I need the true Startup-Sequence, not the User-Startup.\n"
     "The choice presented here by the installer script should be fine "
     "already.\n"
     @askfile-help
))

(set #where-user
(cat "Please locate the active User-Startup:"
))

(set #where-user-help
(cat "Please click on the file that acts as User-Startup in your "
     "system. I need the real User-Startup, not the Startup-Sequence.\n"
     "The choice presented here by the installer script should be fine "
     "already.\n"
     "In case you do not have a User-Startup, please select the "
     "Startup-Sequence again.\n"
     @askfile-help
))

(set #install-bppcfix
(cat "Install and run BPPCFix on startup?"
))

(set #install-bppcfix-help
(cat "BPPCFix is a tiny program provided by Frank Wille which removes "
     "the resident ppc and 68040/68060 libraries from some P5 based boards. "
     "You should run it in case the ppc.library is ROM resident in your "
     "processor board, or if the 68040/68060.library is resident and should "
     "be replaced the MuLib based processor library. "
     "Otherwise, especially if you do not own a P5 board, you may simply skip "
     "this step."
))


(set #set-bppcline
(cat "Please edit the BPPCFix command line, check \"HELP...\" for all options:"
))

(set #set-bppcline-help
(cat "The command line picked by the installer will be good enough, typically, "
     "but experts might want to fine-tune it.\n"
     "The following command line switches are known:\n"
     "\nInstall:        Install BPPCFix if it isn't installed already."
     "\nRemove:         Remove BPPCFix (not useful here)."
     "\n040:            Remove resident processor libraries as well."
     "\nReboot:         Reboot the system if BPPCFix is not yet resident."
     "This is not necessary if SetPatch invokes a reboot anyhow."
))

(set #where-bppcfix
(cat "Where to install BPPCFix:"
))

(set #where-bppcfix-help
(cat "Please pick a directory where BPPCFix should be placed. "
     "Note that this directory must be available on startup. By default, "
     "this should be the C: assign.\n"
     @askdir-help
))

(set #fix-bvision
(cat "Adapt the BVisionPPC monitor driver?"
))

(set #fix-cvision
(cat "Adapt the CVisionPPC or CVPPC monitor driver?"
))

(set #fix-bcvision-help
(cat "The BVisionPPC and CVisionPPC/CVPPC CyberGraphics monitor drivers depend unfortunately "
     "on some undocumented internals of the P5 68040 or 68060.libraries. "
     "Therefore, it will fail if used together with the MuLib based "
     "processor libraries unless it is patched to use the p5emu.library "
     "which is build by the P5Init command in the MMU-Configuration.\n"
     "In case you do not own a BVisionPPC or CVisionPPC/CVPPC graphics card, you may simply "
     "ignore this question and proceed."
))

(set #where-bvision
(cat "Where is the BVisionPPC monitor driver:"
))

(set #where-cvision
(cat "Where is the CVisionPPC or CVPPC monitor driver:"
))

(set #where-bvision-help
(cat "Please locate here the BVisionPPC monitor driver to make it MuLib "
     "conformal.\n"
     #fix-bcvision-help
     "\n"
     @askfile-file
))

(set #where-cvision-help
(cat "Please locate here the CVisionPPC or CVPPC monitor driver to make it MuLib "
     "conformal.\n"
     #fix-bcvision-help
     "\n"
     @askfile-file
))

(set #pick-mutools
(cat "Please pick the MuTools you want to install:"
))

(set #pick-mutools-help
(cat "The MuTools are a collection of mmu.library based tools that "
     "replace other non-MuLib-conformal MMU hacks. They are written in "
     "a way such that all of them co-operate nicely.\n"
     "The following tools are available:\n"
     "\nMuFastRom:       Speedup the system by mirroring the kickstart ROM in RAM."
     "\nMuFastZero:      Speedup the system by relocating the first RAM page from"
     "chip RAM to fast RAM."
     "\nMuMove4K:        Startup tool to allow MuFastZero to mirror the system base "
     "library in fast RAM as well if required."
     "\nMuScan:          Prints the MMU setup."
     "\nMuForce:         The version 40 of Mike Sinz' \"Enforcer\" debugging tool."
     "\nMuGuardianAngel: MuForce \"add-on\" tool to detect illegal accesses. This "
     "is a MungWall/GuardianAngel super-set."
     "\nMuOmniSCSIPatch: Makes the Guru-ROM (omniscsi.device) MMU-aware for "
     "virtual memory applications."
     "\nMuSetCacheMode:  MMU table fine tuning tool for experts."
     "\nMuFastChip:      Speedup chip memory accesses for third-party processor "
     "libraries."
     "\nMuEVD:           MuLib driven ShapeShifter video driver."
     "\nMuLockLib:       Load and lock the MuLib in memory on startup."
     "\nMuLink:          MuLib driven code protection post processing tool for "
     "developers."
     "\nMuProtectModules:Write-protects disk-based resident modules loaded by "
     "LoadModule of the same author."
     "\n\n"
     "More about the tools can be found in the specific AmigaGuide files in the "
     "MuTools directory of the MuLib distribution."
))

(set #where-mutools
(cat "Where should I install the MuTools?"
))

(set #where-mutools-help
(cat "Please pick a directory into which the MuTools will be installed. "
     "No sub-directory will be created.\n"
     @askdir-help
))

(set #where-mutool-guide
(cat "Where should I install the MuTools documentation?"
))

(set #where-mutool-guide-help
(cat "Please pick a directory into which the MuTools documentation, i.e. the "
     "manuals in AmigaGuide format, should be installed. No sub-directory "
     "will be created.\n"
     "Typically, you want to keep the documentation along with the tools.\n"
     @askdir-help
))


(set #startup-mufastrom
(cat "Shall I run MuFastROM on startup?"
))

(set #startup-mufastrom-help
(cat "MuFastROM is a MuLib based tool that remaps the kickstart ROM "
     "into RAM, hence boosting the system at the cost of 512K of fast "
     "RAM space. It should be run in the startup-sequence."
))

(set #startup-mufastrom-go
(cat "The following command is added to the user-startup:\n"
))

(set #startup-mufastzero
(cat "Shall I run MuFastZero on startup?"
))

(set #startup-mufastzero-help
(cat "MuFastZero is a MuLib based zero page remapper. It will relocate "
     "the first memory page from chip memory to fast memory and will "
     "therefore make interrupt processing a little bit faster. It should "
     "be run in the startup-sequence."
))

(set #remap-exec
(cat "Shall MuFastZero remap SysBase?"
))

(set #remap-exec-help
(cat "In case your system does not come with auto-configurable fast RAM, "
     "the system main library will end up in slow chip memory. This option "
     "will tell MuFastZero to relocate SysBase as well. You do NOT need "
     "this feature if SysBase is in fast memory anyhow.\n"
     "This option requires a preparation phase by means of MuMove4K. MuMove4K "
     "installation will be turned on automatically if you select this option."
))

(set #remap-ssp
(cat "Shall MuFastZero remap the supervisor stack?"
))

(set #remap-ssp-help
(cat "The supervisor stack holds temporary information on interrupt "
     "processing. In case your system does not come with auto-configurable "
     "fast RAM, the supervisor stack will end up in slower chip memory. "
     "This option will tell MuFastZero to relocate the stack as well. "
     "You do NOT need this feature if the supervisor stack is in fast "
     "memory anyhow."
))

(set #startup-mufastzero-go
(cat "The following command is added to the user-startup:\n"
))


(set #startup-mumove4k
(cat "Run MuMove4K on startup?"
))

(set #startup-mumove4k-help
(cat "The MuMove4K command is required for the MuFastZero \"FastExec\" "
     "option, i.e. if you picked "Remap SysBase" above in the MuFastZero "
     "installation. It relocates the start of the chip memory by 32K, hence "
     "leaving enough room for the exec.library to be remapped from chip "
     "memory to fast memory. In case you run PrepareEmul for the ShapeShifter "
     "preparation, it must be replaced by MuMove4K as well."
))

(set #prepareemul
(cat "Include the PrepareEmul ShapeShifter setup?"
))

(set #prepareemul-help
(cat "In case you run the ShapeShifter and hence the PrepareEmul program, "
     "please replace this program by MuMove4K and the apropriate options "
     "since MuMove4K is incompatible to PrepareEmul. The apropriate command "
     "line options will be selected for you."
))

(set #edit-mumove4k
(cat "Please edit the MuMove4K command line, check \"HELP...\" for all options:"
))

(set #edit-mumove4k-help
(cat "The following MuMove4K options are available:\n"
     "\nA1200: Compatibility switch for some P5 boards. If it won't boot without "
     "this switch, include it."
     "\nNOREBOOT: Do not reboot the system if MuMove4k is not installed."
     "\nIGNOREVERIFY: Do not wait until all drives completed verification. Required "
     "in case MuMove4K aborts booting frequently due to invalidate partitions."
     "\nPREPAREEMUL: Includes the ShapeShifter PrepareEmul feature."
     "\nREVERSE: Allocate the memory from \"top down\" instead of \"bottom up\"."
     "\nLOWPRI: Lowers the priority of the resident segments.\n"
))

(set #ask-cybfix
(cat "Fix an illegal access in some P5 devices?"
))

(set #ask-cybfix-help
(cat "Some P5 device drivers like the cybscsi.device and the "
     "z3scsi.device, and maybe some more, run an illegal memory access "
     "that will trigger a MuGuardianAngel hit (for good reason!). To avoid "
     "this hit, you are encouraged to install the \"FixCybAccess\" or the "
     "\"FixP5Scsi\" program on startup if you own one of these devices. "
     "It will work around this hit."
))

(set #startup-cybfix-go
(cat "The following command is added to the user-startup:\n"
))

(set #where-cybfix
(cat "Where to install FixCybAccess:"
))

(set #where-fixp5scsi
(cat "Where to install FixP5Scsi:"
))

(set #where-iprefs
(cat "Please locate the IPrefs command:"
))

(set #where-iprefs-help
(cat "The IPrefs release 40.7 (Workbench 3.1) is too low on stack and "
     "may cause crashes or hangs, especially if you use datatypes to provide "
     "a background picture for the workbench. At least, however, it will cause "
     "a lot of (justified!) MuGuardianAngel warnings. This script "
     "checks the IPrefs version and offers a patch for the 40.7 release if it "
     "finds this release active. Later releases of IPrefs, i.e. version 44.xx, "
     "do not have this problem and will be safely left alone by the installer."
))

(set #ask-iprefs-patch
(cat "Increase the IPrefs 40.7 stack size to avoid crashes?"
))

(set #where-narrator
(cat "Please locate the narrator.device:"
))

(set #where-narrator-help
(cat "The last narrator device 37.7 (Workbench 2.04) is too low on stack and "
     "may cause some (justified!) MuGuardianAngel warnings. This script "
     "checks the narrator.device version and offers a patch for the 37.7 release if it "
     "finds this release active. If you don't own the narrator.device, you may ignore "
     "this bug fix silently."
))

(set #ask-narrator-patch
(cat "Increase the narrator.device 37.7 stack size?"
))

(set #where-mfm
(cat "Please locate the mfm.device:"
))

(set #where-mfm-help
(cat "The mfm.device 40.9 (Workbench 3.1) is too low on stack and "
     "may cause some (justified!) MuGuardianAngel warnings. This script "
     "checks the mfm.device version and offers a patch for the 40.9 release if it "
     "finds this release active. If you don't own the mfm.device, or own a different "
     "release, you may ignore this bug fix silently. Unfortunately, I can't offer a "
     "patch other releases."
))

(set #ask-mfm-patch
(cat "Increase the mfm.device 40.9 stack size?"
))

(set #fix-omniscsi
(cat "Shall I make the OmniSCSI MMU aware?"
))

(set #fix-omniscsi-help
(cat "The omniscsi.device, part of the Guru-ROM firmware, is "
     "not MMU aware by default which could cause problems in case "
     "virtual memory applications are run. The MuOmniSCSIPatch "
     "program will fix this potential problem if run as part of "
     "the startup process."
))

(set #startup-omniscsi-go
(cat "The following command is added to the user-startup:\n"
))

(set #ask-mufastchip
(cat "Run MuFastChip on startup?"
))

(set #ask-mufastchip-help
(cat "MuFastChip will speed up the chip RAM access on some "
     "machines by selecting a faster caching mode. However, the "
     "MMU setup will be optimal already in case you use one of the "
     "MuLib based processor libraries or the P5 libraries. Futhermore, "
     "MuFastChip will do nothing on 68020 or 68030 based machines.\n"
     "In these cases, installation is not required."
))

(set #startup-mufastchip-go
(cat "The following command is added to the user-startup:\n"
))

(set #ask-muprotect
(cat "Run MuProtectModules on startup?"
))

(set #ask-muprotect-help
(cat "MuProtectModules write-protects disk-based resident modules "
     "that have been made reset-resident by means of the \"LoadModule\" "
     "program of the same author. These modules are typically Os 3.5 "
     "or Os 3.9 libraries or devices that replace otherwise ROM resident "
     "modules.\n"
     "It does not make sense to install MuProtectModules if "
     "LoadModule is not used."
))

(set #startup-muprotect-go
(cat "The following command is added to the user-startup:\n"
))

(set #ask-mulocklib
(cat "Run MuLockLib on startup?"
))

(set #ask-mulocklib-help
(cat "The MuLockLib program is a tiny tool that loads and locks the "
     "mmu.library in memory such that it cannot be flushed. Since the "
     "MuLib based processor libraries load and lock the mmu.library anyhow, "
     "it makes little sense to run MuLockLib with these libraries installed. "
     "If you use, however, some third party libraries, this tool might help "
     "to decrease the loading time of the MuTools by providing the library "
     "right away."
))

(set #startup-mulocklib-go
(cat "The following command is added to the user-startup:\n"
))

(set #ask-muevd
(cat "Install MuEVD into the ShSh video drivers?"
))

(set #ask-muevd-help
(cat "MuEVD is now installed in the selected MuTools directory, "
     "but it is a good idea to make it available to the ShapeShifter "
     "as well by installing it into its video drivers directory."
))

(set #where-muevd
(cat "Please locate the ShapeShifter video driver directory:"
))

(set #ask-muevdset
(cat "Install the MuEVD default preferences?"
))

(set #ask-muevdset-help
(cat "MuEVD keeps its preferences in an editable text file that should be "
     "kept in EnvArc:. By installing its default preferences explicitly, "
     "you have a handy template to edit and adjust its preferences.\n"
     @askdir-help
))

(set #where-muevdset
(cat "Copy the MuEVD preferences into which drawer:"
))

(set #ask-fastieee 
(cat "Install the FastIEEE speedup?"
))

(set #ask-fastieee-help
(cat "FastIEEE is a patch of the mathieeexxxx.libraries in LIBS: that will "
     "speed up these libraries by replacing math instructions that would have "
     "to be emulated on the 68040 or 68060 to a direct jump into the emulation "
     "routine itself. This patch requires that the emulator routines are made "
     "available by the so called \"fpsp.resource\" which has to be build by the "
     "68040 or 68060.library on startup.\n"
     "This is only useful for Os 3.1 and below as all newer math libraries "
     "check for this resource themselves already.\n"
     "Currently, only the MuLib based processor libraries create this resource. "
     "You cannot run FastIEEE on any third-party libraries."
))

(set #where-fastieee
(cat "Copy FastIEEE to which directory:"
))

(set #startup-fastieee
(cat "Run FastIEEE on startup?"
))

(set #startup-fastieee-go
(cat "The following command is added to the user-startup:\n"
))

(set #adapt-guide
(cat "Please wait...\nI'm adapting the MMU.guide to the new destination."
))

(set #ask-patch-check 
(cat "Replace popular MMU hacks by the MuTools?"
))

(set #ask-patch-check-help
(cat "This installation procedure is able to check your startup-sequence for "
     "some popular MMU hacks and replaces them by the apropriate MuTools if "
     "installed. Otherwise, the apropriate programs are disabled. Please "
     "check then your startup-sequence for completeness.\n"
     "The following changes are made:\n"
     "SetCPU FastROM, CPU FastROM and QuickROM are replaced by MuFastROM.\n"
     "Enforcer and CyberGuard are replaced by MuForce.\n"
     "GuardianAngel, MemSniff, MungWall and Wipeout are disabled. Please add "
     "MuGuardianAngel by hand yourself.\n"
     "PrepareEmul is disabled. Either, it was replaced by MuMove4K by the "
     "installation process, or you have to modify your startup-sequence yourself "
     "accordingly.\n"
     "SetCacheMode is replaced by MuSetCacheMode.\n"
     "SpeedyChip is replaced by MuFastChip.\n"
     "FastExec is disabled. It has been replaced by MuFastZero by this script, "
     "otherwise, you've to modify your startup-sequence accordingly."
))

(set #replace-quickrom
(cat "Replacing ROM remappers by MuFastROM (this may take a while)..."
))

(set #replace-enforcer
(cat "Replacing Enforcer by MuForce (this may take a while)..."
))

(set #replace-memsniff
(cat "Disabling memory debugging tools (this may take a while)..."
))

(set #replace-prepemul
(cat "Disabling PrepareEmul (this may take a while)..."
))

(set #replace-setcachemode
(cat "Replacing SetCacheMode (this may take a while)..."
))

(set #replace-speedychip
(cat "Replacing SpeedyChip (this may take a while)..."
))

(set #replace-fastexec
(cat "Replacing FastExec  (this may take a while)..."
))

(set #exit-text
(cat "The MuLib installation is now complete.\n"
     "Please edit now your startup-sequence and the "
     "user startup for fine tuning. Consult the MMU.Guide "
     "for further help about the library and its power."
))

;==Main program starts here====================================================
;############
;*** MAIN ***
;############

;==Get information about this system===========================================
;
; Initialize some strings
;

;==Get information about this system===========================================
;
; First, get some information about the system
;

(welcome)

; Kickstart version
(set #kversion (getversion))
(set #kversion (/ #kversion 65536))
(set #user-level @user-level)
(set #cpu (database "cpu"))
(set #fpu (database "fpu"))
(onerror (TERMINATE))

;==Initialize some variables (plus documentation) =============================

(set #target 		"SYS:")	;directory to install the files to
(set #musource 		"/")	;directory to take the sources from, assigned to ..MU..:
(set #startup-path	(tackon #target "S/Startup-Sequence")) ;where the startup-sequence can be found
(set #user-path		(tackon #target "S/User-Startup"))
(set #p5mode		0)	;1 if a P5 board (user is queried about this one)
(set #acamode		0)	;1 if a ACA board
(set #gvpmode		0)	;1 if a GVP board
(set #ppclib		0)	;1 if ppc.library found
(set #libresident	0)	;1 if 68040 or 68060 or ppc.library resident
(set #remproc		0)	;1 if BPPCFix must be run to remove the 040 or 060 lib	
(set #remppc		0)	;1 if BPPCFix must be run to remove the ppc.library
(set #install-lib 	1)	;1 if mmu.library should be installed
(set #patch-setpatch 	0)	;1 if SetPatch should be patched to use 680x0
(set #lib0x0-installed	0)	;1 if 680x0.library has been installed
(set #allow-020		1)	;1 if it makes sense to allow 68020/68030.lib
(set #install-proc 	1)	;install processor libraries?
(set #need-mmuconfig	0)	;1 if an ENVARC:MMU-Configuration is required
(set #need-bppcfix 	0)	;1 if BPPCFix should be run
(set #need-bcvisionfix	0)	;1 if the BVisionPPC CGfx driver requires fixing
(set #need-omniscsifix	0)	;1 if the muomniscsipatch should be installed
(set #mulib-proclibs	0)	;1 if MuLib based processor libraries
(set #requires-reboot	1)	;1 if a bppcfix/mumove4k should include a reboot
(set #new-setpatch	0)	;1 if setpatch has been patched
(set #need-mulocklib	1)	;1 if mulocklib should be installed

;==Check whether it makes sense to install the archive at all==================
(complete 0)
;
; Check for the processor first
;

(if (patmatch "(68000|68010)" #cpu)
	(if	(askbool	(prompt #senseless)
				(help	#senseless-help)
				(default #yes)
				(choices #yes #no)
		)
		(abort #canceled)
	)
)

;==Get target drive============================================================
(complete 3)
;
; Get the target directory of the installation
;

(set #old_wb "SYS:")

(set #target (askdir    (prompt #which-disk)
			(help #which-disk-help)
			(disk)
			(default #old_wb)
))

(if (not (and (exists (tackon #target "S/Startup-Sequence"))
	      (exists (tackon #target "DEVS"))
	      (exists (tackon #target "LIBS"))
	      (exists (tackon #target "C"))
	      (exists (tackon #target "Prefs/Env-Archive")) ))
	 (abort #no-assign)
)

;set @default-dest to user selected target
(set @default-dest #target)

;get directory to save old files
(set #stash_old (tackon #target #old-name))

;==Backup old files============================================================
(complete 6)
;
; Make a backup of old files
;

(set #startup-sequence  "S/Startup-Sequence")
(set #lib040		"Libs/68040.library")
(set #lib060		"Libs/68060.library")
(set #lib040old		"Libs/68040old.library")
(set #setpatch		"C/SetPatch")
(set #mmuconfig		"Prefs/Env-Archive/MMU-Configuration")
(set #mmuconfig-d	"Devs/MMU-Configuration")
(set #narrator		"Devs/narrator.device")
(set #iprefs		"C/IPrefs")
(set #mfm		"Devs/mfm.device")
(set #bppcfix		"C/BPPCFix")
(set #bvision		"Devs/Monitors/BVisionPPC")
(set #cvision		"Devs/Monitors/CVisionPPC")
(set #mulib		"Libs/mmu.library")

;get all existing files
(set #oldfiles "")

(P_CAT_OLD #startup-sequence)
(P_CAT_OLD #lib040)
(P_CAT_OLD #lib060)
(P_CAT_OLD #lib040old)
(P_CAT_OLD #setpatch)
(P_CAT_OLD #mmuconfig)
(P_CAT_OLD #mmuconfig-d)
(P_CAT_OLD #narrator)
(P_CAT_OLD #iprefs)
(P_CAT_OLD #mfm)
(P_CAT_OLD #bppcfix)
(P_CAT_OLD #bvision)
(P_CAT_OLD #cvision)
(P_CAT_OLD #mulib)

;if some oldfiles found
(if (<> #oldfiles "")
	(
    		(if 	(askbool	(prompt (#move-old #stash_old #oldfiles))
					(help (#move-old-help #stash_old))
					(choices #move-old-copy #move-old-skip)
					(default 1)
			)
			(
				; create new directory w icon
				(makedir #stash_old (infos))
				; create s and devs subdirectory
				(makedir (tackon #stash_old "S"))
				(makedir (tackon #stash_old "Libs"))
				(makedir (tackon #stash_old "C"))
				(makedir (tackon #stash_old "Devs") (infos))
				(makedir (tackon #stash_old "Devs/Monitors") (infos))
				(makedir (tackon #stash_old "Prefs") (infos))
				(makedir (tackon #stash_old "Prefs/Env-Archive") (infos))

				(P_COPY_OLD #startup-sequence)
				(P_COPY_OLD #lib040)
				(P_COPY_OLD #lib060)
				(P_COPY_OLD #lib040old)
				(P_COPY_OLD #setpatch)
				(P_COPY_OLD #mmuconfig)
				(P_COPY_OLD #mmuconfig-d)
				(P_COPY_OLD #narrator)
				(P_COPY_OLD #iprefs)
				(P_COPY_OLD #mfm)
				(P_COPY_OLD #bppcfix)
				(P_COPY_OLD #bvision)
				(P_COPY_OLD #cvision)
				(P_COPY_OLD #mulib)
			)
		)
	)
)

;==Get installation source=====================================================
(complete 9)
;
; if in expert mode, ask the user where to take the source from
;

(set #musource "/")

(if (or (> #user-level 1) (not (exists (tackon #musource "Libs/mmu.library"))))
    	(
    	 (user 2)
	 (message "need to pick source")
	 (set #musource (askdir (prompt #ask-mu)
				(help  #ask-mu-help)
				(default "/")
			)
	 ) 
	 (user #user-level)
	)
)

(makeassign "..MU.." #musource (safe))

;==Get MultiView===============================================================
(complete 10)

(set #multiview-path (tackon #target ("Utilities/Multiview")))

(if (not (exists #multiview-path))
	(set #multiview-path (tackon #target ("Tools/Multiview")))
)

(if (not (exists #multiview-path))
	(set #multiview-path #target)
)

(set #multiview-path	(askfile	(prompt	#where-multiview)
					(help #where-multiview-help)
					(default #multiview-path)
			)
)

(if (<> #multiview-path "")
	(run ("\"%s\" ..MU..:Readme.First!" #multiview-path) (safe))
)

;==Get even more information about the system==================================
(complete 12)
;
; check for resident libraries and P5 wierdos
;

(set #p5mode		0)
(set #acamode		0)
(set #gvpmode		0)
(set #ppclib		0)
(set #libresident	0)
(set #remproc		0)
(set #remppc		0)
(set #need-bcvisionfix	0)

; First, we need to locate the startup-sequence
(set #startup-path (tackon #target "S/Startup-Sequence"))

(if (> #user-level 1)
	(set	#startup-path	(askfile	(prompt	#where-startup)
						(help #where-startup-help)
						(default #startup-path)
				)
	)
)

(if (= #startup-path "")
	(set #startup-path (tackon #target "S/Startup-Sequence"))
)

(if (= (run "..MU..:Install/FindResident ppc.library" (safe) ) 0)
	(
		(set	#p5mode	1)
		(set	#ppclib 1)
		(set	#libresident 1)
		(set	#remppc 1)
	)
)

(if (= (run "..MU..:Install/FindResident 68040.library" (safe) ) 0)
	(
		(set	#p5mode 1)
		(set	#libresident 1)
		(set	#remproc 1)
	)
)

(if (= (run "..MU..:Install/FindResident 68060.library" (safe) ) 0)
	(
		(set	#p5mode 1)
		(set	#libresident 1)
		(set	#remproc 1)
	)
)

(if (= (run "..MU..:Install/FindResident 68060quick.library" (safe) ) 0)
	(
		(set	#p5mode 1)
		(set	#libresident 1)
		(set	#remproc 1)
	)
)

(if (= (run "..MU..:Install/FindResident BOOT-MMU-Port" (safe) ) 0)
	(
		(set	#p5mode 1)
	)
)

(if (= (run "..MU..:Install/FindPort BOOT-MMU-Port" (safe) ) 0)
	(
		(set	#p5mode 1)
	)
)

(if (exists (tackon #target "Libs/ppc.library"))
	(
		(set	#p5mode 1)
		(set	#ppclib	1)
	)
)

(if (= (run "..MU..:Install/FindResident CyberstormPPC.IDTag" (safe) ) 0)
	(
		(set	#p5mode 1)
	)
)

(if (= (run "..MU..:Install/FindResident CyberstormMK3.IDTag" (safe) ) 0)
	(
		(set	#p5mode 1)
	)
)

(if (= (run "..MU..:Install/FindResident BlizzardPPC.IDTag" (safe) ) 0)
	(
		(set	#p5mode 1)
	)
)

(if (= (run ("..MU..:Install/Sed TO NIL: FROM \"%s\" MATCH \"{(#[~;][/: \\t]|)BPPCFix([ ,\\t]#?|)}\"" #startup-path) (safe) ) 5)
	(
		(set	#p5mode 1)
		(set	#libresident 1)
	)
)

(if (= (run ("..MU..:Install/Sed TO NIL: FROM \"%s\" MATCH \"{(#[~;][/: \\t]|)BPPCFix([ ,\\t]#?|)040([ ,\\t]#?|)}\"" #startup-path) (safe) ) 5)
	(
		(set	#p5mode 1)
		(set	#libresident 1)
		(set	#remproc 1)
	)
)

(if (> #user-level 0)
	(set #p5mode	(askbool	(prompt	#ask-p5)
					(help	#ask-p5-help)
					(default #p5mode)
					(choices #yes #no)
			)
	)
)

(if (> #user-level 0)
	(set #acamode	(askbool	(prompt	#ask-aca)
					(help	#ask-aca-help)
					(default #acamode)
					(choices #yes #no)
			)
	)
)

(if (> #user-level 0)
	(set #gvpmode	(askbool	(prompt	#ask-gvp)
					(help	#ask-gvp-help)
					(default #gvpmode)
					(choices #yes #no)
			)
	)
)

;==Install the mmu.library=====================================================
(complete 16)
;
; check for the destination directory for the library and ask the user
; then copy the library to where it should go
;

(set #install-lib 1)
(set #lib-dest (tackon #target "LIBS"))

(if (> #user-level 1)
	(set #install-lib	(askbool	(prompt	#ask-copy-mmulib)
						(help   #copy-mmulib-help)
						(default #install-lib)
						(choices #yes #no)
				)
	)
)

(if (= #install-lib 1)
	(if (> #user-level 1)
		(set #lib-dest	(askdir		(prompt #ask-mmulib-dest)
						(help	#copy-mmulib-help)
						(default #lib-dest)
				)
		)
	)
)

(if (= #lib-dest "")
	(set #install-lib 0)
)

(if (= #install-lib 1)
	(copyfiles	(prompt	#copy-mmulib)
			(help	#copy-mmulib-help)
			(source	"..MU..:Libs/mmu.library")
			(dest	#lib-dest)
	)
)

;==Install the SetPatch upgrade================================================
(complete 19)
;
; ask the user whether to upgrade setpatch for auto-detecting the right
; processor library
;

(set #patch-setpatch 1)
(set #setpatch-dest (tackon #target "C/SetPatch"))
(set #lib0x0-dest (tackon #target "LIBS"))
(set #patch-file "")
(set #lib0x0-installed 0)
(set #new-setpatch 0)

;
; We need the setpatch destination anyhow, so ask about it now
;
(if (> #user-level 1)
	(set #setpatch-dest	(askfile	(prompt	#ask-setpatch)
						(help	#ask-patch-setpatch-help)
						(default #setpatch-dest)
				)
	)
)

;
;Use default if the reply is nonsense

(if (= #setpatch-dest "")
	(
		(set #patch-setpatch 0)
		(set #setpatch-dest (tackon #target "C/SetPatch"))
	)
)

; Get the setpatch revision. We need it anyhow.
(if (exists #setpatch-dest)
	(
		(set #setpatch-verrev (getversion #setpatch-dest))
		(set #setpatch-size   (getsize    #setpatch-dest))

		(if (= #setpatch-verrev (+ (* 40 65536) 16))
			(
				(set #patch-file "SetPatch_40_16.pch")
			)
		)
		(if (= #setpatch-verrev (+ (* 43 65536) 6))
			(
				(set #patch-file "SetPatch.pch")
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 2))
			(
				(set #patch-file "SetPatch_44.pch")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 6))
			(
				(set #patch-file "SetPatch_44_6.pch")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 13))
			(
				(set #patch-file "SetPatch_44_13.pch")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 16))
			(
				(set #patch-file "SetPatch_44_16.pch")
				(set #requires-reboot 0)
			)
		)
		(if (AND (= #setpatch-verrev (+ (* 44 65536) 17))
			 (<> #setpatch-size 32988))
			(
				(set #patch-file "SetPatch_44_17.pch")
				(set #requires-reboot 0)
			)
		)

		(if (= #setpatch-verrev (+ (* 43 65536) 7))
			(
				(set #patch-file "+")
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 3))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 7))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 14))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (AND (= #setpatch-verrev (+ (* 44 65536) 17))
			 (= #setpatch-size 32988))
			(	
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (AND (= #setpatch-verrev (+ (* 40 65536) 17))
			 (= #setpatch-size 13484))
			(	
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 18))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (= #setpatch-verrev (+ (* 44 65536) 38))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
		(if (>= #setpatch-verrev (+ (* 45 65536) 0))
			(
				(set #patch-file "+")
				(set #requires-reboot 0)
			)
		)
	)
)

;
;The installer script found a new SetPatch release
;
(if (= #patch-file "+")
        (       		   
		(set #new-setpatch 1)
		(set #patch-setpatch 0)
	)
	(
		(if (> #user-level 1)
		    (set #patch-setpatch	(askbool	(prompt	#ask-patch-setpatch)
	     	    						(help	#ask-patch-setpatch-help)
								(default #patch-setpatch)
								(choices #yes #no)
						)
		    )
		)
	)
)


(if (= #patch-setpatch 1)
	(
		(if (> #user-level 1)
			(set #lib0x0-dest	(askdir	(prompt #ask-lib0x0-dest)
							(help	#ask-lib0x0-dest-help)
							(default #lib0x0-dest)
						)
			)
		)
		(if (= #lib0x0-dest "")
			(set #patch-setpatch 0)
		)
	)
)

(if (= #patch-setpatch 1)
	(
		(if (= #patch-file "")
			(
				(message #setpatch-unknown)
				(set #patch-setpatch 0)
			)
		)
	)
)

(if (= #patch-setpatch 1)
	(

		(if (= #new-setpatch 0)
			(

				(run ("..MU..:Fixes/spatch -oT:SetPatch -p..MU..:Fixes/%s \"%s\"" #patch-file #setpatch-dest))

				(if (exists "T:SetPatch")
					(
						(copyfiles	(prompt	#copy-new-setpatch)
								(help #ask-patch-setpatch-help)
								(source "T:SetPatch")
								(dest (pathonly #setpatch-dest))
						)
						(delete	"T:SetPatch" (safe))
						;note that SetPatch has now been updated
						(set #new-setpatch 1)
					)
					(abort #setpatch-unknown)
				)
			)
		)

		(copylib	(prompt #copy-lib0x0)
				(help	#ask-patch-setpatch-help)
				(source	"..MU..:Libs/680x0.library")
				(dest	#lib0x0-dest)
		)

		(set #lib0x0-installed 1)
	)
)

;==Install the processor libraries=============================================
(complete 22)
;
; ask the user whether to install the mmu.lib based processor libraries
;


; try to check whether it makes sense to install the 68020/68030

(set #allow-020	0)				;does it make sense to allow 68020/68030.lib
(set #install-proc 1)				;install processor libraries?
(set #proc-dest (tackon #target "LIBS"))	;where to install them?
(set #cpu-mask 0)				;which libraries to install?
(P_FIND_CPU)

;
; This *requires* a new setpatch. Otherwise, the 020-libs are never loaded.
;
(if (= #new-setpatch 1)
	(set #allow-020 1)
)

(if (= #allow-020 1)
	(

		(if (= "68020" #cpu)
			(set #cpu-mask 1)
		)
		(if (= "68030" #cpu)
			(set #cpu-mask 2)
		)
	)
)

(if (= "68040" #cpu)
	(set #cpu-mask 4)
)

(if (= "68060" #cpu)
	(set #cpu-mask 8)
)

(if (= #allow-020 1)
	(	;68020 and 68030 are an option

		(if (> #user-level 1)
			(
				(set #cpu-mask	(askoptions	(prompt	#which-proclibs)
								(help	#copy-proclib-help)
								(default #cpu-mask)
								(choices "68020" "68030" "68040" "68060")
						)
				)
			)
		)

		(if (= #cpu-mask 0)
			(set #install-proc 0)
		)
	)
	(	;only 68040 or 68060 library could possibly be installed
		;do not give options, but just install the one and only library
		;which is useful.	
		(if (= #cpu-mask 0)
			(
				(set #install-proc 0)
			)
			(
				(if (> #user-level 1)
					(
						(set #install-proc	(askbool	(prompt #ask-proclib)
						     					(help	#copy-proclib-help)
											(default #install-proc)
											(choices #yes #no)
									)
						)
					)
				)
			)
		)
	)
)

(complete 24)

(if (= #install-proc 1)
	(if (> #user-level 1)
		(set #proc-dest	(askdir		(prompt #ask-proclib-dest)
						(help	#copy-proclib-help)
						(default #proc-dest)
				)
		)
	)

	(if (= #proc-dest "")
		(set #install-proc 0)
	)
)

;now copy the selected libraries.
;note that we do NOT use copylib as we want to replace libraries with
;higher version numbers 
;
(set #need-mmuconfig 0)

(if (= #install-proc 1)
	(
		(if (> (bitand #cpu-mask 1) 0)
			(P_COPYCPU "68020")
		)
		(if (> (bitand #cpu-mask 2) 0)
			(P_COPYCPU "68030")
		)
		(if (> (bitand #cpu-mask 4) 0)
			(P_COPYCPU "68040")
		)
		(if (> (bitand #cpu-mask 8) 0)
			(P_COPYCPU "68060")
		)
		(set #need-mmuconfig 1)
	)
)

;==Setup an MMU-Configuration==================================================
(complete 27)
;
; check whether we need an MMU-Configuration file. If so, generate it now.
; This is required if some mmu.library based libraries are present

(if (= #proc-dest "")
	(set #proc-dest (tackon #target "LIBS"))
)

(set #mmuconfig-exists 0)
(set #config-path (tackon #target "Prefs/Env-Archive"))

; If we found a MuLib processor library, the BVision driver
; must be fixed as well.
(if (= #need-mmuconfig 1)
	(
		(set #mulib-proclibs 1)
		(set #need-bcvisionfix 1)
	)
)

(if (exists (tackon #target "Prefs/Env-Archive/MMU-Configuration"))
	(
		(set #mmuconfig-exists 1)
		(set #need-mmuconfig 0)
		(if (and (= #p5mode 1) (not (exists (tackon #proc-dest "MMU/P5Init"))))
			(set #need-mmuconfig 1)
		)
		(if (and (= #acamode 1) (not (exists (tackon #proc-dest "MMU/ACAInit"))))
			(set #need-mmuconfig 1)
		)
		(if (and (= #gvpmode 1) (not (exists (tackon #proc-dest "MMU/GVPInit"))))
			(set #need-mmuconfig 1)
		)
	)
)

(if (exists (tackon #target "Devs/MMU-Configuration"))
	(
		(set #mmuconfig-exists 1)
		(set #config-path (tackon #target "Devs"))
		(set #need-mmuconfig 0)
		(if (and (= #p5mode 1) (not (exists (tackon #proc-dest "MMU/P5Init"))))
			(set #need-mmuconfig 1)
		)
		(if (and (= #acamode 1) (not (exists (tackon #proc-dest "MMU/ACAInit"))))
			(set #need-mmuconfig 1)
		)
		(if (and (= #gvpmode 1) (not (exists (tackon #proc-dest "MMU/GVPInit"))))
			(set #need-mmuconfig 1)
		)
	)
)

;ask the expert user if our choice is all right
(if (> #user-level 1)
	(if (= #mmuconfig-exists 1)
		(set #need-mmuconfig	(askbool	(prompt #overwrite-config)
							(help   #build-config-help)
							(default #need-mmuconfig)
							(choices #yes #no)
					)
		)
		(set #need-mmuconfig	(askbool	(prompt #build-config)
							(help   #build-config-help)
							(default #need-mmuconfig)
							(choices #yes #no)
					)
		)
	)
)

;now ask the expert where to put the file if we need it
(if (= #need-mmuconfig 1)
	(
		(if (> #user-level 1)
			(set #config-path	(askdir	(prompt #ask-config-dest)
							(help	#ask-config-help)
							(default #config-path)
						)
			)
		)

		(if (= #config-path "")
			(set #need-mmuconfig 0)
		)
	)
)

(if (= #need-mmuconfig 1)
	(
		(set #config-source "..MU..:Envarc/MMU-Configuration")
		(if (= #gvpmode 1)
			(set #config-source "..MU..:Envarc/MMU-Configuration.GVP")
		)
		(if (= #p5mode 1)
			(set #config-source "..MU..:Envarc/MMU-Configuration.P5")
		)
		(if (= #acamode 1)
			(set #config-source "..MU..:Envarc/MMU-Configuration.ACA")
		)

		(copyfiles	(prompt	#config-copy)
				(source #config-source)
				(dest	#config-path)
				(newname "MMU-Configuration")
		)

		;in P5 mode, "P5Init" must be copied as well.
		(if (= #p5mode 1)
			(
				(if (not (exists (tackon #proc-dest "MMU")))
					(makedir (tackon #proc-dest "MMU"))
				)
				(copyfiles	(source "..MU..:Libs/MMU/P5Init")
						(dest	(tackon #proc-dest "MMU"))
				)
			)
		)
		;in ACA mode, "ACAInit" must be copied as well.
		(if (= #acamode 1)
			(
				(if (not (exists (tackon #proc-dest "MMU")))
					(makedir (tackon #proc-dest "MMU"))
				)
				(copyfiles	(source "..MU..:Libs/MMU/ACAInit")
						(dest	(tackon #proc-dest "MMU"))
				)
			)
		)
		;in GVP mode, "GVPInit" must be copied as well.
		(if (= #gvpmode 1)
			(
				(if (not (exists (tackon #proc-dest "MMU")))
					(makedir (tackon #proc-dest "MMU"))
				)
				(copyfiles	(source "..MU..:Libs/MMU/GVPInit")
						(dest	(tackon #proc-dest "MMU"))
				)
			)
		)
	)
)

;==Install BPPCFix=============================================================
(complete 30)
;
; check whether we need to run BPPCFix to replace the ROM-resident 
; P5 libraries by the MMU-Libraries.

(set #need-bppcfix 0)
(set #bppc-reboot  1)
(set #bppcline "BPPCFix Install")

;
; Now check whether we need to include a reboot.
; This goes for i) SetPatch is older than V43 
; or ii) NOROMUPDATES is set.

(if (= #requires-reboot 0)	;assumption: new SetPatch found so far
	(			;the following line-noise will filter the startup sequence
		(if (= 5 (run ("..MU..:Install/SED TO NIL: FROM \"%s\" MATCH \"(#[~;][/: \\t]|)SetPatch+[ \\t](|+[~;]+[ \\t])NOROMUPDATE([; \\t]#?|)\"" #startup-path) (safe)))
			(set #requires-reboot 1) ;SetPatch run with NOROMUPDATES here.
		)
	)
)

; The BPPCFix is required if either the ppc.library is in ROM
; or the 68040/68060 library is in ROM.

(if (= #libresident 1)
	(
		(set #need-bppcfix 1)
		(set #bppcline "BPPCFix Install")
	)
)
(if (= #remproc 1)
	(
		(set #need-bppcfix 1)
		(set #bppcline "BPPCFix Install 040")
	)
)

;
; Add a reboot if this is required.
(if (= #requires-reboot 1)
	(set #bppcline (cat #bppcline " Reboot"))
)

; Now ask the expert user whether this is ok.
(if (> #user-level 1)
	(set #need-bppcfix	(askbool	(prompt #install-bppcfix)
						(help   #install-bppcfix-help)
						(default #need-bppcfix)
						(choices #yes #no)
				)
	)
)

;Ask the expert about the BPPC command line
(if (= #need-bppcfix 1)
	(if (> #user-level 1)
		(set #bppcline (askstring 	(prompt #set-bppcline)
						(help   #set-bppcline-help)
						(default #bppcline)
				)
		)
	)
)

(if (= #bppcline "")
	(set #need-bppcfix 0)
)

;Ask the Expert user where to install the BPPCFix

(set #bppcfix-path (tackon #target "C"))

(if (= #need-bppcfix 1)
	(if (> #user-level 1)
		(set #bppcfix-path 	(askdir	(prompt #where-bppcfix)
						(help #where-bppcfix-help)
						(default #bppcfix-path)
						(newpath)
				)
		)
	)
)

(if (= #bppcfix-path "")
	(set #need-bppcfix 0)		;...else...
	(set #bppcline (tackon #bppcfix-path #bppcline))
)

;If we still need to run bppcfix, prepend its command line to the startup-sequence
(if (= #need-bppcfix 1)
	(
		(copyfiles	(source "..MU..:Install/BPPCFix")
				(dest #bppcfix-path)
		)
		(run ("echo >t:__bppcfix_line__ \"%s\"" #bppcline) (safe))
		; the following line copies the Startup sequence to t: removing
		; all references to BPPCFix
		(run ("..MU..:Install/Sed FROM \"%s\" MATCH \"{(#[~;][/: \\t]|)BPPCFix([ ,\\t]#?|)}\" CHANGE \";{1}\" TO t:__tempfile__" #startup-path) (safe))
		(run ("c:join t:__bppcfix_line__ t:__tempfile__ to \"%s\"" #startup-path))
		(run ("c:delete t:__bppcfix_line__ t:__tempfile__ QUIET") (safe))

		;As the BPPCFix covers now the reboot, we don't need to do it.
		(set #requires-reboot 0)
	)
)

;==Install the BVision fix=====================================================
(complete 35)
;
; check whether the P5/CGfx BVision monitor driver is in DEVS:Monitors.
; If it is and we use the MuLib based processor libraries, we must
; hack it to call the p5emu.library instead of the 68040/68060 library.
;

; The #need-bcvisionfix variable has been set above already.

(set #bvision-dest	(tackon #target "Devs/Monitors/BVisionPPC"))
(set #cvision-dest	(tackon #target "Devs/Monitors/CVisionPPC"))

(if (> #user-level 1)
	(set #need-bcvisionfix	(askbool	(prompt	#fix-bvision)
						(help	#fix-bcvision-help)
						(default #need-bcvisionfix)
						(choices #yes #no)
				)
	)
)

(if (= #need-bcvisionfix 1)
	(if (> #user-level 1)
		(
			(set #bvision-dest 	(askfile	(prompt #where-bvision)
								(help	#where-bvision-help)
								(default #bvision-dest)
						)
			)

			(set #cvision-dest	(askfile	(prompt	#where-cvision)
								(help	#where-cvision-help)
								(default #cvision-dest)
						)
			)
		)
	)
)

(if (= #need-bcvisionfix 0)
	(
		(set #bvision-dest "")
		(set #cvision-dest "")
	)
	(
		(if (not (exists #bvision-dest))
			(set #bvision-dest "")
		)
		(if (not (exists #cvision-dest))
			(set #cvision-dest "")
		)
	)
)

(if (NOT (= #bvision-dest ""))
	(
		(run ("..MU..:Fixes/PatchString \"%s\" 68040.library p5emu.library" #bvision-dest))
		(run ("..MU..:Fixes/PatchString \"%s\" 68060.library p5emu.library" #bvision-dest))
	)
)

(if (NOT (= #cvision-dest ""))
	(
		(run ("..MU..:Fixes/PatchString \"%s\" 68040.library p5emu.library" #cvision-dest))
		(run ("..MU..:Fixes/PatchString \"%s\" 68060.library p5emu.library" #cvision-dest))
	)
)

;==Install the MuTools=========================================================
(complete 37)
;
; ask the user about various tool files
;

; Define some mask values for convenience and for simpler
; reference.

(set #mufastrom-mask	1)
(set #mufastzero-mask	2)
(set #mumove4k-mask	4)
(set #muscan-mask	8)
(set #muforce-mask	16)
(set #mga-mask		32)
(set #muomniscsi-mask	64)
(set #musetcache-mask	128)
(set #mufastchip-mask	256)
(set #muevd-mask	512)
(set #mulocklib-mask	1024)
(set #mulink-mask	2048)
(set #mmuguide-mask	4096)
(set #muprotect-mask	8192)

(set #toolmask #mufastrom-mask)
(set #toolmask (bitor #toolmask #muscan-mask))
(set #toolmask (bitor #toolmask #mmuguide-mask))
;
; Install MuForce, MGA, MuLink,... only in expert mode
(if (> #user-level 1)
	(
		(set #toolmask (bitor #toolmask #muforce-mask))
		(set #toolmask (bitor #toolmask #mga-mask))
		(set #toolmask (bitor #toolmask #musetcache-mask))
		(set #toolmask (bitor #toolmask #mulink-mask))
	)
)

; Install MuFastChip, MuLockLib only if no MuLib based processor libraries
; doesn't make sense otherwise.
;

(set #run-mufastchip 0)		;run it not by default

(if (= #mulib-proclibs 0)
	(
		(set #toolmask (bitor #toolmask #mulocklib-mask))
		(set #toolmask (bitor #toolmask #mufastchip-mask))
		;
		;doesn't make sense on 030 or 020.
		(if (= #cpu "68040")
			(set #run-mufastchip 1)
		)
		(if (= #cpu "68060")
			(set #run-mufastchip 1)
		)
	)
	;...else...
	; Install MuFastZero only if MuLib based processor libs are in
	; since P5 libs do this by default (I don't, I'm too careful!)
	(
		(if (> #user-level 1)
			(	 
				(set #toolmask (bitor #toolmask #mufastzero-mask))
				(set #toolmask (bitor #toolmask #mumove4k-mask))
			)
		)
	)
)

; Install MuOmniSCSIPatch only if the omniscsi-device is found
;
(if (= (run "..MU..:Install/FindDevice omniscsi.device" (safe)) 0)
	(
		(set #toolmask (bitor #toolmask #muomniscsi-mask))
	)
)

; Now ask the user whether the choice is fine.
;
(if (exists "..MU..:MuTools")
	(
		(set #toolmask	(askoptions	(prompt	#pick-mutools)
						(help #pick-mutools-help)
						(choices 	"MuFastRom"	"MuFastZero"
								"MuMove4K"	"MuScan"
								"MuForce"	"MuGuardianAngel"
								"MuOmniSCSIPatch" "MuSetCacheMode"
								"MuFastChip"	"MuEVD"
								"MuLockLib"	"MuLink"
								"MMU.guide"	"MuProtectModules"
						)
						(default #toolmask)
				)
		)
	)
	(
		(set #toolmask 0)
	)
)

;
; Query destionation for guides and files, but only if we have toolmasks
;
(set #mutool-path	(tackon #target "MuTools"))

(if (<> #toolmask 0)
	(
		(set #mutool-path	(askdir	(prompt #where-mutools)
						(help #where-mutools-help)
						(default #mutool-path)
						(newpath)
					)
		)
	)
)

(if (= #mutool-path "")
	(set #toolmask 0)
	(if (not (exists #mutool-path))		;note that this is in the else-part
		(makedir #mutool-path (infos))
	)
)		

(set #mutool-guide-path	#mutool-path)

(complete 39)

(if (<>	#toolmask 0)
	(
		(set #mutool-guide-path	(askdir (prompt #where-mutool-guide)
						(help #where-mutool-guide-help)
						(default #mutool-guide-path)
						(newpath)
					)
		)
	)
)

(if (<> #mutool-guide-path "")
	(if (not (exists #mutool-guide-path))
		(makedir #mutool-guide-path (infos))
	)
)

;Now, one after another, install all the tools and documentation.

;==Install MuFastROM===========================================================
(complete 42)

(if (<> (bitand #toolmask #mufastrom-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuFastROM")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuFastROM.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)

		(set #run-mufastrom 1)
		
		(set #run-mufastrom	(askbool	(prompt #startup-mufastrom)
							(help	#startup-mufastrom-help)
							(default 1)
							(choices #yes #no)
					)
		)

		(set #mufastrom-cmd "On Protect >NIL:")

		(if (= #run-mufastrom 1)
			(startup "MuFastROM"	(prompt (cat #startup-mufastrom-go "MuFastRom " #mufastrom-cmd))
						(help #startup-mufastrom-help)
						(command ("%s %s" (tackon #mutool-path "MuFastRom") #mufastrom-cmd))
			)
		)
	)
)

;==Install MuFastZero==========================================================
(complete 45)

(set #run-mumove4k 0)	;for later, possibly set to 1

(if (<> (bitand #toolmask #mufastzero-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuFastZero")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuFastZero.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)

		(set #run-mufastzero 1)
		(set #mufastzero-cmd "ON")

(complete 46)

		;ask whether it is ok to run it on startup		

		(set #run-mufastzero	(askbool	(prompt #startup-mufastzero)
							(help	#startup-mufastzero-help)
							(default 1)
							(choices #yes #no)
					)
		)

		;in expert mode, query more startup options

		(if (and (> #user-level 1) (= #run-mufastzero 1))
			(

				;remap exec as well?
				(if 	(= 1	(askbool	(prompt #remap-exec)
								(help #remap-exec-help)
								(default 0)
								(choices #yes #no)
						) 
					)
					(
						(set #toolmask (bitor #toolmask #mumove4k-mask))
						(set #run-mumove4k 1)
						(set #mufastzero-cmd (cat #mufastzero-cmd " FastExec"))
					)
				)
				;remap the SSP?
				(if	(= 1	(askbool	(prompt #remap-ssp)
								(help #remap-ssp-help)
								(default 0)
								(choices #yes #no)
						)
					)
					(set #mufastzero-cmd (cat #mufastzero-cmd " MoveSSP"))
		
				)
			)
		)
		
		(set #mufastzero-cmd (cat #mufastzero-cmd " >NIL:"))

		(if (= #run-mufastzero 1)
			(startup "MuFastZero"	(prompt (cat #startup-mufastzero-go "MuFastZero " #mufastzero-cmd))
						(help #startup-mufastzero-help)
						(command ("%s %s" (tackon #mutool-path "MuFastZero") #mufastzero-cmd))
			)
		)
	)
)

;==Install MuMove4K============================================================
(complete 47)

(if (<> (bitand #toolmask #mumove4k-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuMove4K")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuMove4K.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)

		(set #mumove4k-cmd "MuMove4K")
		(if (= #p5mode 1)
			(set #mumove4k-cmd (cat #mumove4k-cmd " A1200"))
		)

		;if we require a reboot, do it now.
		(if (= #requires-reboot 0)
			(set #mumove4k-cmd (cat #mumove4k-cmd " NoReboot"))
		)

(complete 49)

		;ask whether we should run it.
		(if (> #user-level 1)
			(set #run-mumove4k	(askbool	(prompt #startup-mumove4k)
								(help	#startup-mumove4k-help)
								(default #run-mumove4k)
								(choices #yes #no)
						)
			)
		)

		(set #include-prep 0)
		;check whether PrepareEmul is run on the startup-sequence
		;
		(if (= 5 (run ("..MU..:Install/SED TO NIL: FROM \"%s\" MATCH \"(#[~;][/: \\t]|)(MuMove4K[ ,\\t]#?|)PrepareEmul([ ,\\t]#?|)\"" #startup-path) (safe)))
			(set #include-prep 1)
		)

(complete 51)

		;ask whether we should include the prepareemul-feature
		;
		(if (= #run-mumove4k 1)
			(
				(if 	(= 1 	(askbool	(prompt #prepareemul)
								(help #prepareemul-help)
								(default 0)
								(choices #yes #no)
						)
					)
					(set #mumove4k-cmd (cat #mumove4k-cmd " PrepareEmul"))
				)
			)
		)

(complete 53)
		
		;ask the expert user if the startup line is ok
		(if (= #run-mumove4k 1)
			(if (> #user-level 1)
				(set #mumove4k-cmd 	(askstring	(prompt #edit-mumove4k)
									(help #edit-mumove4k-help)
									(default #mumove4k-cmd)
							)
				)
			)
		)

		(if (= #mumove4k-cmd "")
			(set #run-mumove4k 0)
		)

(complete 55)

		;if we still need to run MuMove4K, install it now
		(if (= #run-mumove4k 1)
			(
				(set #mumove4k-cmd (tackon #mutool-path #mumove4k-cmd))
				(run ("echo >t:__mumove4k_line__ \"%s\"" #mumove4k-cmd) (safe))
				; the following line copies the Startup sequence to t: removing
				; all references to MuMove4k and PrepareEmul
				(run ("..MU..:Install/Sed FROM \"%s\" MATCH \"{(#[~;][/: \\t]|)(MuMove4k|PrepareEmul)([ ,\\t]#?|)}\" CHANGE \";{1}\" TO t:__tempfile__" #startup-path) (safe))
				(run ("c:join t:__mumove4k_line__ t:__tempfile__ to \"%s\"" #startup-path))
				(run ("c:delete t:__mumove4k_line__ t:__tempfile__ QUIET") (safe))

				;As the MuMove4k covers now the reboot, we don't need to do it.
				(set #requires-reboot 0)
			)
		)
	)
)

;==Install MuScan==============================================================
(complete 57)

(if (<> (bitand #toolmask #muscan-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuScan")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuScan.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
	)
)

;==Install MuGuardianAngel=====================================================
(complete 60)

(if (<> (bitand #toolmask #mga-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuGuardianAngel")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(copyfiles	(source	"..MU..:MuTools/MuGuardianAngel_Off")
				(dest #mutool-path)
				(infos) (noposition)
		)


		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuGuardianAngel.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)

		;If MGA is run, we need to install MuForce as well
		;
		(set #toolmask (bitor #toolmask #muforce-mask))

(complete 62)

		;now check whether we need to run FixCybAccess or FixP5Scsi
		;
		(set #need-cybfix 0)
		(set #need-fixp5scsi 0)

		(if (= 0 (run ("..MU..:Install/FindDevice cybscsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)

		(if (= 0 (run ("..MU..:Install/FindDevice z3scsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)

		(if (= 0 (run ("..MU..:Install/FindDevice 2060scsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)

		(if (= 0 (run ("..MU..:Install/FindDevice 2040scsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)

		(if (= 0 (run ("..MU..:Install/FindDevice 1260scsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)

		(if (= 0 (run ("..MU..:Install/FindDevice 1240scsi.device") (safe)))
			(set #need-cybfix 1)
			(set #need-fixp5scsi 1)
		)


		;this fix might require updating more P5 stuff...
		;
		(if (> #user-level 1)
			(set #need-cybfix 	(askbool	(prompt #ask-cybfix)
								(help #ask-cybfix-help)
								(default #need-cybfix)
								(choices #yes #no)
						)
			)
		)

		(set #cybfix-path (tackon #target "C"))

		(set #where-string	#where-cybfix)
		(set #fixstring		"FixCybAccess")
		(set #gostring		"FixCybAccess >NIL:")
		(set #startupcmd	"FixCybAccess")
		(if (= #need-fixp5scsi 1)
			(
				(set #where-string	#where-fixp5scsi)
				(set #fixstring		"FixP5Scsi")
				(set #gostring		"FixP5Scsi >NIL:")
				(set #startupcmd	"FixP5Scsi")
			)
		)
		
		;
		;ask the user where to put it
		;
		(if (and (= #need-cybfix 1) (> #user-level 1))
			(set #cybfix-path	(askdir		(prompt #where-string)
								(help #ask-cybfix-help)
								(default #cybfix-path)
								(newpath)
						)
			)
		)

		(if (= #cybfix-path "")
			(set #need-cybfix 0)
		)

		(if (= #need-cybfix 1)
			(
				(copyfiles	(source	"..MU..:Fixes/FixP5Scsi")
						(dest #cybfix-path)
				)
				(startup #fixstring	(prompt (cat #startup-cybfix-go #gostring))
							(help #ask-cybfix-help)
							(command ("run <>NIL: %s >NIL:" (tackon #cybfix-path #startupcmd)))
				)
			)
		)

(complete 64)

		;now check for the IPrefs stack size increasement patch
		;	
		(set #iprefs (tackon #target "C/IPrefs"))
		(if (> #user-level 1)
			(set	#iprefs	(askfile	(prompt #where-iprefs)
							(help #where-iprefs-help)
							(default #iprefs)
					)
			)
		)

		(set #patch-iprefs 0)
		
		(if (<> #iprefs "")
			(if (exists #iprefs)
				(
					(set #iprefs-verrev (getversion #iprefs))
					(if (< #iprefs-verrev (+ (* 45 65536) 0))
						(set #patch-iprefs 1)
					)
				)
			)
		)

		(if (> #user-level 1)
			(set #patch-iprefs	(askbool	(prompt #ask-iprefs-patch)
								(help #where-iprefs-help)
								(default #patch-iprefs)
								(choices #yes #no)
						)
			)
		)

		(if (= #patch-iprefs 1)
			(
				(if (exists "t:__tempfile__")
					(run ("C:Delete t:__tempfile__ QUIET") (safe))
				)
				(run ("..MU..:Fixes/SPatch -ot:__tempfile__ -p..MU..:Fixes/IPrefs.pch \"%s\"" #iprefs) (safe))
				(if (exists "t:__tempfile__")
					(
						(run ("C:Copy t:__tempfile__ \"%s\"" #iprefs))
						(run ("C:Delete t:__tempfile__ QUIET") (safe))
					)
				)
			)
		)

(complete 66)
		;now check for the narrator stack size increasement patch
		;	
		(set #narrator (tackon #target "DEVS/narrator.device"))

		(set #patch-narrator 0)
		
		(if (> #user-level 1)
			(set	#narrator (askfile	(prompt #where-narrator)
							(help #where-narrator-help)
							(default #narrator)
					)
			)
		)
		
		(if (<> #narrator "")
			(if (exists #narrator)
				(
					(set #narrator-verrev (getversion #narrator))
					(if (= #narrator-verrev (+ (* 37 65536) 7))
						(set #patch-narrator 1)
					)
					(if (> #user-level 1)
						(set #patch-narrator	(askbool	(prompt #ask-narrator-patch)
				     							(help #where-narrator-help)
											(default #patch-narrator)
											(choices #yes #no)
									)
						)
					)
				)
			)
		)	

		(if (= #patch-narrator 1)
			(
				(if (exists "t:__tempfile__")
					(run ("C:Delete t:__tempfile__ QUIET") (safe))
				)
				(run ("..MU..:Fixes/SPatch -ot:__tempfile__ -p..MU..:Fixes/narrator.device.pch \"%s\"" #narrator) (safe))
				(if (exists "t:__tempfile__")
					(
						(run ("C:Copy t:__tempfile__ \"%s\"" #narrator))
						(run ("C:Delete t:__tempfile__ QUIET") (safe))
					)
				)
			)
		)

(complete 68)

		;now check for the mfm.device stack size increasement patch
		;	
		(set #mfm (tackon #target "DEVS/mfm.device"))
		(if (> #user-level 1)
			(set	#mfm	(askfile	(prompt #where-mfm)
							(help #where-mfm-help)
							(default #mfm)
					)
			)
		)
			
		(set #patch-mfm 0)
		
		(if  (<> #mfm "")
			(if (exists #mfm)
				(
					(set #mfm-verrev (getversion #mfm))
					(if (= #mfm-verrev (+ (* 40 65536) 9))
						(set #patch-mfm 1)
					)
					(if (> #user-level 1)
						(set #patch-mfm	(askbool	(prompt #ask-mfm-patch)
				     						(help #where-mfm-help)
										(default #patch-mfm)
										(choices #yes #no)
								)
						)
					)
				)
			)
		)

		(if (= #patch-mfm 1)
			(
				(if (exists "t:__tempfile__")
					(run ("C:Delete t:__tempfile__ QUIET") (safe))
				)
				(run ("..MU..:Fixes/SPatch -ot:__tempfile__ -p..MU..:Fixes/mfmdev.pch \"%s\"" #mfm) (safe))
				(if (exists "t:__tempfile__")
					(
						(run ("C:Copy t:__tempfile__ \"%s\"" #mfm))
						(run ("C:Delete t:__tempfile__ QUIET") (safe))
					)
				)
			)
		)
	)
)

;==Install MuForce=============================================================
(complete 70)

(if (<> (bitand #toolmask #muforce-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuForce")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(copyfiles	(source	"..MU..:MuTools/MuForce_Window")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(copyfiles	(source	"..MU..:MuTools/MuForce_Off")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuForce.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
	)
)

;==Install MuOmniSCSIPatch=====================================================
(complete 72)

(if (<> (bitand #toolmask #muomniscsi-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuOmniSCSIPatch")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuOmniSCSIPatch.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)

		;run this in the startup-sequence?	
		;
		(set #run-muomni 0)
		
		(if (= 0 (run ("..MU..:Install/FindDevice omniscsi.device") (safe)))
			(set #run-muomni 1)
		)
		
		(if (> #user-level 1)
			(set #run-muomni	(askbool 	(prompt #fix-omniscsi)
								(help #fix-omniscsi-help)
								(default #run-muomni)
								(choices #yes #no)
						)
			)
		)


		(if (= #run-muomni 1)
			(startup "MuOmniSCSIPatch"	(prompt (cat #startup-omniscsi-go "MuOmniSCSIPatch >NIL:"))
							(help #fix-omniscsi-help)
							(command ("%s >NIL:" (tackon #mutool-path "MuOmniSCSIPatch")))
			)
		)
	)
)

;==Install MuSetCacheMode======================================================
(complete 75)

(if (<> (bitand #toolmask #musetcache-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuSetCacheMode")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuSetCacheMode.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
	)
)

;==Install MuFastChip==========================================================
(complete 77)

(if (<> (bitand #toolmask #mufastchip-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuFastChip")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuFastChip.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
		
		;run-mufastchip was setup at the tool
		;selection window.
		;
		(if (> #user-level 1)
			(set	#run-mufastchip	(askbool	(prompt #ask-mufastchip)
								(help #ask-mufastchip-help)
								(default #run-mufastchip)
								(choices #yes #no)
						)
			)
		)

		(if (= #run-mufastchip 1)
			(startup "MuFastChip"		(prompt (cat #startup-mufastchip-go "MuFastChip ON >NIL:"))
							(help #ask-mufastchip-help)
							(command ("%s ON >NIL:" (tackon #mutool-path "MuFastChip")))
			)
		)
	)
)

;==Install MuProtectModules=======================================================
(complete 79)

(if (<> (bitand #toolmask #muprotect-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuProtectModules")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuProtectModules.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
		
		;run-muprotectmodules was setup at the tool
		;selection window.
		;
		(if (> #user-level 1)
			(set	#run-muprotect	(askbool	(prompt #ask-muprotect)
								(help #ask-muprotect-help)
								(default #run-muprotect)
								(choices #yes #no)
						)
			)
		)

		(if (= #run-muprotect 1)
			(startup "MuProtectModules"	(prompt (cat #startup-muprotect-go "MuProtectModules ON >NIL:"))
							(help #ask-muprotect-help)
							(command ("%s ON >NIL:" (tackon #mutool-path "MuProtectModules")))
			)
		)
	)
)

;==Install MuEVD===============================================================
(complete 81)

(if (<> (bitand #toolmask #muevd-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuEVD")
				(dest #mutool-path)
				(infos) (noposition)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuEVD.guide")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
		
		;now as the user whether to install MuEVD in the shapeshifter
		;directory.

		(set #shsh-install	(askbool	(prompt #ask-muevd)
							(help #ask-muevd-help)
							(default 0)
							(choices #yes #no)
					)
		)

		(if (= #shsh-install 1)
			(set	#shsh-path	(askdir	(prompt #where-muevd)
							(help #ask-muevd-help)
							(default #target)
						)
			)
		)

		(if (= #shsh-path "")
			(set	#shsh-install	0)
		)
		
		(if (= #shsh-install 1)
			(copyfiles	(source	"..MU..:MuTools/MuEVD.guide")
					(dest	#shsh-path)
					(infos) (noposition)
			)
		)

		;Now copy the preferences
		;
		(if (= #config-path "")
			(set	#config-path	(tackon #target	"Prefs/Env-Archive"))
		)

		(if (= #shsh-install 1)
			(if (> #user-level 1)
				(set #shsh-install	(askbool	(prompt #ask-muevdset)
									(help #ask-muevdset-help)
									(default #shsh-install)
									(choices #yes #no)
							)
				)
			)
		)

		(if (= #shsh-install 1)
			(if (> #user-level 1)
				(set #config-path	(askdir		(prompt	#where-muevdset)
									(help	#ask-muevdset-help)
									(default #config-path)
							)
				)
			)
		)

		(if (= #config-path "")
			(set #shsh-install 0)
		)

		(if (= #shsh-install 1)
			(copyfiles	(source	"..MU..:EnvArc/MuEVD.prefs")
					(dest	#config-path)
			)
		)
	)
)

;==Install MuLockLib===========================================================
(complete 83)

(if (<> (bitand #toolmask #mulocklib-mask) 0)
	(
		(copyfiles	(source "..MU..:MuTools/MuLockLib")
				(dest #mutool-path)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuLockLib.readme")
					(dest #mutool-guide-path)
					(infos) (noposition)
			)
		)

		(if (= #mulib-proclibs 1)
			(set #need-mulocklib 0)	;MuLib based libraries: MuLockLib not required
			(set #need-mulocklib 1) ;..else yes...
		)

		(if (> #user-level 1)
			(set #need-mulocklib	(askbool	(prompt #ask-mulocklib)
								(help #ask-mulocklib-help)
								(default #need-mulocklib)
								(choices #yes #no)
						)
			)
		)

		(if (= #need-mulocklib 1)
			(startup "MuLockLib"		(prompt (cat #startup-mulocklib-go "MuLockLib >NIL:"))
							(help #ask-mulocklib-help)
							(command ("%s >NIL:" (tackon #mutool-path "MuLockLib")))
			)
		)
	)
)

;==Install MuLink==============================================================
(complete 85)

(if (<> (bitand #toolmask #mulink-mask) 0)
	(
		(copyfiles	(source	"..MU..:MuTools/MuLink")
				(dest #mutool-path)
		)
		(copyfiles	(source	"..MU..:MuTools/MuOVLYMGR")
				(dest #mutool-path)
		)

		(if (<> #mutool-guide-path "")
			(copyfiles	(source "..MU..:MuTools/MuLink.readme")
					(dest	#mutool-guide-path)
					(infos) (noposition)
			)
		)
	)
)

;==Install the MMU.guide=======================================================
(complete 87)

(if (<> (bitand #toolmask #mmuguide-mask) 0)
	(
		;copy the mmu.guide and patch in the right path.

		(if (<> #mutool-guide-path "")
			(
				(working #adapt-guide)
				(run ("..MU..:Install/SED FROM ..MU..:MMU.guide TO t:MMU.guide MATCH \"{#?}MuTools/{#?.guide/MAIN\\}#?}\" CHANGE \"{1}%s/{2}\"" #mutool-guide-path) (safe))
				(run ("C:Copy ..MU..:MMU.guide.info to t:MMU.guide.info") (safe))
				(copyfiles	(source	"t:MMU.guide")
						(dest	#mutool-guide-path)
						(infos) (noposition)
				)
				(run ("C:Delete t:MMU.guide t:MMU.guide.info QUIET") (safe))
			)	
		)
	)
)

;
; all MuTools are now "in".
;
;==Install FastIEEE============================================================
(complete 90)
;
; Check whether we may install FastIEEE. This is the case
; for i) MuLib processor libraries and ii) not Carsten's 40.17 68060.library
; and iii) the mathieee version is not 45 (because it has FastIEEE build-in)
;

(set #fastieee-possible 0)
(if (or (= (database "cpu") "68040") (= (database "cpu") "68060"))
        (set #fastieee-possible 1)
)


(if (= #mulib-proclibs 0)
	(set #fastieee-possible 0)
)

(set #lib060	(tackon #target "LIBS/68060.library"))
(set #libieee	(tackon #target "LIBS/mathieeedoubbas.library"))

(if (= #fastieee-possible 1)
	(if (= #cpu "68060")
		(if (exists #lib060)
			(if (= (+ (* 40 65536) 17) (getversion #lib060))
				(set #fastieee-possible 0) ;Carsten doesn't do the fpsp.resource
			)
		)
	)
)

(if (= #fastieee-possible 1)
	(if (exists #libieee)
		(if (<= 45 (/ (getversion #libieee) 65536))
			(set #fastieee-possible 0)	;no need to add FastIEEE in V45 or above
		)
	)
)
	

(if (> #user-level 1) 
	(set	#fastieee-possible	(askbool	(prompt #ask-fastieee)
							(help	#ask-fastieee-help)
							(default #fastieee-possible)
							(choices #yes #no)
					)
	)
)

(set #fastieee-dest (tackon #target "C"))

(if (and (= #fastieee-possible 1) (> #user-level 1))
	(set	#fastieee-dest		(askdir		(prompt #where-fastieee)
							(help #ask-fastieee-help)
							(default #fastieee-dest)
					)
	)
)

(if (= #fastieee-dest "")
	(set #fastieee-possible 0)
)

(if (= #fastieee-possible 1)
	(copyfiles			(source "..MU..:Install/FastIEEE")
					(dest #fastieee-dest)
					(files)
	)
)

(set #run-fastieee #fastieee-possible)

(if (= #fastieee-possible 1)
	(set	#run-fastieee		(askbool	(prompt #startup-fastieee)
							(help	#ask-fastieee-help)
							(default #run-fastieee)
							(choices #yes #no)
					)
	)
)

(set #fastieee-cmd (tackon #fastieee-dest "FastIEEE"))

(if (= #run-fastieee 1)
	(startup	"FastIEEE"	(prompt	(cat #startup-fastieee-go #fastieee-cmd))
					(help #ask-fastieee-help)
					(command #fastieee-cmd)
	)
)

;
;==Check for MMU patches=======================================================
(complete 94)
;
; Checks now for some (but not all) popular MMU hacks and disables them
;

(set #patch-check 1)
(if (> #user-level 1)
	(set #patch-check	(askbool	(prompt #ask-patch-check)
						(help #ask-patch-check-help)
						(default #patch-check)
						(choices #yes #no)
				)
	)
)

(if (= #patch-check 1)
	(

		(set #user-path (tackon #target "S/User-Startup"))

		(if (> #user-level 1)
			(set	#user-path	(askfile	(prompt	#where-user)
								(help #where-user-help)
								(default #user-path)
						)
			)
		)

		(if (= #user-path "")
			(set #user-path (tackon #target "S/User-Startup"))
		)

		(working #replace-quickrom)

		(set #rplc "(QuickROM|(Set|)CPU(|60|060)+[ \\t](|+[~;]+[ \\t])FastROM)")

		(if (<> (bitand #toolmask #mufastrom-mask) 0)
			(
				(set #dest (tackon #mutool-path "MuFastROM"))
				(set #dest (cat #dest " \\>NIL: PROTECT ON;"))
				(P_PATCH_REPLACE)
			)
			(
				(P_PATCH_REMOVE)
			)
		)

(complete 95)
		(working #replace-enforcer)

		(set #rplc "(Enforcer|CyberGuard)")

		(if (<> (bitand #toolmask #muforce-mask) 0)
			(
				(set #dest (tackon #mutool-path "MuForce"))
				(set #dest (cat #dest ";"))
				(P_PATCH_REPLACE)
			)
			(
				(P_PATCH_REMOVE)
			)
		)

(complete 96)
		(working #replace-memsniff)

		(set #rplc "(GuardianAngel|MemSniff|MungWall|CyberPatcher|OxyPatcher|WipeOut)")
		(P_PATCH_REMOVE)

		;this is done at MM4K installation already!
		;(working #replace-prepemul)

		;(set #rplc "PrepareEmul")
		;(P_PATCH_REMOVE)

(complete 97)
		(working #replace-setcachemode)

		(set #rplc "SetCacheMode")
		(if (<> (bitand #toolmask #musetcache-mask) 0)
			(
				(set #dest (tackon #mutool-path "MuSetCacheMode"))
				(set #dest (cat #dest ";"))
				(P_PATCH_REPLACE)
			)
			(
				(P_PATCH_REMOVE)
			)
		)

(complete 98)
		(working #replace-speedychip)

		(set #rplc "SpeedyChip")
		(if (<> (bitand #toolmask #mufastchip-mask) 0)
			(
				(set #dest (tackon #mutool-path "MuFastChip"))
				(set #dest (cat #dest " ON \\>NIL:;"))
				(P_PATCH_REPLACE)
			)
			(
				(P_PATCH_REMOVE)
			)
		)

(complete 99)
		(working #replace-fastexec)

		(set #rplc "(FastExec|SetMemMode|SetFastAvec)")
		(P_PATCH_REMOVE)

	)
)

(complete 100)

;==we're done==================================================================
;
(TERMINATE)
(exit #exit-text)
