(set vernum (getversion "libs:version.library"))
(set ver (/ vernum 65536))
(complete 0)

(message
   "If you wish to create a boot disk during this installation, boot your system "
   "with a copy of your original Workbench disk.  The 1.3, 2.04 and 2.1 releases of AmigaDOS are "
   "supported by this script.\n\n"
   "If you're using a boot disk of your own you will need approximately 140k of free disk "
   "space for the various font and library files that will be copied to it.\n\n"
)
(message
   "You will also need a formatted, blank disk for storing the Terminus "
   "program and supporting files if this is a first time installation.\n\n"
   "Otherwise, insert your Terminus program disk in a drive now, it must be "
   "labeled \"TERMINUS\" for the update to take place."
)
(if (strlen (getassign "TERMINUS" "a"))
   (
      (set rem_flag
         (askbool
            (prompt
               "The logical assignment \"TERMINUS\" is present, it must be unassigned "
               "before the installation can continue."
            )
            (help 
               "Select \"Remove\" to remove"
               "this assignment or \"Abort\" to abort this script."
            )
            (default 1)
            (choices "Remove" "Abort")
         )
      )
      (if (= rem_flag 0)
         (abort "User aborted installation.")
         (run "Assign TERMINUS:")
      )
   )
)

(set is_update 0)
(set term-dest (getassign "TERMINUS" "v"))

(if (strlen term-dest)
   (
      (set is_update 
         (askbool
            (prompt 
               "The volume \"TERMINUS:\" is present in \""
               (getdevice term-dest)
               ":\".\n\n  Do you want the update installed there?"
            )
            (help @askchoice-help)
            (choices "Proceed" "Abort")
            (default 1)
         )
      )
      (if (= is_update 0)
         (abort "User aborted installation.")
      )
   )
)

(if (= is_update 0)
   (set make_boot 
      (askchoice
         (prompt 
            "If you need to create a boot floppy, select the appropriate "
            "choice for the version of AmigaDOS that you're using.  If not, "
            "select the option labled \"None\".\n\n"
            "In either case you must have a boot disk inserted in DF0: so that "
            "the required fonts, libraries and other files can be copied to it."
         )
         (choices "None" "AmigaDOS 1.3" "AmigaDOS 2.x")
         (default 0)
         (help @askchoice-help)
      )
   )
)
         
(set boot-dest "DF0:")
(if (not (= make_boot 0))
   (
      (message
         "The disk installed DF0: is about to be modified.  All unneccesary files are going to "
         "be deleted.  Are you certain that you want to continue with this installation?\n\n"
         "CAUTION -- DO NOT USE YOUR ORIGINAL WORKBENCH DISK!"
      )
      (set n 0)
      (if (= make_boot 1)
         (
            (while (set thisfile (select n "c/Ask" 
                                             "c/DiskChange"
                                             "c/DiskDoctor"
                                             "c/Edit"
                                             "c/Lock"
                                             "c/Protect"
                                             "c/Quit"
                                             "c/RemRAD"
                                             "c/Sort"
                                             "c/Which"
                                             "c/Why"
                                             "System/FixFonts"
                                             "System/FixFonts.info"
                                             "System/InitPrinter"
                                             "System/InitPrinter.info"
                                             "System/MergeMem"
                                             "System/MergeMem.info"
                                             "System/NoFastMem"
                                             "System/NoFastMem.info"
                                             "Prefs/CopyPrefs"
                                             "Prefs/CopyPrefs.info"
                                             "Prefs/Pointer.info"
                                             "Prefs/Serial.info"
                                             "l/Aux-Handler"
                                             "l/FastFileSystem"
                                             "l/Pipe-Handler"
                                             "l/Speak-Handler"
                                             "devs/narrator.device"
                                             "s/Startup-Sequence"
                                             "s/Startup-Sequence.HD"
                                             "s/StartupII"
                                             "fonts/ruby/12"
                                             "fonts/ruby/15"
                                             "fonts/ruby/8"
                                             "fonts/opal/12"
                                             "fonts/opal/9"
                                             "fonts/sapphire/14" 
                                             "fonts/sapphire/19"
                                             "fonts/diamond/12"
                                             "fonts/diamond/20"
                                             "fonts/garnet/16"
                                             "fonts/garnet/9"
                                             "fonts/emerald/17"
                                             "fonts/emerald/20"
                                             "fonts/diamond.font"
                                             "fonts/emerald.font"
                                             "fonts/garnet.font"
                                             "fonts/opal.font"
                                             "fonts/ruby.font"
                                             "fonts/sapphire.font"
                                             "fonts/diamond"
                                             "fonts/emerald"
                                             "fonts/garnet"
                                             "fonts/opal"
                                             "fonts/ruby"
                                             "fonts/sapphire"
                                             "libs/mathieeedoubbas.library"
                                             "libs/mathieeedoubtrans.library"
                                             "libs/mathtrans.library"
                                             "libs/translator.library"
                                             "Empty"
                                             "Utilities/.info"
                                             "Utilities/Calculator"
                                             "Utilities/Calculator.info"
                                             "Utilities/Clock"
                                             "Utilities/Clock.info"
                                             "Utilities/ClockPtr"
                                             "Utilities/ClockPtr.info" 
                                             "Utilities/CMD"
                                             "Utilities/Cmd.info"
                                             "Utilities/InstallPrinter"
                                             "Utilities/InstallPrinter.info"
                                             "Utilities/Notepad"
                                             "Utilities/Notepad.info"
                                             "Utilities/Say"
                                             "Utilities/Say.info"
                                             "Expansion"
                                             "Empty.info"
                                             "Expansion.info"
                                             ""))
               (
                  (set n (+ n 1))
                  (working "Deleting file: " thisfile)
                  (delete (tackon boot-dest thisfile) (optional "force"))
               )
            )

            (complete 9)
            (working "Building Startup-sequence script file...")
            (textfile
               (dest (tackon boot-dest "s/startup-sequence"))
               (append
                  "echo \"Terminus 2.0 boot disk for Workbench 1.3\"\n"
                  "version\n"
                  "c:SetPatch >NIL: r\n"
                  "Addbuffers df0: 10\n"
                  "Binddrivers\n"
                  "FF >NIL: -0\n"
                  "Sys:System/FastMemFirst\n"
                  "resident CLI L:Shell-Seg SYSTEM pure add\n"
                  "mount newcon:\n"
                  "makedir ram:t\n"
                  "assign T: ram:t\n"
                  "makedir ram:env\n"
                  "assign ENV: ram:env\n"
                  "makedir ram:clipboards\n"
                  "assign CLIPS: ram:clipboards\n"
                  "SYS:System/SetMap usa1\n"
                  "path ram: c: s: sys:system sys:utilities add\n"
                  "LoadWB\n"
                  "EndCLI >NIL:\n"
               )
            )
         )
         (
            (while (set thisfile (select n "Prefs/Pointer"
                                             "Prefs/Pointer.info"
                                             "Prefs/Serial"
                                             "Prefs/Serial.info"
                                             "Libs/bullet.library"
                                             "Libs/mathieeedoubbas.library"
                                             "Libs/mathieeedoubtrans.library"
                                             "Libs/mathieeesingtrans.library"
                                             "Libs/mathtrans.library"
                                             "Expansion"
                                             "System/Fountain"
                                             "System/Fountain.info"
                                             "System/FixFonts"
                                             "System/FixFonts.info"
                                             "System/NoFastMem"
                                             "System/NoFastMem.info"
                                             "Utilities/Clock"
                                             "Utilities/Clock.info"
                                             "Utilities/Display"
                                             "Utilities/Display.info"
                                             "Utilities/Exchange"
                                             "Utilities/Exchange.info"
                                             "Utilities/Say"
                                             "Utilities/Say.info"
                                             "L/aux-handler"
                                             "L/speak-handler"
                                             "S/HDBackup.config"
                                             "S/PickMap"
                                             "S/Startup-sequence"
                                             "S/Startup-sequence.HD"
                                             "C/DiskChange"
                                             "C/DiskDoctor"
                                             "C/Edit"
                                             "C/Lock"
                                             "C/MagTape"
                                             "C/Protect"
                                             "C/RemRAD"
                                             "C/Sort"
                                             "C/Which"
                                             "Devs/postscript_init.ps"
                                             "Devs/narrator.device"
                                             "Devs/DOSDrivers/PIPE"
                                             "Devs/DOSDriversPIPE.info"
                                             "Devs/DOSDrivers"
                                             "Expansion.info"
                                             ""))
               (
                  (set n (+ n 1))
                  (working "Deleting file: " thisfile)
                  (delete (tackon boot-dest thisfile) (optional "force"))
               )
            )

            (complete 9)
            (working "Building Startup-sequence script file...")
            (textfile
               (dest (tackon boot-dest "s/startup-sequence"))
               (append
                  "c:setpatch >NIL:\n"
                  "c:version >NIL:\n"
                  "addbuffers >NIL: df0: 15\n"
                  "Resident >NIL: C:Execute PURE ADD\n"
                  "makedir ram:T ram:Clipboards ram:env ram:env/sys\n"
                  "copy >NIL: ENVARC: ram:env all quiet noreq\n"
                  "assign ENV: ram:env\n"
                  "assign T: ram:t ;set up T: directory for scripts\n"
                  "assign CLIPS: ram:clipboards\n"
                  "assign REXX: s:\n"
                  "if exists sys:Monitors\n"
                  "    join >NIL: sys:monitors/~(#?.info) as t:mon-start\n"
                  "    execute t:mon-start\n"
                  "    delete >NIL: t:mon-start\n"
                  "endif\n"
                  "BindDrivers\n"
                  "setenv Workbench $Workbench\n"
                  "setenv Kickstart $Kickstart\n"
                  "echo \"Terminus 2.0 boot disk for Workbench 2.x\"\n"
                  "version\n"
                  "IPrefs\n"
                  "conclip\n"
                  "path ram: c: sys:utilities sys:rexxc sys:system s: sys:prefs sys:wbstartup add\n"
                  "LoadWB\n"
                  "EndCLI >NIL:\n"
               )
            )
            (if (not (exists (tackon boot-dest "fonts")))
               (
                  (makedir (tackon boot-dest "fonts"))
                  (run "Assign FONTS: DF0:fonts")
               )
            )
         )
      )
   )
)

(complete 18)

(askdisk
   (prompt "Please insert the disk labeled \"TerminusMaster\"")
   (help @askdisk-help)
   (dest "TerminusMaster")
)

(copyfiles
   (source "TerminusMaster:Fonts")
   (dest (tackon boot-dest "fonts"))
   (all)
)

(complete 27)

(if (and (not (exists (tackon boot-dest "libs/req.library"))) (not (exists (tackon boot-dest "libs/asl.library"))))
   (copyfiles
      (source "TerminusMaster:Libs/req.library")
      (dest (tackon boot-dest "libs"))
   )
)

(complete 36)

(if (not (exists (tackon boot-dest "libs/OwnDevUnit.library")))
   (copyfiles
      (source "TerminusMaster:Libs/OwnDevUnit.library")
      (dest (tackon boot-dest "libs"))
   )
)

(complete 45)

(if (not (exists (tackon boot-dest "libs/xprkermit.library")))
   (copyfiles
      (source "TerminusMaster:Libs/xprkermit.library")
      (dest (tackon boot-dest "libs"))
   )
)

(complete 54)

(if (not (exists (tackon boot-dest "libs/xprbplus.library")))
   (copyfiles
      (source "TerminusMaster:Libs/xprbplus.library")
      (dest (tackon boot-dest "libs"))
   )
)

(complete 63)

(if (= is_update 0)
   (
      (message 
         "Please insert a formatted, blank disk in drive DF0:\n\n"
         "-- CAUTION --\n\n  DO NOT insert your boot disk in drive DF0: "
         "if the system asks for it during the following operations."
      )
      (set term-dest "DF0:")
   )
)

(set script-dest (tackon term-dest "Scripts"))
(if (not (exists script-dest))
   (makedir script-dest)
)

(copyfiles
   (source "TerminusMaster:Scripts")
   (dest script-dest)
   (all)
)

(complete 72)

(working "Extracting Terminus executable files...")
(run "TerminusMaster:bin/lharc -mx e TerminusMaster:ExecFiles DF0:")

(complete 81)

(working "Extracting Terminus documentation files...")
(run "TerminusMaster:bin/lharc -mx e TerminusMaster:DocFiles DF0:")

(complete 90)

(run "relabel drive DF0: TERMINUS")
(delete (tackon term-dest ".info") (optional "force"))
(set @default-dest "TERMINUS:")

(complete 100)

(message
   "Please remove then reinsert your Terminus program disk so that Workbench can "
   "update the disk volume name."
)

(exit)

