; 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.")
)
(if (exists "inet:libs/inet.library" (noreq))
        (if (< (/ (getversion "inet:libs/inet.library") 65536) 5.0)
                (abort
                        "If using AS225, you must be using inet.library 5.0"
                        "or higher to install 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 SYS:")
(run "envoyinstall:guessenvoydev >env:envoydev" (safe))
(set @default-dest (getenv "envoydev"))

(set envoy_dest
        (tackon
                (askdir
                        (prompt
                                "\nIn which disk (or drawer) should the Envoy "
                                "drawer be installed (or updated)?"
                        )
                        (help @askdir-help)
                        (default @default-dest)
                )
                "Envoy"
        )
)

(set is_update 0)
(if (exists envoy_dest)
        (if (askbool
                (prompt
                        "\nDo you wish to update your current installation? "
                )
                (help
                        "\nThe installer has determined that you may already "
                        "have a copy of Envoy correctly installed on your "
                        "system.  If this is incorrect or if you want to "
                        "update from scratch select 'Re-Install' as an answer.  "
                        "Otherwise, select 'Update.'"
                )
                (choices "Update" "Re-Install")
                (default 1)
            )
        (set is_update 1)       ; if user wants in same place
        )
)

;
;
;  Now that we know where it goes and whether we are overwriting it:
;
(if (NOT @pretend)
        (set @default-dest envoy_dest)
)
;
;  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 "libs:")
           (all)
)

(copyfiles (prompt "Copy Envoy Configuration Editors.")
           (source "EnvoyInstall:Configuration")
           (dest (tackon envoy_dest "Configuration"))
           (all)
           (infos)
           (help
                   "\nConfiguration editors for NIPC, Services, EFS"
           )
)

; Now comes the 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.

(copylib (prompt "Updating C: commands.")
	 (source "EnvoyInstall:c/mount")
	 (dest "C:")
)

(copylib
	(source "EnvoyInstall:C/protect")
	(dest "C:")
)

(copylib
	(source "EnvoyInstall:C/list")
	(dest "C:")
)

(copyfiles
	(source "EnvoyInstall:C/owner")
	(dest "C:")
)

(copyfiles
	(source "EnvoyInstall:C/group")
	(dest "C:")
)

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


(if 	(askbool
            (prompt
                    "\nWould you like the Envoy server programs installed?"
            )
            (help
                    "\nIf you are planning on providing services to other machines, "
                    "such as exporting a filesystem or providing a print service,"
                    "select Yes.  Otherwise, select No."
            )
        )
        (copyfiles
                (prompt "Copy Envoy services.")
                (source "EnvoyInstall:Services")
                (dest (tackon envoy_dest "Services"))
                (all)
                (infos)
                (optional)
        )
)
;
;   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 "devs:Networks"))
	(makedir "devs:Networks")
)

(copylib
        (source "EnvoyInstall:DEVS/envoyprint.device")
        (dest "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
;
(if 	(askbool
                (prompt
                        "\nRun the Services Manager and Security Manager from your s:user-startup?"
                )
                (help
                        "\nIf you intend to export nearly anything, you should select YES."
                )
        )
        (set manager ("Run >NIL: \"%s/services/Services Manager\"\n" envoy_dest))
)
(startup "Envoy"
	(prompt "About to add strings to your user-startup.\nIs this okay?")
        (command
                ("PATH \"%s%s\" \"%s%s\" ADD\n%s%s\n%s%s"
                                            envoy_dest "/Config"
                                            envoy_dest "/Services"
                                             manager
                                             authserv
                )
        )
        (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
                                	(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	"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   "DEVS:Networks")
                                        )
	                                (set IPType 240)
	                                (set ARPType 241)
	                        )
                        )
                )
                (set HostInfo
                        (cat    "HOSTINFO "
                                HostName
                        )
                )
                (set DevInfo
                        (cat    "DEVINFO "
                                DeviceName
                                " "
                                HostNumber
                                " "
                                ("%ld " IPType)
                                ("%ld " 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	"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   "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 ENVARC:Envoy/NIPC.prefs")
                (run "EnvoyInstall:C/BuildNIPCPrefs t:nipcconfig.txt ENV:Envoy/NIPC.prefs")
        )
)

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