; 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."
                )
        )
)

;
;  Figure out where to put it if installing to a hard disk.
;
(run "setenv envoydev SYS:" (safe))
(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 'No' as an answer.  "
                        "Otherwise, select 'Yes.'"
                )
                (choices "Update" "Re-Install")
                (default 1)
            )
        (set is_update 1)       ; if user wants in same place
        )
)

(if (NOT (exists envoy_dest))
	(makedir envoy_dest
		(infos)
	)
)

;
;
;  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 (NOT (exists "env:Envoy"))
	(makedir "env:Envoy")
)

(if (NOT (exists "envarc:Envoy"))
	(makedir "envarc:Envoy")
)

(if (NOT (exists "devs:Networks"))
	(makedir "devs:Networks")
)

(set Servers 0)
(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."
            )
        )
        (
            (set Servers 1)
            (copyfiles
                    (prompt "Copy Envoy services.")
                    (source "EnvoyInstall:Services")
                    (dest (tackon envoy_dest "Services"))
                    (all)
                    (infos)
                    (optional)
            )
            (copyfiles
            	    (source "EnvoyInstall:Accounts")
            	    (dest (tackon envoy_dest "Accounts"))
            	    (all)
            	    (infos)
            )
            (tooltype
                    (dest (tackon envoy_dest "Services/Services Manager"))
                    (settooltype "DONOTWAIT" "TRUE")
            )
            (tooltype
                    (dest (tackon envoy_dest "Accounts/Accounts Manager"))
                    (settooltype "UserDB" (tackon envoy_dest "Accounts/UserData"))
                    (settooltype "GroupDB" (tackon envoy_dest "Accounts/GroupData"))
                    (settooltype "DONOTWAIT" "TRUE")
            )
            (set svcs	(askoptions
            			(prompt		"Please Select which Envoy Services that you would like "
            					"to be activated when Services Manager is run. "
            			)
            			(choices	"Printer"
            					"File System"
            			)
            			(default	-1)
            			(help		"fill in.")
            		)
            )
            (textfile
                    (dest   "T:servicesconfig.txt")
                    (if (in svcs 0)
                    	(append (cat	"SERVICE PrintSpooler "
                    			(tackon envoy_dest "Services/printspool.service")
                    			"\n"
                    		)
                    	)
                    )
                    (if (in svcs 1)
                    	(append	(cat	"SERVICE Filesystem "
                    			(tackon	envoy_dest "Services/filesystem.service")
                    			"\n"
                    		)
                    	)
                    )

            )
            (run "EnvoyInstall:C/BuildSMPrefs t:servicesconfig.txt ENVARC:Envoy/Services.prefs")
            (run "EnvoyInstall:C/BuildSMPrefs t:servicesconfig.txt ENV:Envoy/Services.prefs")
            (set UserDBPath (tackon envoy_dest "Accounts/UserData"))
            (set GroupDBPath (tackon envoy_dest "Accounts/GroupData"))
            (if     (askbool
                        (prompt
                                "\nRun the Services Manager and Accounts Manager at startup time?"
                        )
                        (help
                                "\nIf you intend to export anything, you should select YES."
                        )
                    )
                    (
                            (copyfiles
                                    (source (tackon envoy_dest "Services/Services Manager"))
                                    (dest   "SYS:WBStartup")
                                    (infos)
                            )
                            (copyfiles
                                    (source (tackon envoy_dest "Accounts/Accounts Manager"))
                                    (dest   "SYS:WBStartup")
                                    (infos)
                            )
                    )
	    )
        )
)
;
;   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
;

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

(if is_update (exit))

(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
                                "\n"
                        )
                )
                (set DevInfo
                        (cat    "DEVINFO "
                                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	"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
                                " 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")
        )
)
(if (= Servers 1)
	(
            (run ("\"%s\" %s %s \"%s\" %s \"%s\" " (tackon envoy_dest "Accounts/Accounts Manager")
            			      "InitDB"
            			      "UserDB"
            			      UserDBPath
            			      "GroupDB"
            			      GroupDBPath))

	)
)