; $VER: KMS 1.90b
; Copyright ©1994 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 KMSAUX: 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 950000)
      (if
         (<> (getdevice kmsdest) "RAM")
         (abort
            "This installation requires about 950 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 Doors Files...")
      (source "Doors")
      (dest "KMS:Doors")
      (help @copyfiles-help)
      (all)
   )
   
   (complete 65)

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

   (delete
      "KMS:Devs.info"
      (help
         "This file is junk."
      )
      (optional "force")
   )

   (copyfiles
      (prompt "Copying /KMS.info...")
      (source "/KMS.info")
      (dest "KMS:/")
      (help @copyfiles-help)
      (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
      (exists "DEVS:DosDrivers" (noreq))
      (copyfiles
         (prompt "Copying DosDrivers...")
         (source "Devs/DosDrivers")
         (dest "DEVS:DosDrivers")
         (help
            "I'm now copying the KMSAUX: mountlist entry into your "
            "\"DEVS:DosDrivers\" directory with your friendly permission...\n"
            "\n"
            @copyfiles-help
         )
         (infos)
         (all)
      )
      (
      (rename
         ("DEVS:MountList")
         ("DEVS:MountList.old")
         (prompt "Renaming original DEVS:MountList...")
         (help "Your original MountList will be renamed to \"MountList.old\" with your friendly permission...")
      )
      (textfile
         (prompt "Adding entry to DEVS:MountList...")
         (dest "DEVS:MountList")
         (include "DEVS:MountList.old")
         (append "KMSAUX:")
         (include "Devs/DosDrivers/KMSAUX")
         (append "#")
         (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 adding an entry for KMSAUX: to your DEVS:MountList..."
         )
      )
      )
   )
   (copylib
      (prompt "Copying KMSAUX-Handler...")
      (source "l/KMSAUX-Handler")
      (dest "L:")
      (help
         "KMSAUX needs this handler to be copied to your \"L:\" directory.\n"
         "\n"
         @copylib-help
      )
   )
   (if
      (NOT (exists "DEVS:DosDrivers" (noreq)))
      (startup "KMSAUX"
         (prompt "Adding \"Mount KMSAUX:\" statement to your user-startup...")
         (command "Mount KMSAUX:")
         (help
            "You will need to execute this statement before being able to use "
            "the KMSAUX: 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 "
         "and/or let KMS-Install do updates to the actual version from an older one. "
         "If you edit the KMS related entries in ums.config not to use \"KMS:\", "
         "then you may skip this part. In this case you also have to install KMS "
         "from scratch for each update.\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)

   (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)

   (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
      (
      (if
         (NOT (exists "KMS:/KMSBackup" (noreq)))
         (makedir "KMS:/KMSBackup" (infos))
      )
      
      (working "Copying KMS: directory to KMS:/KMSBackup...")

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

   (complete 10)

   (if
      (= oldkmsrev 75)
      (
      ; Special work to be done for updating from KMS 1.75b

      (if
         (exists "KMS:Data/KMS_AMIGA.CHR" (noreq))
         (delete
            "KMS:Data/KMS_AMIGA.CHR"
            (help
               "The character conversion scheme has changed. The old translation "
               "tables aren't supported any longer."
            )
            (optional "force")
         )
      )
      (if
         (exists "KMS:Data/KMS_IBM.CHR" (noreq))
         (delete
            "KMS:Data/KMS_IBM.CHR"
            (help
               "The character conversion scheme has changed. The old translation "
               "tables aren't supported any longer."
            )
            (optional "force")
         )
      )
      (if
         (exists "KMS:Data/KMS_ATARI.CHR" (noreq))
         (delete
            "KMS:Data/KMS_ATARI.CHR"
            (help
               "The character conversion scheme has changed. The old translation "
               "tables aren't supported any longer."
            )
            (optional "force")
         )
      )
      (if
         (exists "KMS:Doc/KMS.dok" (noreq))
         (delete
            "KMS:Doc/KMS.dok"
            (help
               "The documentation to KMS is now supplied as an AmigaGuide® file, "
               "KMS.dok is no longer supported."
            )
            (optional "force")
         )
      )
      (makedir "KMS:Doors")
      (if
         (exists "KMS:Data/KMS_UAF.DAT" (noreq))
         (
         (rename
            ("KMS:Data/KMS_UAF.DAT")
            ("KMS:Data/KMS_UAF.OLD")
            (help "Your original user data file will be renamed to \"KMS_UAF.OLD\"...")
         )
         (run "Bin/KMSConv -u KMS:Data/KMS_UAF.OLD KMS:Data/KMS_UAF.DAT")
         (message
            "Your user data file was converted into the new format. Your old "
            "\"KMS_UAF.DAT\" was renamed to \"KMS_UAF.OLD\"."
         )
         )
      )
      )
   )

   (if
      (OR (= oldkmsrev 75) (= oldkmsrev 80))
      
      ; Special work to be done for updating from KMS 1.75b or 1.80b

      (if
         (exists "KMS:Data/KMS_AREAS.DAT" (noreq))
         (
         (rename
            ("KMS:Data/KMS_AREAS.DAT")
            ("KMS:Data/KMS_AREAS.OLD")
            (help "Your original area data file will be renamed to \"KMS_AREAS.OLD\"...")
         )
         (run "Bin/KMSConv -a KMS:Data/KMS_AREAS.OLD KMS:Data/KMS_AREAS.DAT")
         (message
            "Your area data file was converted into the new format. Your old "
            "\"KMS_AREAS.DAT\" was renamed to \"KMS_AREAS.OLD\"."
         )
         )
      )
   )

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

   (complete 20)

   (copyfiles
      (prompt "Updating 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 "Updating Documentation...")
      (source "Doc")
      (dest "KMS:Doc")
      (infos)
      (help @copyfiles-help)
      (all)
   )

   (complete 50)

   (copyfiles
      (prompt "Updating 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 "Updating 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 75)

   (copyfiles
      (prompt "Updating 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 80)

   (copyfiles
      (prompt "Updating Doors Files...")
      (source "Doors")
      (dest "KMS:Doors")
      (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 85)

   (copylib
      (prompt "Updating KMSAUX-Handler...")
      (source "l/KMSAUX-Handler")
      (dest "L:")
      (help
         "You really should update your KMSAUX-Handler if necessary!\n"
         "\n"
         @copylib-help
      )
   )

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

   (delete
      "KMS:Devs.info"
      (help
         "This file is junk."
      )
      (optional "force")
   )

   (complete 90)

   (if
      (< oldkmsrev 85)
      (
      ; Special work to be done for updating from before KMS 1.85b

      (if
         (exists "DEVS:DosDrivers" (noreq))
         (copyfiles
            (prompt "Copying DosDrivers...")
            (source "Devs/DosDrivers")
            (dest "DEVS:DosDrivers")
            (help
               "I'm now copying the KMSAUX: mountlist entry into your "
               "\"DEVS:DosDrivers\" directory with your friendly permission...\n"
               "\n"
               @copyfiles-help
            )
            (infos)
            (all)
         )
         (
         (rename
            ("DEVS:MountList")
            ("DEVS:MountList.old")
            (prompt "Renaming original DEVS:MountList...")
            (help "Your original MountList will be renamed to \"MountList.old\" with your friendly permission...")
         )
         (textfile
            (prompt "Adding entry to DEVS:MountList...")
            (dest "DEVS:MountList")
            (include "DEVS:MountList.old")
            (append "KMSAUX:")
            (include "Devs/DosDrivers/KMSAUX")
            (append "#")
            (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 adding an entry for KMSAUX: to your DEVS:MountList..."
            )
         )
         )
      )
      (copylib
         (prompt "Copying KMSAUX-Handler...")
         (source "l/KMSAUX-Handler")
         (dest "L:")
         (help
            "KMSAUX needs this handler to be copied to your \"L:\" directory.\n"
            "\n"
            @copylib-help
         )
      )
      (if
         (NOT (exists "DEVS:DosDrivers" (noreq)))
         (startup "KMSAUX"
            (prompt "Adding \"Mount KMSAUX:\" statement to your user-startup...")
            (command "Mount KMSAUX:")
            (help
               "You will need to execute this statement before being able to use "
               "the KMSAUX: device.\n"
               "\n"
               @startup-help
            )
         )
      )
      )
   )

   (complete 95)

   (if
      (< oldkmsrev 80)

      ; Special work to be done for updating from before KMS 1.80b
      
      (message
         "Update complete. There's just one thing left to do for you:\n"
         "\n"
         "Please insert the entries \"KMS.doordir\" and \"KMS.tempdir\" "
         "into your \"ums.config\" and assign the appropriate directories to them.\n"
         "\n"
         "For example:\n\"KMS.doordir = KMS:Doors/\" (default: \"Doors/\")\n"
         "\"KMS.tempdir = KMS:Files/Uploads/\" (default: \"T:\")"
      )
   )

   (if
      (< oldkmsrev 90)

      ; Special work to be done for updating from before KMS 1.90b
      
      (
      (message
         "\nUpdate complete.\n"
         "\n"
         "There's just one thing left to do for you:\n"
         "\n"
         "Please add the local variables \"KMS.xprcmdreceive\" and \"KMS.xprcmdsend\" "
         "to the KMS user entry in your \"ums.config\".\n"
      )
      (message
         "\nIf you want to use the included XPR-Tool KMSXPR for binary transfers, "
         "these entries have to be defined as follows:\n\n"
         "KMS.xprcmdreceive = \"KMS:Bin/KMSXPR %v OPTIONS \\\"%w\\\" RECEIVE LOGDIR T: DEFDIR %o %x\"\n"
         "KMS.xprcmdsend    = \"KMS:Bin/KMSXPR %v OPTIONS \\\"%w\\\" SEND LOGDIR T: %x\"\n\n"
         "(You may copy these lines from Doc/KMS_UMS.config...)"
      )
      )
   )

   (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
      "\nAll done. Now you got rid of KMS...\n"
      "\n"
      "I hardly hope that you deinstalled KMS only to install it again lateron :-]"
   )

   (exit (quiet))
   )
)

