; Installer Patch Script for KingFisher Release 2
; Copyright © 1994,1995 Udo Schuermann
; All rights reserved
; $VER: Install-KFPatch 2.8 (7.3.95)
;
;
; Installer and Installer project icon
; (c) Copyright 1991-93 Commodore-Amiga, Inc.  All Rights Reserved.
; Reproduced and distributed under license from Commodore.
; 
; INSTALLER SOFTWARE IS PROVIDED "AS-IS" AND SUBJECT TO CHANGE;
; NO WARRANTIES ARE MADE.  ALL USE IS AT YOUR OWN RISK.  NO LIABILITY
; OR RESPONSIBILITY IS ASSUMED.

; (set VER "blah 2.7 (28.2.94)"
; returns with REV set to "2" and VER set to "7" as in "2.7" without the dot
(procedure extract-version (
  (set i 0 sREV "" sVER "" LOOKING 1)
  (while (AND (= LOOKING 1) (< i 255)) (
    (set t (substr VER i 1))
    (if (<= t '9')
      ((if (>= t '0')
        (set LOOKING 0)
        (set i (+ i 1))))
      (set i (+ i 1)))
  ))
  (if (= LOOKING 0) (
    (set sREV (substr VER i 1))
    (set i (+ i 1) LOOKING 1)
    (while (AND (= LOOKING 1) (< i 255)) (
      (set t (substr VER i 1))
      (if (<= t '9')
        (if (>= t '0')
          (set sREV ("%s%s" sREV t))))
      (if (OR (= t '.') (= t ' '))
        (set LOOKING 0))
      (set i (+ i 1))
    ))
    (set LOOKING 1)
    (while (AND (= LOOKING 1) (< i 255)) (
      (set t (substr VER i 1))
      (if (<= t '9')
        (if (>= t '0')
          (set sVER ("%s%s" sVER t))))
      (if (= t ' ')
        (set LOOKING 0))
      (set i (+ i 1))
    ))
  ))
))

; (set VSTR "blah 2.7 (28.2.94)")
; call with VSTR set to "blah 2.7 (d.m.y)..."
; presence of EVALUATION or REGISTERED will return TYPEKEY "R" or TYPEKEY "E"
; as well as TYPENAME set to the 10 character string EVALUATION or REGISTERED
; defaults to registered (if the keystrings are not found)
(procedure extract-type (
  (set i 0 LOOKING 1 TYPEKEY "R" TYPENAME "REGISTERED")
  (while (AND (= LOOKING 1) (< i 255)) (
    (set t (substr VSTR i 10))
    (set i (+ i 1))
    (if (= t "REGISTERED")
      (set TYPEKEY "R" TYPENAME t LOOKING 0)
      (if (= t "EVALUATION")
        (set TYPEKEY "E" TYPENAME t LOOKING 0)))))
))

;(set REV sREVKFS VER sVERKFS FILE "KFServer" THISONE "26" THISREV "2" THISVER "6" VSTR vKFS)
; REV and VER are the original revision and version values of FILE
; THISONE is the new version without the dot in the middle; THISREV and THISVER
; are the value of THISONE but separated, VSTR is the version string as required
; by the (extract-type) function which is called from here.
(procedure patch-file (
  (if (= ("%s%s" REV VER) THISONE)
    (message ("You already have %s %s.%s installed!\n\nPatch skipped" FILE REV VER))
    ((extract-type)
     (working ("Patching %s %s version from\n\n%s.%s to %s.%s" FILE TYPENAME REV VER THISREV THISVER))
     (set ORIGINAL (tackon DESTIN FILE))
     (set PATCH ("%s_%s-%s%s-%s.pch" TYPEKEY FILE REV VER THISONE))
     (set OUTPUT (tackon DESTIN ("%s.new" FILE)))
     (if (exists PATCH)
       ((set CMD ("spatch \"-o%s\" \"-p%s\" \"%s\"" OUTPUT PATCH ORIGINAL))
        (run CMD)
        (if (exists (tackon DESTIN ("%s.new" FILE)))
          ((set BACKUP (tackon DESTIN ("%s-%s.%s" FILE REV VER)))
           (if (exists BACKUP)
             (delete BACKUP))
           (rename (tackon DESTIN FILE) BACKUP)
           (rename (tackon DESTIN ("%s.new" FILE)) (tackon DESTIN FILE)))
          ((set xUSER @user-level)
           (user 3)
           (message ("ERROR!\n\n%s could not be patched!" FILE))
           (user xUSER)))
       )
       (message ("A patch file to upgrade the %s version of %s from %s.%s to %s.%s is not available"
		TYPENAME FILE REV VER THISREV THISVER)))))
))

(complete 0)

(set LOOKING 1 FAIL 0)
(while (= LOOKING 1)
  (set DESTIN
    (askdir
      (prompt
	"Please indicate the directory where KingFisher is installed. "
	"This is the directory where the files are actually located.")
      (help @askdir-help)
      (default "SYS:KingFisher2")))
  (if (exists (tackon DESTIN "KFServer"))
    (if (exists (tackon DESTIN "KingFisher"))
      (set LOOKING 0)))
  (if (= LOOKING 1)
    (message (
	"The Installer cannot seem to find the KFServer and KingFisher "
	"programs in the '%s' directory.  Please try again or cancel the "
	"installation." DESTIN))))
(complete 1)

(working "Determining which software revisions you have currently installed")

(run ("c:version >ENV:VERKFS full \"%s\"" (tackon DESTIN "KFServer")))
(set VER (getenv "VERKFS"))
(set vKFS VER)
(extract-version)
(set sREVKFS sREV sVERKFS sVER)
(complete 3)

(run ("c:version >ENV:VERKF full \"%s\"" (tackon DESTIN "KingFisher")))
(set VER (getenv "VERKF"))
(set vKF VER)
(extract-version)
(set sREVKF sREV sVERKF sVER)
(complete 4)

(if (exists (tackon DESTIN "RexxFisher"))
  ((run ("c:version >ENV:VERRXF full \"%s\"" (tackon DESTIN "RexxFisher")))
   (set VER (getenv "VERRXF"))
   (set vRXF VER)
   (extract-version)
   (set sREVRXF sREV sVERRXF sVER))
  (set sREVRXF "" sVERRXF ""))
(complete 5)

(set REV sREVKFS VER sVERKFS FILE "KFServer" THISONE "28" THISREV "2" THISVER "8" VSTR vKFS)
(patch-file)
(complete 25)

(set REV sREVKF VER sVERKF FILE "KingFisher" THISONE "28" THISREV "2" THISVER "8" VSTR vKF)
(patch-file)
(complete 50)

(if (exists (tackon DESTIN "RexxFisher"))
  ((if (= @user-level 0) (user 1))
   (if (askbool
         (prompt
	   "Would you like to install RexxFisher 2.8?")
         (choices "Yes, proceed" "No, skip it")
         (default "Yes, proceed")
         (help @askbool-help))
; the next two lines are to be used at a later time:
;	((set REV sREVRXF VER sVERRXF FILE "RexxFisher" THISONE "28" THISREV "2" THISVER "8" VSTR vRXF)
;	 (patch-file))
; (copyfiles) replaces lines above for the moment:
        (copyfiles
	  (source "RexxFisher")
	  (dest DESTIN))))
  (message "No new RexxFisher available"))
(complete 75)

(delete "ENV:VERKFS")
(delete "ENV:VERKF")
(delete "ENV:VERRXF")

; update all installed catalogs (if any)
(if (exists (tackon DESTIN "Catalogs"))
  (foreach (tackon DESTIN "Catalogs") "#?" (
    (if (exists ("Catalogs/%s" @each-name))
      ((working ("Installing %s Language" @each-name))
       (copyfiles
  	(source (tackon "Catalogs" @each-name))
  	(dest (tackon DESTIN ("Catalogs/%s" @each-name)))
  	(all)))
      (message ("No new %s catalog available" @each-name))))))
(complete 85)

(if (exists (tackon DESTIN "KingFisher2.guide"))
  ((if (= @user-level 0) (user 1))
   (if (askbool
         (prompt
	   "Would you like to upgrade to a new online English "
	   "language documentation file?\n\nIf you have a translated "
	   "version of this file installed, you should first make a "
	   "backup copy before proceeding!")
         (choices "Yes, proceed" "No, skip it")
         (default "Yes, proceed")
         (help @askbool-help))
     ((copyfiles
        (source "KingFisher2.guide")
        (dest DESTIN))
      (set dosver (/ (getversion "dos.library" (resident)) 65536))
      (if (< dosver 39)
        (set WrapIt
          (askbool
	     (prompt (cat
		"*** ATTENTION - READ THIS!  ***\n\n"
		"The KingFisher2.guide documentation that ships with "
		"KingFisher Release 2 is formatted with long lines to "
		"provide Kickstart 3.x users with more readable, "
		"paragraph oriented documentation.\n"
		"This format causes trouble for AmigaGuide under "
		"Kickstart 2.x wherefore a special 'WrapGuide' tool "
		"is supplied to fix the .guide file for use with "
		("your installed version of AmigaDOS V%ld\n\n" dosver)
		"You are STRONGLY URGED to have Installer apply "
		"the WrapGuide operation to the documentation!!"))
	     (choices "WrapGuide" "Skip it")
	     (default "WrapGuide")
	     (help @askbool-help)))
        (set WrapIt
          (askbool
	     (prompt (cat
		"The KingFisher2.guide documentation that ships with "
		"KingFisher Release 2 is formatted with long lines to "
		"provide Kickstart 3.x users with more readable, "
		"paragraph oriented documentation.\n"
		"If you have pressing reasons to convert this file "
		"to a version that will not cause trouble with Kickstart "
		"2.x, then you may choose to have the Installer apply "
		"the supplied 'WrapGuide' tool to the file.\n\n"
		"For best performance under your installed version of "
		("AmigaDOS %ld, it is recommended to skip this step!" dosver)))
	     (choices "WrapGuide" "Skip it")
	     (default "Skip it")
	     (help @askbool-help))))
     (if (or (< dosver 39) WrapIt)
       ((working "\nWord-wrapping KingFisher2.guide\nfor use with AmigaGuide V34")
         (run ("stack 50000\nKFINST:WrapGuide %s 65" (tackon DESTIN "KingFisher2.guide")))
	 (message "NOTE: If AmigaGuide® crashes on KingFisher2.guide, "
		  "then the Installer did not properly call WrapGuide "
		  "on the file to fix it for KS2.x use."))))))
  (message "No new KingFisher2.guide available"))

(complete 100)
(exit)
