; $VER: Install_AWeb30b_patch (18.9.97)
; Description: Installer script for AWeb-II 3.0 -> 3.0b patch

(complete 0)

(set #welcome
   (cat "This installation procedure installs the AWeb-II 3.0b patch on your system.\n\n"
        "You must have installed AWeb-II 3.0 previously.\n\n"
        "You will need the original AWeb-II 3.0 disks for this installation procedure."
   )
)

(set #no_aweb30 
   (cat "AWeb3: assign not found.\nYou haven't properly installed AWeb-II 3.0.\n\n"
        "The AWeb-II 3.0b patch is not applied."
   )
)

(set #ask_backup "Do you want to make a backup copy of the currently installed version of AWeb-II?")

(set #insert_disk1
   (cat "Please insert disk 1 of the original AWeb-II 3.0 distribution "
        "(AWeb3.0_Disk1) into any drive\n"
   )
)

(set #settingup "Setting up utilities")
(set #patching "Patching %s\n\nBe patient, this can take a while...")
(set #installing "Installing %s")

(set #invalid_version
   (cat "Patch could not be performed.\n"
        "The file '%s' has an invalid version. This patch can ONLY be applied "
        "to AWeb-II version 3.0 (not 3.0b)\n\n"
        "Installation script terminated."
   )
)

(set #ask_run
   (cat "The first time you run AWeb again, you will have to re-enter "
        "the serial number found on the original distribution, and your name.\n\n"
        "Do you want to start AWeb now?"
   )
)

(set #ready "Installation of AWeb-II 3.0 -> 3.0b patch complete.")


;---- Procedure to patch 1 file from 1 archive ----

(procedure P_patch #arc #file #sum #complete

   (working (#patching #file))

   ; Build command like
   ;  T:lhex -w=T: e AWeb3.0_Disk1:<#arc> <#file>
   ;
   ; extracts 1 file from the archive on the distribution disk into T:
   (run
      ("T:lhex -w=T: e AWeb3.0_Disk1:%s %s" #arc #file)
   )

   (complete #complete)

   ; Check the checksum of the extracted file
   (if
      (<>
         (getsum (tackon "T:" #file)
         )
         #sum
      )
      (
         (delete (cat "T:" #file) )
         (exit (#invalid_version #file) (quiet))
      )
   )

   ; Check the version of the extracted file - it must be 3.0 (not 3.0b)
   (run ("setenv AWEB30BPCHVERSION `version T:%s`" #file)
   )
   (set #version (getenv "AWEB30BPCHVERSION")
   )
   (set #strl
      (-
         (strlen #version)
         3
      )
   )
   (if
      (<>
         (substr #version #strl)
         "3.0"
      )
      (
         (delete (cat "T:" #file) )
         (exit (#invalid_version #file) (quiet))
      )
   )

   (complete (+ #complete 10))

   ; Build command like
   ;  T:spatch -p<path><#file>.pch -oAWeb3:<#file> T:<#file>
   ;
   ; patches the original file in T: using the .pch file building a new file in AWeb3:
   (run
      (cat
         "T:spatch -p"
         (cat #file ".pch")
         (" -oAWeb3:%s T:%s" #file #file)
      )
   )

   ; Delete the extracted file from T:
   (delete
      (cat "T:" #file)
   )
)

;---- Display welcome message ----

(message #welcome)

(complete 5)

;---- Check the existence of AWeb3: assign ----

(if 
   (not (exists "AWeb3:"))
   (
      (message #no_aweb30)
      (exit (quiet))
   )
)

(complete 10)

;---- Ask for backup of current versions ----

(if
   (askbool
      (prompt #ask_backup)
      (help @askbool-help)
   )
   (
      (copyfiles
         (prompt #make_backup)
         (help @copyfiles-help)
         (source "AWeb3:AWeb-II")
         (dest "AWeb3:")
         (newname "AWeb-II_3.0")
         (optional "nofail")
      )
      (copyfiles
         (prompt #make_backup)
         (help @copyfiles-help)
         (source "AWeb3:AWebCfg")
         (dest "AWeb3:")
         (newname "AWebCfg_3.0")
         (optional "nofail")
      )
   )
)

(complete 20)

;---- Let the user insert the correct disk ----

(askdisk
   (prompt #insert_disk1)
   (help @askdisk-help)
   (dest "AWeb3.0_Disk1")
;   (assigns)               ; **** Remove this before distribution ****
)

(complete 25)

;---- Copy required utilities to T: ----

(copyfiles
   (prompt #settingup)
   (help @copyfiles-help)
   (source "AWeb3.0_Disk1:lhex")
   (dest "T:")
)

(copyfiles
   (prompt #settingup)
   (help @copyfiles-help)
   (source (tackon (pathonly @icon) "spatch"))
   (dest "T:")
)

(complete 30)

;---- Patch AWeb-II ----

(P_patch "aweb2.lha" "AWeb-II" 1498876731 40)

(complete 60)

;---- Patch AWebCfg ----

(P_patch "aweb1.lha" "AWebCfg" 664509881 70)

(complete 90)

;---- Delete utilities from T: ----

(delete "T:lhex")
(delete "T:spatch")

;---- Install new documentation ----

(copyfiles
   (prompt (#installing "documentation"))
   (help @copyfiles-help)
   (source (tackon (pathonly @icon) "docs"))
   (dest "AWeb3:docs")
   (all)
)

(complete 95)

;---- Ask if AWeb should run now to enter the registration details ----

(if
   (askbool
      (prompt #ask_run)
      (help @askbool-help)
   )
   (
      (run "run AWeb3:AWeb-II local AWeb3:docs/history.html")
   )
)

(complete 100)

;---- Ready ----

(exit #ready (quiet))

