; $VER: FACTS4Install 1.7 (03.06.02)

(set #inst-ver (/ @installer-version 65536))
(if (< #inst-ver 43)
    (abort "You are running an old version of Installer.\n\n"
            "Please update to at least version 43.3, available on Aminet.")
)

(welcome)

(set #ver-verstr (getversion "C:Version"))
(set #ver-ver (/ #ver-verstr 65536))

;(if (< #ver-ver 44)
;    (
;(message "The FACTS installation will start by updating your Version command.\n\n"
;            "Please read the following text before proceeding.\n\n"
;            "Note that if v40.3 if already installed, you will not be prompted to replace it..." (all))
;
;        (run "SYS:Utilities/Multiview Version_v40.3/Version_ReadMe")
;
;    (copylib
;        (prompt "If you have a version command older than v40.3, "
;                   "you may experience problems with dates in year 2000 and above.")
;        (help "Dates in version strings display incorrectly on WB 3.0 and 3.1 versions "
;                "of the Version command - please install this update.\n\n" @copylib-help)
;        (source "Version_v40.3/Version")
;        (dest "C:")
;        (confirm "expert")
;    )
;
;    )
;)

(set @default-dest
    (askdir
        (prompt "Where would you like to install FACTS?")
        (help "It is recommended that FACTS be running all the time, so "
        "WBStartUp is the logical place.\n\n" @askdir-help)
        (default "SYS:WBStartUp")
    )
)

(startup "RexxMast"
    (prompt "FACTS REQUIRES ARexx to be active when it starts up.  "
    "To do this, the RexxMast command should be executed in your "
    "user-startup script.\n\nDoes this need adding? (if you are not "
    "sure, it is safe to proceed with this anyway)")
    (help @startup-help)
    (command "RexxMast >NIL:")
    (confirm "expert")
)

(set #already-installed (exists (tackon @default-dest "Facts.info")))
(set #timeserver-var (exists "ENVARC:TIMESERVER"))

(if #already-installed
    (
        (set #facts-vernum (getversion (tackon @default-dest "Facts")))
        (set #facts-ver (/ #facts-vernum 65536))
        (copylib
            (prompt "Please check the version of FACTS you are copying against "
            "any which might already be installed.")
            (help @copylib-help)
            (source "Facts")
            (dest @default-dest)
            (optional "askuser" "force" "oknodelete")
        )

        (if (< #facts-ver 3)
            (
                (iconinfo
                    (prompt "Updating TIMESERVER config file...")
                    (help @iconinfo-help)
                    (dest (tackon @default-dest "Facts"))
                    (gettooltype "SERVICE" "#service")
                    (gettooltype "TIMEOUT" "#timeout")
                    (safe)
                )
                (set #timeserver (getenv "ENV:TIMESERVER"))
                (set #extra-config ("%s:%s:%s" #timeserver #service #timeout))
                (textfile
                    (prompt "You appear to be updating from a pre-3.0 version of FACTS.\n"
                                "Do you want to update your TIMESERVER variable to the newer format?\n\n"
                                "NB: AutoDST settings have also changed - please check and update this manually "
                                "if necessary")
                    (help "Although FACTS 3 will read FACTS 2.x TIMESERVER variables, "
                            "this update procedure will ensure that all your previous prefs are kept "
                            "intact.\n\n" @textfile-help)
                    (dest "ENV:TIMESERVER")
                    (append #extra-config)
                    (confirm)
                )
                (copyfiles
                    (prompt "Copying ENV: to ENVARC:...")
                    (help @copyfiles-help)
                    (source "ENV:TIMESERVER")
                    (dest "ENVARC:")
                )
            )
        )

        (if (exists "ENVARC:TIMESERVER")
            (
                (copyfiles
                    (prompt "Moving old timeserver variable...")
                    (help @copyfiles-help)
                    (source "ENVARC:TIMESERVER")
                    (dest "ENVARC:FACTS")
                    (newname "TIMESERVER.0")
                )
                (delete "ENVARC:TIMESERVER")
            )
        )
        (if (exists "ENV:TIMESERVER")
            (
                (copyfiles
                    (prompt "Moving old timeserver variable...")
                    (help @copyfiles-help)
                    (source "ENV:TIMESERVER")
                    (dest "ENV:FACTS")
                    (newname "TIMESERVER.0")
                )
                (delete "ENV:TIMESERVER")
            )
        )
    )

    (
        (copylib
            (prompt "Please check the version of FACTS you are copying against "
            "any which might already be installed.")
            (help @copylib-help)
            (source "Facts")
            (dest @default-dest)
            (infos)
        )

        (tooltype
            (prompt "Do you want FACTS to run silently in the background?\n\n"
            "If you would prefer it to show the GUI on startup, please skip "
            "this part.")
            (help @tooltype-help)
            (dest (tackon @default-dest "Facts"))
            (settooltype "CXPOPUP" "NO")
            (confirm "expert")
        )

    )
)

        (copylib
            (prompt "Please check the version of SLEFF you are copying against "
            "any which might already be installed.")
            (help "SLEFF is the Server List Editor for FACTS\n\n" @copylib-help)
            (source "SLEFF")
            (dest "SYS:Prefs")
            (infos)
            (confirm)
        )

(set #doc-dir
    (copyfiles
        (prompt "Do you want to copy the documentation?")
        (help @copyfiles-help)
        (source "Docs")
        (dest "HELP:")
        (optional "askuser" "force")
        (infos)
        (all)
        (confirm "expert")
    )
)

(if (exists "libs:locale.library")
    (copyfiles
        (prompt "Select languages you wish to install (English is built in)")
        (help "FACTS supports locale.library!\n\n" @copyfiles-help)
        (source "catalogs")
        (dest "LOCALE:catalogs")
        (all)
        (confirm "expert")
    )
)

(copyfiles
    (prompt "Do you want to install the example ARexx scripts in REXX:?\n\n"
    "See Facts docs for more details")
    (help @copyfiles-help)
    (source "Rexx")
    (dest "REXX:")
    (optional "askuser" "force")
    (confirm "expert")
    (all)
)

(working "Starting ARexx...")

(run "RexxMast >NIL:")

(set #rexx-string ("install.rexx %s" (tackon @default-dest "Facts")))

(rexx #rexx-string
    (prompt "I will now quit any old versions of FACTS, and "
    "load the new one")
    (help "Does what it says!  Needs ARexx to be running.")
)

(run ("WBRun" (tackon @default-dest "Facts")))

(set #time (getenv "T:TIME"))

(if (> #inst-ver 43)
    (
;    (openwbobject @default-dest (safe))
;    (showwbobject (tackon @default-dest "Facts"))
    (openwbobject #doc-dir)
    (showwbobject (tackon #doc-dir "Facts.guide"))
    )
)

(exit "Finished!\n\n\nThe current time is\n\n" #time
"\n\n\nFACTS has been started, please ensure your timezone is set "
"correctly and then save config." (quiet))

