; VARIABLES:
;  is_update    - means that a previous install is being written over.  Do not
;                 mess with user-editables.
;       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)) "SoftServe:Public/NewEnvoy")
(
    (set serverMode TRUE)
    (makeassign "EnvoyInstall" "SoftServe:Public/NewEnvoy")
))

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


;
;
(welcome)
(set old_level @user-level)
(message
        "\nEnvoy installation BETA v1.0.\n"
        "The entire contents of this disk are\n"
        "CONFIDENTIAL\n"
        "DO NOT DISTRIBUTE.\n\n"
)
;
;  Figure out where to put it
;

(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"
        )
)
;
;  See if this is really an update
;
(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
        )
)
;
;
;  Now that we know where it goes and whether we are overwriting it:
;
(if (NOT is_update)
        (makedir envoy_dest (infos))
)
(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")
           (all)
           (infos)
           (dest (tackon envoy_dest "Configuration"))
)

; Accounts stuff
(copyfiles (prompt "Copying User Accounts files")
           (source "EnvoyInstall:Accounts/Accounts Manager")
           (dest (tackon envoy_dest "Accounts"))
)

(if (NOT (exists (tackon envoy_dest "Accounts/UserData") (noreq)))
    (copyfiles (prompt "Copying User and Group database")
               (source "EnvoyInstall:Accounts/UserData")
               (dest (tackon envoy_dest "Accounts"))
    )
)

(if (NOT (exists (tackon envoy_dest "Accounts/GroupData") (noreq)))
    (copyfiles (prompt "Copying User and Group database")
               (source "EnvoyInstall:Accounts/GroupData")
               (dest (tackon envoy_dest "Accounts"))
    )
)

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

(copyfiles (prompt "Updating C: commands.")
           (source "EnvoyInstall:c")
           (all)
           (dest "c:")
)

(copyfiles (prompt "Envoy 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")
                (all)
                (infos)
                (dest (tackon envoy_dest "Services"))
        )
)

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

(copyfiles
        (prompt "Copy Device Drivers")
        (source "EnvoyInstall:devs")
        (all)
        (infos)
        (dest "devs:")
        (help
                "\nNetworking device drivers for SANA-II hardware and"
                "networking printing support."
        )
)

;(if (exists (tackon envoy_dest "Config"))
;(
;    (foreach (tackon envoy_dest "Config") "#?"
;        (delete (tackon (tackon envoy_dest "Config") @each-name))
;        (delete (cat (tackon envoy_dest @each-name) ".info"))
;    )
;))
;
;(if (exists "envarc:envoy/efs.config")
;(
;    (delete ("envarc:envoy/efs.config"))
;    (delete ("env:envoy/efs.config"))
;))
;
;(if (exists (tackon envoy_dest "Services/Manager"))
;(
;    (delete (tackon envoy_dest "Services/Manager"))
;))

;
;  We're all done if this was just an update!
;

(if is_update (exit))

;
;  Get configuration information from the user:
;
;(message
;       "\nIf you will be using a SANA-II device which has not yet been "
;       "copied to devs: yet, copy it there now manually.  Come back to "
;       "installer when you are done."
;)
;
;  This is really just temporary.  We want to ask the user a series of questions
;  rather than forcing him to use NIPCConfig and guess which fields are relevant
;  to his network.
;

(makedir "Env:Envoy")
(makedir "EnvArc:Envoy")
(if (NOT (exists "Env:Envoy"))
        ((copyfiles
                (prompt "Copy Envoy 'default' configuration.")
                (source "EnvoyInstall:Env")
                (dest "EnvArc:")
                (all)
                (help
                        "\nThe default configuration for NIPC, Services, EFS, etc.\n"
                )
        )
        (copyfiles
                (prompt "Copy Envoy 'default' configuration.")
                (source "EnvoyInstall:Env")
                (dest "Env:")
                (all)
                (help
                        "\nfill"
                )
        ))
)

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


;  Internet:
;
;  Hostname
;  Address (full IP number)
;  SANA-II Devices (with all the fixin's)
;  Realm name
;  Realm Server
;  Realms
;  Gateway config
;
;  Let's configure what the user will do at boot time
;
(if (askbool
        (prompt
                "\nRun the Services Manager and Accounts Manager from your s:user-startup?"
        )
        (help
                "\nIf you intend to export anything, you should select YES."
        ))
        ((set manager ("Run >NIL: \"%s/services/Services Manager\"\n" envoy_dest))
        (set accounts ("Run >NIL: \"%s/Accounts/Accounts 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 "/Configuration"
                                            envoy_dest "/Services"
                                            accounts
                                            manager
                )
        )
        (help
                "\nThese add CLI paths to Envoy directories."
        )
)


