;;;; Hey Emacs, this script might as well be -*- lisp -*-
;;;;
;;;; Patch_AmiTCP_4.1 - AmiTCP/IP patch script for Installer
;;;;
;;;; Copyright © 1995 AmiTCP/IP Group,
;;;;                  NSDi - Network Solutions Development Inc., Finland
;;;;                  All rights reserved.
;;;;
;;;; $Id: Patch_AmiTCP,v 4.4 1995/01/18 00:03:24 ppessi Exp $
;;;;
;;;; This script has been tested with Installer 1.24:
;;;;
;;;;     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.
;;;;
;;;; Use following Icon tooltypes / Command line options:
;;;; APPNAME=AmiTCP/IP
;;;; MINUSER=AVERAGE
;;;;
(welcome "    Welcome to the patch of " @app-name " 4.1.\n")
;;;;
;;;; What we are?
;;;;
(set app-name (cat @app-name " 4.1"))

;;;
;;; Destination directories of the AmiTCP/IP
;;;
(set 
 atcp-name "AmiTCP"
 atcp-assign (cat atcp-name ":")	; Assign to AmiTCP
 ;; Configuration
 conf-dir (tackon atcp-assign "db")
 ;; User binaries
 bin-dir (tackon atcp-assign "bin")
 ;; AmigaGuide documentation
 help-dir (tackon atcp-assign "help")
 ;; The source directory name
 source-dir (if (= 1 (exists @icon))
		    (pathonly (expandpath @icon))
		  (expandpath @icon))
 ;; Destination file
 @default-dest "t:"
 )

(set 
 release-note-file "Release_Notes_4.2")

;;; copy "more" to ram: to be able to use it
(set
 pager-cmd 
 (if (exists "SYS:Utilities/More" (noreq))
     ((copyfiles
       (prompt "Copying sys:utilities/more to ram: for use")
       (source "SYS:Utilities/More")
       (dest   "RAM:")
       (safe)
       (optional "nofail"))
      "RAM:More")
   ("more"))
 )

;; Restore old version on error
(onerror
 0) ; not implemented yet

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(procedure
 check-user-level
 ;;
 (transcript "Checking user level.")
 (if (< @user-level 1)
     ((transcript "Installation aborted due to too low user level.")
      (abort "AmiTCP/IP installation requires at least the \"average\" "
	     "user level. Restart installation and select appropriate user "
	     "level."))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(procedure
 check-atcp-assign
 ;; Check that AmiTCP is installed
 (transcript "Checking for already installed AmiTCP.")
 (if (exists atcp-assign (noreq))
     (set current-atcp-directory (getassign atcp-name))
   ((transcript "Installation aborted due to lacking AmiTCP: assign.")
    (abort "AmiTCP/IP configuration requires that AmiTCP/IP is installed on "
	   "your system. The configuration tool could not find the AmiTCP: "
	   "assign, so the configuration cannot be completed. "
	   "Please install AmiTCP: from the AmiTCP/IP installation disk."
	   ))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Check that the old AmiTCP/IP binary is really 4.1
;;;
(procedure 
 check-amitcp-41
 (set vernum (getversion (tackon atcp-assign "AmiTCP")))
 (set ver (shiftright vernum 16))
 (set rev (bitand vernum 65535))
 (if (or (<> ver 4) (<> rev 1))
     (abort "This patch can be installed over " app-name " only.")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Patch a file if its checksum matches 
;;;
(procedure 
 patch-file
 ;;(file-name patch-file-name)
 (set 
  full-file-name (tackon atcp-assign file-name)
  temp-file-name (tackon "t:" (fileonly file-name)))

 (if (exists full-file-name)
     (
      (copyfiles 
       (source full-file-name)
       (dest "t:")
       (newname (fileonly file-name))
       (safe))
      (set patch-result
	   (run (cat "patch"
		     " " temp-file-name 
		     " \"" (tackon source-dir patch-file-name) "\""
		     ) (safe)))
      (transcript ("patch result code %ld" patch-result))
      (if (= patch-result 0)
	  (
	   (set rename-result 
		(rename full-file-name (cat full-file-name ".old")))
	   (transcript ("Rename results %ld" rename-result))
	   (if (<> rename-result 0)
	       (copyfiles
		(prompt "Copying " temp-file-name " as " full-file-name)
		(files)
		(source temp-file-name)
		(dest (pathonly full-file-name))
		(newname (fileonly full-file-name))
		(optional "nofail"))
	     (message "Cannot patch " full-file-name ".\nContinue patch?")))
	(message "Cannot patch " full-file-name ".\nContinue patch?"))
      (delete temp-file-name (safe)))
   (message "Cannot patch: " full-file-name " does not exist.\nContinue patch?"))
 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SAS-Patch a file if its checksum matches 
;;;
(procedure 
 spatch-file-1
 ;;(file-name checksum spatch-file-name old-version)
 (set full-file-name (tackon atcp-assign file-name))

 (if (exists full-file-name)
     (
      (working "\nCalculating checksum on " full-file-name 
	       "\nthis may take some time.")
      (set real-sum (getsum full-file-name))
      (transcript full-file-name (" sum is $%08lx" real-sum))
      (if (= real-sum checksum)
	  (
	   (set temp-file-name (tackon "t:" (fileonly file-name)))
	   (set spatch-result
		(run (cat "spatch"
			  " -o" temp-file-name 
			  " -p" spatch-file-name
			  " " full-file-name) 
		     (safe)))
	   (if (and (= spatch-result 0)
		    (rename
		     full-file-name
		     (cat full-file-name ".old")))
	       ((copyfiles
		 (prompt "Copying " temp-file-name " as " full-file-name)
		 (files)
		 (source temp-file-name)
		 (dest (pathonly full-file-name))
		 (newname (fileonly full-file-name))
		 (optional "nofail")))
	     (message "Cannot spatch " full-file-name ".\nContinue patch?"))
	   (delete temp-file-name (safe)))
	(message "File " full-file-name " is not original.\nContinue patch?")))
      (message "Cannot patch: " full-file-name " does not exist.\nContinue patch?"))
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SAS-patch a file if its checksum matches 
;;;
(procedure 
 spatch-file-2
 ;;(file-name checksum-1 spatch-1 checksum-2 spatch-2 old-version)
 (set full-file-name (tackon atcp-assign file-name))

  (if (exists full-file-name)
     (
      (working "\nCalculating checksum on " full-file-name)
      (set real-sum (getsum full-file-name))
      (if (= real-sum checksum-1)
	  (
	   (set temp-file-name (tackon "t:" (fileonly file-name)))
	   (set spatch-result
		(run (cat "spatch"
			  " -o" temp-file-name 
			  " -p" spatch-1
			  " " full-file-name) (safe)))
	   (if (and (= spatch-result 0)
		    (rename
		     full-file-name
		     (cat full-file-name ".old")))
	       ((copyfiles
		 (prompt "Copying " temp-file-name " as " full-file-name)
		 (files)
		 (source temp-file-name)
		 (dest (pathonly full-file-name))
		 (newname (fileonly full-file-name))
		 (optional "nofail")))
	     (message "Cannot spatch " full-file-name ".\nContinue patch?"))
	   (delete temp-file-name (safe)))
	(if (= real-sum checksum-2)
	    (
	     (set temp-file-name (tackon "t:" (fileonly file-name)))
	     (set spatch-result
		  (run (cat "spatch"
			    " -o" temp-file-name 
			    " -p" spatch-2
			    " " full-file-name) (safe)))
	     (if (and (= spatch-result 0)
		      (rename
		       full-file-name
		       (cat full-file-name ".old")))
		 ((copyfiles
		   (prompt "Copying " temp-file-name " as " full-file-name)
		   (files)
		   (source temp-file-name)
		   (dest (pathonly full-file-name))
		   (newname (fileonly full-file-name))
		   (optional "nofail")))
	       (message "Cannot spatch " full-file-name ".\nContinue patch?"))
	     (delete temp-file-name (safe))))
	(message "File " full-file-name " is not original.\nContinue patch?")))
    (message "Cannot patch: " full-file-name " does not exist.\nContinue patch?"))
  )

	    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Installatation sequence
;;;
(run (cat "run " pager-cmd) release-note-file (safe))
(message "    Please read the release notes from the patch archive. "
	 "They contain valuable info you cannot afford to miss."
	 )

(complete 00) (transcript "Patching " app-name ".")
(complete 01) (check-user-level)
(complete 04) (check-atcp-assign)
(set @default-dest current-atcp-directory)

(complete 05) (check-amitcp-41)

(while (<> 0 (getversion "bsdsocket.library" (resident)))
  (message "\nYou are running AmiTCP/IP.\n"
	   "Please stop it before continuing patch with\n"
	   "`stopnet' command"))

;;; login
(set
 file-name "bin/login"
 checksum  $38A547A3
 spatch-file-name "login.pch")
(spatch-file-1)
(complete 20)

;;; online
(set
 file-name "bin/online"
 checksum  $8E2B0B3F
 spatch-file-name "online.pch")
(spatch-file-1)
(complete 30)

;;; bootpconfig
(set
 file-name "bin/bootpconfig"
 checksum  $AA9444B1
 spatch-file-name "bootpconfig.pch")
(spatch-file-1)
(complete 40)

;;; AmiTCP.guide
(set
 file-name "help/AmiTCP.guide"
 checksum  $AA49F582
 spatch-file-name "AmiTCP.guide.pch")
(spatch-file-1)
(complete 50)

;;; Config_AmiTCP
(set
 file-name "Config_AmiTCP"
 checksum  $65F5DAEB
 spatch-file-name "Config_AmiTCP.pch")
(spatch-file-1)
(complete 60)

;;; Interfaces
(set 
 file-name "db/interfaces"
 patch-file-name "interfaces.patch")
(patch-file)
(complete 70)

;;;; AmiTCP.020
(set at-020-name (tackon atcp-assign "AmiTCP.020"))
(set at-000-name (tackon atcp-assign "AmiTCP.000"))

(set
 file-name (if (exists at-020-name) "AmiTCP.020" "AmiTCP.000")
 checksum-2 $e7301a5e
 spatch-2 "AmiTCP.020.pch"
 checksum-1 $68e7efd4
 spatch-1 "AmiTCP.000.pch")
(spatch-file-2)
(complete 85)

;;;; AmiTCP(.000)
(set
 file-name "AmiTCP"
 checksum-1 $68e7efd4
 spatch-1 "AmiTCP.000.pch"
 checksum-2 $e7301a5e
 spatch-2 "AmiTCP.020.pch")
(spatch-file-2)
(complete 100)				; All done!

(message 
 "\nAmITCP/IP 4.1 to 4.2 patch applied succesfully.\n")

(exit (quiet))
; EOF
