; $VER: KMS 1.75b
; Copyright 1993 Thomas Schwarz - All Rights Reserved

(complete 0)

(onerror
   (if
      (> @ioerr 0)
      (message
         "Error code: " @ioerr "\n"
         "\n"
         "Some error has occurred. Please inform the author "
         "of KMS by email to blacky@bmagic.incubus.sub.org\n"
         "\n"
         "The following information is required: error code "
         "(above), last error message, the percentage done "
         "(window title) and your log file. Thank you!"
      )
   )
)

(set @default-dest "SYS:")

; **************************************************************************

(if
   (< (/ (getversion) 65536) 37)
   (
   (message "Sorry, this package requires OS v2.04 or better.")
   (exit (quiet))
   )
)

; **************************************************************************

(if
   (exists "KMS:" (noreq))
   (set update 
      (askchoice
         (prompt "KMS: assign detected...")
         (help
            "If you decide for updating your old installation "
            "you won't be asked for directories. Directories "
            "of your previous installation will be used. You "
            "will get the opportunity to make a backup of "
            "your current KMS installation in this case, too.\n"
            "\n"
            "If you decide for removing your current KMS "
            "installation this script will try to delete "
            "files introduced by a former KMS installation. "
            "However, this utility is not able to remove all "
            "files related to KMS. For example it won't "
            "remove the AXAUX: related files because they "
            "might be used by other applications, too. It "
            "won't remove the KMS related entries in your "
            "ums.config either because it isn't able to do so.\n"
            "\n"
            "If you ever run into troubles related to this "
            "install script (which certainly is not perfect), "
            "remove your outdated KMS copy (use the 'Deinstall' "
            "option) before attempting a completely new installation."
         )
         (default 1)
         (choices
            "Install KMS from scratch"
            "Update existing KMS installation"
            "Deinstall KMS"
         )
      )
   )
   (set update 0)
)

(complete 1)

; **************************************************************************
; update = 0 : Neuinstallation
; **************************************************************************

(if
   (= update 0)
   (
   (set kmsdest
      (askdir
         (prompt "Please select the drawer you want KMS to be installed in. A new drawer \"KMS\" will be created therein.")
         (default @default-dest)
         (help @askdir-help)
      )
   )

   (set kmsdest (tackon kmsdest "KMS"))
   (makedir kmsdest (infos))
   (set @default-dest kmsdest)
   (makeassign "KMS" kmsdest)

   (set targetsize (getdiskspace kmsdest))
   (if
      (< targetsize 800000)
      (if
         (<> (getdevice kmsdest) "RAM")
         (abort
            "This installation requires about 800 KB free "
            "on the volume where you wish to install KMS. "
            "Please make this space available and then try again."
         )
      )
   )

   (complete 5)

   (copyfiles
      (prompt "Copying Binaries...")
      (source "Bin")
      (dest "KMS:Bin")
      (help @copyfiles-help)
      (infos)
      (all)
   )

   (complete 15)

   (copyfiles
      (prompt "Copying Data Files...")
      (source "Data")
      (dest "KMS:Data")
      (help @copyfiles-help)
      (all)
   )

   (complete 25)

   (copyfiles
      (prompt "Copying Documentation...")
      (source "Doc")
      (dest "KMS:Doc")
      (help @copyfiles-help)
      (infos)
      (all)
   )

   (complete 35)

   (copyfiles
      (prompt "Copying Text Files...")
      (source "Text")
      (help @copyfiles-help)
      (dest "KMS:Text")
      (all)
   )

   (complete 50)

   (copyfiles
      (prompt "Copying ARexx Files...")
      (source "Rexx")
      (dest "KMS:Rexx")
      (help @copyfiles-help)
      (all)
   )

   (complete 55)

   (copyfiles
      (prompt "Copying User Files...")
      (source "User")
      (dest "KMS:User")
      (help @copyfiles-help)
      (all)
   )

   (complete 60)

   (copyfiles
      (prompt "Copying Misc Files...")
      (source "")
      (dest "KMS:")
      (pattern "~(#?Install#?)")
      (help @copyfiles-help)
      (files)
      (infos)
   )

   (complete 70)

   (set umsbase (getenv "UMSMB.default"))
   (if (= umsbase "")
      (set umsbase "SYS:")
   )

   (set doit
      (askbool
         (prompt "May I append the necessary config entries to your ums.config?")
         (help
            "KMS needs configuration entries in your ums.config. If you didn't "
            "already insert the necessary entries by yourself, you have to let "
            "me do this now in order to get a working KMS installation."
         )
      )
   )

   (if doit
      (
      (set umsbase
         (askdir
            (prompt "In which directory is your ums.config please?")
            (default umsbase)
            (help 
               "Your ums.config resides in your UMS messagebase directory. "
               "So please just tell me the name of this directory.\n"
               "\n"
               @askdir-help
            )
         )
      )
      (rename 
         (tackon umsbase "ums.config")
         (tackon umsbase "ums.config.old")
         (prompt "Renaming original ums.config...")
         (help "Your original ums.config will be renamed to \"ums.config.old\" with your friendly permission...")
      )
      (textfile
         (prompt "Creating new ums.config...")
         (dest (tackon umsbase "ums.config"))
         (include (tackon umsbase "ums.config.old"))
         (include "Doc/KMS_UMS.config")
         (help
            "Why can't you simply let me do my work, eh? :-) "
            "You seem to be very curious but anyhow why shouldn't I tell you: "
            "I'm creating a new ums.config with the KMS related entries appended now..."
         )
      )
      )
   )

   (complete 80)

   (if
      (askbool
         (prompt "Do you want to install the AXAUX: package?")
         (help 
            "If you want to use KMS over a serial line, you have to install "
            "some DOS device the standard io streams may be redirected through.\n"
            "\nNota bene: You can't use the standard Commodore AUX: device, because this "
            "is not capable of handling binary data correctly.\n"
            "\nThe AXAUX: handler, written by Pasi\"Albert\"Ojala (po87553@cs.tut.fi) "
            "meets all needs very well. Many thanks go to Pasi for him giving "
            "me his ok to include it in the KMS package!"
         )
      )
      (
      (copyfiles
         (prompt "Copying DosDrivers...")
         (source "AXAUX/devs/DosDrivers")
         (dest "DEVS:DosDrivers")
         (help
            "I'm now copying the AXAUX: mountlist entry into your "
            "\"DEVS:DosDrivers\" directory with your friendly permission.\n"
            "\n"
            @copyfiles-help
         )
         (infos)
         (all)
      )
      (copylib
         (prompt "Copying AXAUX-Handler...")
         (source "AXAUX/l/AXAUX-Handler")
         (dest "L:")
         (help
            "AXAUX needs this handler copied to your \"L:\" directory.\n"
            "\n"
            @copylib-help
         )
      )
      (set axdocdest
         (askdir
            (prompt "Please select the location where I shall place the AXAUX documentation.")
            (default (tackon kmsdest "Doc"))
            (help @askdir-help)
         )
      )
      (copyfiles
         (prompt "Copying documentation...")
         (source "AXAUX/AXAUX-Handler.doc")
         (dest axdocdest)
         (help @copyfiles-help)
      )
      (startup "AXAUX"
         (prompt "Adding \"Mount AXAUX:\" statement to your user-startup...")
         (command "Mount AXAUX:")
         (help 
            "You don't need this, if you've got a standard WB 2.1 startup-sequence. "
            "If you have not, then you will need to execute this statement before "
            "being able to use the AXAUX: device.\n"
            "\n"
            @startup-help
         )
      )
      )
   )

   (complete 95)

   (startup "KMS"
      (prompt "Adding a \"KMS:\" Assignment statement to your user-startup...")
      (command "Assign KMS: \"" kmsdest "\"")
      (help 
         "You'll need this, if you want to use the default KMS configuration. "
         "If you edit the KMS related entries in ums.config not to use \"KMS:\", "
         "then you may skip this part.\n"
         "\n"
         @startup-help
      )
   )

   (complete 100)

   (exit)
   )
)

; **************************************************************************
; update = 1 : Update KMS Installation
; **************************************************************************

(if (= update 1)
   (
   (if
      (NOT (exists "KMS:" (noreq)))
      (exit (quiet))
   )

   (if
      (= (getversion "KMS:Bin/KMSPort") (getversion "Bin/KMSPort"))
      (
      (complete 100)

      (abort "Your KMS installation is up-to-date!")
      )
   )
   (if
      (> (getversion "KMS:Bin/KMSPort") (getversion "Bin/KMSPort"))
      (
      (complete 100)

      (abort "Degrading? Are you kidding? :-)")
      )
   )

   (set vernum (getversion "Bin/KMSPort"))
   (set kmsver (/ vernum 65536))
   (set kmsrev (- vernum (* kmsver 65536)))
   (set vernum (getversion "KMS:Bin/KMSPort"))
   (set oldkmsver (/ vernum 65536))
   (set oldkmsrev (- vernum (* kmsver 65536)))

   (complete 5)

   (if (= vernum 0)
      (message ("You're about to update\nfrom KMS stoneage to KMS V%ld.%ld..." kmsver kmsrev))
      (message ("You're about to update\nfrom KMS V%ld.%ld to KMS V%ld.%ld..." oldkmsver oldkmsrev kmsver kmsrev))
   )

   (set kmsdest (getassign "KMS"))
   (set @default-dest kmsdest)

   (if
      (= vernum 0)
      (set doit 1)
      (set doit
         (askbool
            (prompt "Do you want to backup your current KMS installation?")
            (help 
               "In the case you have made many changes to your KMS related "
               "text and data files, I recommend you to do the backup for being "
               "able to refer to them afterwards for reconfiguring the new version. "
               "Otherwise all your changes will be lost after updating.\n"
               "\n"
               "For information about which of your old files may be reused "
               "in the new version, please refer to the \"Version.dok\" file "
               "supplied in the documentation directory. Thank you."
            )
         )
      )
   )

   (if doit
      (
      (makedir "KMS:/KMSBackup" (infos))
      
      (working "Copying KMS: directory to KMS:/KMSBackup...")

      (run "Copy KMS: KMS:/KMSBackup ALL CLONE QUIET")
      )
   )

   (complete 10)

   (if
      (= vernum 0)
      (
      ; Special work to be done for updating from KMS 1.6

      (message
         "Your current KMS directory was copied to \"KMSBackup\" in the parent "
         "directory of your current KMS installation because of major changes "
         "done since KMS stoneage up to the current version.\n"
         "\n"
         "For information about which of your old files may be reused "
         "in the new version, please refer to the \"Version.dok\" file "
         "supplied in the documentation directory. Thank you."
      )
      (if
         (exists "L:KMSServer" (noreq))
         (delete
            "L:KMSServer"
            (prompt "Deleting L:KMSServer...")
            (help
               "The KMSServer program now resides in the KMS binary directory. "
               "So I'm deleting it from \"L:\"... With your very friendly permission."
            )
            (optional "force")
         )
      )
      )
   )

   (foreach
      "Bin"
      "#?"
      (copylib
         (prompt "Updating Binaries...")
         (source (tackon "Bin" @each-name))
         (dest "KMS:Bin")
         (help @copylib-help)
         (infos)
      )
   )

   (complete 20)

   (copyfiles
      (prompt "Copying Data Files...")
      (source "Data")
      (dest "KMS:Data")
      (help
         "You may choose files not to be copied here, if you know what you "
         "are doing...\nPlease refer to \"Version.dok\" for information about "
         "reuseable files.\n"
         "\n"
         @copyfiles-help
      )
      (confirm)
      (all)
   )

   (complete 30)

   (copyfiles
      (prompt "Copying Documentation...")
      (source "Doc")
      (dest "KMS:Doc")
      (infos)
      (help @copyfiles-help)
      (all)
   )

   (complete 50)

   (copyfiles
      (prompt "Copying Text...")
      (source "Text")
      (dest "KMS:Text")
      (help
         "You may choose files not to be copied here, if you know what you "
         "are doing...\nPlease refer to \"Version.dok\" for information about "
         "reuseable files.\n"
         "\n"
         @copyfiles-help
      )
      (confirm)
      (all)
   )

   (complete 70)

   (copyfiles
      (prompt "Copying ARexx Files...")
      (source "Rexx")
      (dest "KMS:Rexx")
      (help
         "You may choose files not to be copied here, if you know what you "
         "are doing...\nPlease refer to \"Version.dok\" for information about "
         "reuseable files.\n"
         "\n"
         @copyfiles-help
      )
      (confirm)
      (all)
   )

   (complete 80)

   (copyfiles
      (prompt "Copying User Files...")
      (source "User")
      (dest "KMS:User")
      (help
         "You may choose files not to be copied here, if you know what you "
         "are doing...\nPlease refer to \"Version.dok\" for information about "
         "reuseable files.\n"
         "\n"
         @copyfiles-help
      )
      (confirm)
      (all)
   )

   (complete 90)

   (copyfiles
      (prompt "Copying Misc Files...")
      (source "")
      (dest "KMS:")
      (pattern "~(#?Install#?)")
      (files)
      (infos)
   )

   (complete 100)

   (exit)
   )
)

; **************************************************************************
; update = 2 : Remove KMS Installation
; **************************************************************************

(if (= update 2)
   (
   (if
      (NOT (exists "KMS:" (noreq)))
      (exit (quiet))
   )
   (if 
      (NOT
         (askbool
            (prompt
               "This will delete the complete \"KMS:\" directory!\n"
               "\n"
               "Do you *really* want to do that?"
            )
            (help "Do you?")
         )
      )
      (exit (quiet))
   )

   (complete 10)

   (makeassign "TempKMS" "KMS:/")
   (makeassign "KMS")

   (working "Deleting KMS: directory...")

   (run "Delete >NIL: TempKMS:KMS ALL QUIET FORCE")
   (run "Delete >NIL: TempKMS:KMS.info QUIET FORCE")
   
   (makeassign "TempKMS")

   (complete 90)

   (startup "KMS"
      (prompt "Removing \"KMS:\" Assignment statement from user-startup...")
      (command "; (removed)")
      (help "You *must* have this done, if you've got such a statement in there!")
   )

   (complete 100)

   (message
      "All done. Now you got rid of KMS...\n"
      "\n"
      "I hardly hope that you deinstalled KMS only to install it again lateron :-)"
   )

   (exit (quiet))
   )
)

