; VARIABLES:
;       envoy_dest   - where to copy it all to.  We default to inet:, if assigned
;                 (but only if assigned, not if a device has that name.)
;

;
;  Make sure that we are running under 2.04 or better
;
(if (< (/ (getversion) 65536) 37)
        (abort "You must be using Kickstart 2.04 or higher to install and use Envoy.")
)
; Allow updating over SoftEng's big Envoy server

(set serverMode FALSE)
(if 	(= (expandpath (pathonly @icon)) "ProDrive:appn/install/disk")
	(
            (set serverMode TRUE)
            (makeassign "EnvoyInstall" "ProDrive:appn/install/disk")
	)
)

(if 	(= (expandpath (pathonly @icon)) "SoftPublic:NewEnvoy")
	(
            (set serverMode TRUE)
            (makeassign "EnvoyInstall" "SoftPublic:NewEnvoy")
	)
)

;
;  Figure out where to put it if installing to a hard disk.
;
;(run "setenv envoydev TST:")
;(run "envoyinstall:guessenvoydev >env:envoydev" (safe))
(set envoy_dest "TST:Envoy")

(if (NOT (exists envoy_dest))
	(makedir envoy_dest (infos))
)
(set is_update 0)
;
;
;  Now that we know where it goes and whether we are overwriting it:
;
(if (NOT @pretend)
        (set @default-dest envoy_dest)
)

;
; Figure out what workbench version they are using.
;
(set WBVersion (/ (getversion "TST:Libs/version.library") 65536))

;
; Ick. V37.  We have to delete a whole bunch of stuff to make
; room for everything.
;
(if 	(= WBVersion 37)
	(
		(delete	"TST:Libs/translator.library")
		(delete "TST:Devs/narrator.device")
		(delete "TST:System/DiskCopy")
		(delete "TST:System/DiskCopy.info")
		(delete "TST:System/Format")
		(delete "TST:System/Format.info")
		(delete "TST:System/NoFastMem")
		(delete "TST:System/NoFastMem.info")
		(delete "TST:System/FixFonts")
		(delete "TST:System/FixFonts.info")
		(delete "TST:Utilities/Clock")
		(delete "TST:Utilities/Clock.info")
		(delete "TST:Utilities/Display")
		(delete "TST:Utilities/Display.info")
		(delete "TST:Utilities/Say")
		(delete "TST:Utilities/Say.info")
	)
)
;
; If V38, then we don't have to delete anything.


; Ick. V39.  We have to delete a whole bunch of stuff to make
; room for everything.
;
(if 	(= WBVersion 39)
	(
		(delete "TST:System/DiskCopy")
		(delete "TST:System/DiskCopy.info")
		(delete "TST:System/Format")
		(delete "TST:System/Format.info")
		(delete "TST:System/NoFastMem")
		(delete "TST:System/NoFastMem.info")
		(delete "TST:System/FixFonts")
		(delete "TST:System/FixFonts.info")
		(delete "TST:Utilities/Clock")
		(delete "TST:Utilities/Clock.info")
		(delete "TST:Devs/mfm.device")
		(delete "TST:C/Edit")
		(delete "TST:L/CrossDOSFileSystem")
	)
)

;
;

;
;  do the main copying.  Because pattern and all are mutually exclusive,
;  I have to copy each freaking directory separately!!!  Copylib is used
;  for each library individually so that no smaller version is overwritten
;  by a higher version.  Last time I checked though, this feature was broken.
;
(copyfiles (source "EnvoyInstall:libs")
           (dest "TST:libs")
           (all)
)

(if (NOT (exists (tackon envoy_dest "Configuration")))
	(makedir (tackon envoy_dest "Configuration") (infos))
)

(set n 0)
(while	(< n 4)
	(
                (copylib (prompt "Copy Envoy Configuration Editors.")
                         (source (select n       "EnvoyInstall:Configuration/Filesystem Imports"
                                                 "EnvoyInstall:Configuration/Network Printing"
                                                 "EnvoyInstall:Configuration/NIPC Configuration"
                                                 "EnvoyInstall:Configuration/Printer Import"
                                                 "EnvoyInstall:Configuration/Users"
                                                 "EnvoyInstall:Configuration/Groups"
                                 )
                         )
                         (dest  (tackon envoy_dest "Configuration"))
                         (infos)
                )
                (set n (+ n 1))
        )
)

; Now comes somthing really fun. Depending on which version of the
; OS the user has, we may or may not need to update some of the commands
; in the C: directory.

(set n 0)
(while	(< n 5)
	(
                (copylib (prompt "Updating C: commands.")
                         (source (select n       "EnvoyInstall:c/mount"
                                                 "EnvoyInstall:c/protect"
                                                 "EnvoyInstall:c/list"
                                                 "EnvoyInstall:c/owner"
                                                 "EnvoyInstall:c/group"
                                 )
                         )
                         (dest  "TST:C")
                )
                (set n (+ n 1))
        )
)

(copylib
	(prompt "Installing the Envoy Network File System.")
        (source "EnvoyInstall:L/EnvoyFileSystem")
        (dest "TST:L")
)

;   Need to delete old drivers located in devs: rather than devs:Networks.
;   Need to ask which SANA-II drivers to install
;   Note that this also copies mount examples to devs:DOSDrivers
;

(if (NOT (exists "TST:Devs/Networks"))
	(makedir "TST:Devs/Networks")
)

(copylib
        (source "EnvoyInstall:DEVS/envoyprint.device")
        (dest "TST:Devs/")
)

;
;  We're all done if this was just an update!
;
(if is_update (exit))
;
;  Get configuration information from the user:
;

;  Let's configure what the user will do at boot time
;
(startup "Envoy"
	(prompt "About to add strings to your user-startup.\nIs this okay?")
        (command
                ("PATH \"%s%s\" ADD"
                                            envoy_dest "/Config"
                )
        )
        (help
                "\nThese add CLI paths to Envoy directories."
        )
)

(set NetworkType
	(askchoice
		(prompt "Please Select what type of network configuration you are using." )
		(choices "Simple Network"
			 "Internet"
		)
		(help
			"\nFill in."
		)
	)
)

;  No Internet:
;
;  Hostname
;  Address (1-254)
;  SANA-II Device  (no need to support multiple devices if not internet)
;               (don't need subnet mask)

(if (= NetworkType 0)
	(
		(set HostNumber "137.239.1.")

                (set HostName
                        (askstring
                                (prompt "Please Enter a name for your Amiga.")
                                (default "Amiga")
                                (help
                                        "\nEach machine on a network must have a unique "
                                        "name and number.  Machine names are limited to "
                                        "31 characters or less, and may not contain a ':' "
                                        "character. "
                                )
                        )
                )
                (set HostNumber
                	(cat	HostNumber
                                (asknumber
                                        (prompt "Please enter a network address for your Amiga.")
                                        (default "1")
                                        (help
                                                "\nEach machine on a network must have a unique "
                                                "name and number.  Machine numbers must be in the "
                                                "range of 1 to 254."
                                        )
                                        (range 1 254)
                                )
                        )
                )
                (set DeviceDriver
                        (askchoice
                                (prompt "Please Select what type of Network Interface you will be using")
                                (choices        "Commodore Ethernet Card (A2065)"
                                                "Commodore Arcnet Card   (A2060)"
                                                "Other"
                                )
                                (default 0)
                                (help
                                        "\n Fill in!"
                                )
                        )
                )
                (if (= DeviceDriver 2)
                	(
                                (message
                                       "\nIf you will be using a SANA-II device which has not yet been "
                                       "copied to DEVS:Networks/ yet, copy it there now manually.  Come back to "
                                       "installer when you are done."
                                )
                                (set DeviceName
                                        (askfile
                                        	(default "DEVS:Networks")
                                                (prompt	"Please select the SANA-II driver for your network interface. ")
                                                (help
                                                        "\nFill in!"
                                                )
                                        )
                                )
                                (set IPType
                                	dir a(asknumber
                                		(default 2048)
                                		(prompt "Please enter the packet type number for IP packets "
                                			"for your Network Interface."
                                		)
                                		(help
                                			"\nFill In!"
                                		)
                                	)
                                )
                                (set ARPType
                                	(asknumber
                                		(default 2054)
                                		(prompt "Please enter the packet type number for ARP packets "
                                			"for your Network Interface."
                                		)
                                		(help
                                			"\Fill In!"
                                		)
                                	)
                                )
                        )
                )

                (if (= DeviceDriver 0)
                	(
                	        (set DeviceName "DEVS:Networks/a2065.device")
                	        (copylib
                	        	(source "EnvoyInstall:DEVS/Networks/a2065.device")
                	        	(dest	"TST:Devs/Networks")
                	        )
                	        (set IPType 2048)
                	        (set ARPType 2054)
                	)
                        (if (= DeviceDriver 1)
                        	(
	                                (set DeviceName "DEVS:Networks/a2060.device")
                                        (copylib
                                                (source "EnvoyInstall:DEVS/Networks/a2060.device")
                                                (dest   "TST:Devs/Networks")
                                        )
	                                (set IPType 240)
	                                (set ARPType 241)
	                        )
                        )
                )
                (set HostInfo
                        (cat    "HOSTINFO "
                                HostName
                        )
                )
                (set DevInfo
                        (cat    "\nDEVINFO "
                                DeviceName
                                " "
                                HostNumber
                                " "
                                ("%ld " IPType)
                                ("%ld\n" ARPType)
                        )
                )
                (textfile
                	(dest	"T:nipcconfig.txt")
                        (append HostInfo)
                        (append DevInfo)
                )
                (run "EnvoyInstall:C/BuildNIPCPrefs t:nipcconfig.txt ENVARC:Envoy/NIPC.prefs")
                (run "EnvoyInstall:C/BuildNIPCPrefs t:nipcconfig.txt ENV:Envoy/NIPC.prefs")
	)

;  Internet:
;
;  Hostname
;  Address (full IP number)
;  SANA-II Devices (with all the fixin's)
;  Realm name
;  Realm Server
;  Realms
;  Gateway config
;
        (
		(set HostNumber "137.239.1.")

                (set HostName
                        (askstring
                                (prompt "Please Enter a name for your Amiga.")
                                (default "Amiga")
                                (help
                                        "\nEach machine on a network must have a unique "
                                        "name and number.  Machine names are limited to"
                                        "31 characters or less, and may not contain a ':' "
                                        "character. "
                                )
                        )
                )
                (set HostIP
                        (askstring
                                (prompt "Please enter a network address for your Amiga.")
                                (default "137.239.1.1")
                                (help
                                        "\nEach machine on a network must have a unique "
                                        "network address."
                                )
                        )
                )
                (if 	(askbool
                                (prompt "Is your network using the a default subnet mask?")
                                (default 0)
                                (help
                                        "\nSubnet masks are used to break a large network into smaller"
                                        "networks.  If you are unsure of whether or not subnet masks"
                                        "are in use, please contact your System Administrator."
                                )
                	)
                	(set SubnetMask "none")
                        (set SubnetMask
                                (askstring
                                        (prompt "Please enter the subnet mask being used on your network.")
                                        (default "255.255.255.0")
                                        (help
                                        	"\nFill In."
                                        )
                                )
                        )
                )
                (if	(askbool
                		(prompt "Would you like to select the network address for a "
                			"default router?"
                		)
                		(default 1)
                		(help
                			"\nRouters are machines that transfer packets from one network "
                			"to another.  Usually each physical network has one router that "
                			"is designated as the 'default' router.  If you are not sure "
                			"if you have a default router, please contact your System "
                			"Administrator."
                		)
                	)
                	(set DefaultRouter
                		(askstring
                			(prompt "Please enter the network address of the default router.")
                			(help
                				"\nFill In."
                			)
                		)
                	)
                	(set DefaultRouter "none")

                )
                (set RealmName
                	(askstring
                		(prompt "Please Enter the name of the Realm that your Amiga is in.")
                		(default "Realm1")
                		(help
                			"\nAn Envoy realm is a logical grouping of physical networks. "
                			"Realms are typically used to organize large numbers of "
                			"machines into reasonable sized groups."
                		)
                	)
                )
                (set RealmIP
                	(askstring
                		(prompt "Please enter the network address of your Realm Server.")
                		(default "137.239.1.254")
                		(help
                			"\nFill In."
                		)
                	)
                )
                (set DeviceDriver
                        (askchoice
                                (prompt "Please Select what type of Network Interface you will be using")
                                (choices        "Commodore Ethernet Card (A2065)"
                                                "Commodore Arcnet Card   (A2060)"
                                                "Other"
                                )
                                (default 0)
                                (help
                                        "\n Fill in!"
                                )
                        )
                )
                (if (= DeviceDriver 2)
                	(
                                (set DeviceName
                                        (askfile
                                        	(default "DEVS:Networks")
                                                (prompt "Please select the SANA-II driver for your network interface.")
                                                (help
                                                        "\nFill in!"
                                                )
                                        )
                                )
                                (set IPType
                                	(asknumber
                                		(default 2048)
                                		(prompt "Please enter the packet type number for IP packets"
                                			"for your Network Interface."
                                		)
                                		(help
                                			"\nFill In!"
                                		)
                                	)
                                )
                                (set ARPType
                                	(asknumber
                                		(default 2054)
                                		(prompt "Please enter the packet type number for ARP packets"
                                			"for your Network Interface."
                                		)
                                		(help
                                			"\Fill In!"
                                		)
                                	)
                                )
                        )
                )
                (if (= DeviceDriver 0)
                	(
                	        (set DeviceName "DEVS:Networks/a2065.device")
                	        (copylib
                	        	(source "EnvoyInstall:DEVS/Networks/a2065.device")
                	        	(dest	"TST:Devs/Networks")
                	        )
                	        (set IPType 2048)
                	        (set ARPType 2054)
                	)
                        (if (= DeviceDriver 1)
                        	(
	                                (set DeviceName "DEVS:Networks/a2060.device")
                                        (copylib
                                                (source "EnvoyInstall:DEVS/Networks/a2065.device")
                                                (dest   "TST:Devs/Networks")
                                        )
	                                (set IPType 240)
	                                (set ARPType 241)
	                        )
                        )
                )
                (set HostInfo
                        (cat    "HOSTINFO "
                                HostName
                                " REALMNAME "
                                RealmName
                                " REALMIP "
                                RealmIP
                                "\n"
                        )
                )
                (set DevInfo
                        (cat    "DEVINFO "
                                DeviceName
                                " "
                                HostIP
                                " "
                                ("%ld " IPType)
                                ("%ld " ARPType)

                        )
                )
                (if (<> "none" SubnetMask)
                	(set DevInfo
                		(cat	DevInfo
                			" SUBMASK "
                			SubnetMask
                		)
                	)
                )
                (set DevInfo
                	(cat	Devinfo
                		"\n"
                	)
                )
                (textfile
                	(dest	"T:nipcconfig.txt")
                        (append HostInfo)
                        (append DevInfo)
                        (if (<> "none" DefaultRouter)
                        	(append
                        		(cat	"ROUTEINFO "
                        			"0.0.0.0 "
                        			DefaultRouter
                        			" 1\n"
                        		)
                        	)
                        )
                )
                (run "EnvoyInstall:C/BuildNIPCPrefs t:nipcconfig.txt TST:NIPC.prefs")
                (run "EnvoyInstall:C/BuildNIPCPrefs t:nipcconfig.txt TST:NIPC.prefs")
        )
)

;(run ("%s/Config/NIPC Configuration" envoy_dest) )
