;;;; Hey Emacs, this script might as well be -*- lisp -*-
;;;;
;;;; Patch_AmiTCP_4.2 - AmiTCP/IP patch script for Installer
;;;;
;;;; Copyright © 1995 AmiTCP/IP Group,
;;;;                  NSDi - Network Solutions Development Inc., Finland
;;;;                  All rights reserved.
;;;;
;;;;
;;;; 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.2.\n")
;;;;
;;;; What we are?
;;;;
(set app-name (cat @app-name " 4.3"))

;;;
;;; Destination directories of the AmiTCP/IP
;;;
(set 
 atcp-name "AmiTCP"
 atcp-assign (cat atcp-name ":")	; Assign to AmiTCP
 conf-dir (tackon atcp-assign "db")	; Configuration
 bin-dir (tackon atcp-assign "bin")	; User binaries
 util-dir (tackon atcp-assign "util")	; System binaries
 help-dir (tackon atcp-assign "help")	; AmigaGuide documentation
 ;; 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.3beta")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(set path1 "SYS:C" 
     path2 "SYS:Utilities" 
     path3 "SYS:Rexxc"
     pathn 3)

(procedure 
 search-command
 (set n 0 
      command-path "")
 (while (< n pathn)
   ((set dir (select n path1 path2 path3)
	 n (+ n 1))
    (if (= 1 (exists (tackon dir cmd)))
	(set command-path (tackon dir cmd) 
	     n pathn))))
 (if (= command-path "")
     (abort "Command `" cmd "' Not found in "
	    path1 ", " path2 " or " path3 ".\n"
	    "\nThis is a system command shipped with operating system. "
	    "It is essential for the operation of this patch release "
	    "that command `" cmd "' is located in its default place."))
)

;(set cmd "foo") (search-command)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; copy "more" to ram: to be able to use it
(set cmd "More")
(search-command)
(set
 pager-cmd 
 ((copyfiles
   (prompt "Copying " command-path " to RAM: for use")
   (source command-path)
   (dest "RAM:")
   (safe))
  "RAM:More")
 )

;; Search ARexx program `rx' for use. 
(set cmd "rx")
(search-command)
(set rexx-command command-path)

;; 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."
	   ))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Run command ...

(set result-file "installer-result")
(set result 0)

(procedure
 run-command
 (if (= @pretend 0)
     ((delete (cat "ENV:" result-file) (optional force))
      (set result (run command-line (cat ">ENV:" result-file)))
      (set result-string (getenv result-file))
      (set len (strlen result-string))
      (if (= '\n' (substr result-string (- len 1) 1))
	  (set result-string (substr result-string 0 (- len 1)))))))

(procedure
 abort-corrupt
 (abort "For some reason installer cannot perform it's operation. "
	"The reason may be corrupted archive or insufficient resources "
	"such as lack of memory."))


(set hist1 "" hist2 "" hist3 "" hist4 "" hist5 "") 
(procedure 
 update-history
 (transcript "*** " wmsg)
 (set hist1 hist2
      hist2 hist3
      hist3 hist4
      hist4 hist5
      hist5 (cat "\n" wmsg)
      history-message (cat "\n\nHistory:" hist1 hist2 hist3 hist4 hist5)))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Check that the old AmiTCP/IP binary is really 4.2
;;;

(set amitcp-versionfile "data/amitcpversions")
(set amitcp-versionline 0)

(procedure 
 check-amitcp-version
 (set command-line (cat "bin/tmd5sum " (tackon atcp-assign "AmiTCP")))
 (run-command)
 (if (<> result 0) (abort-corrupt))
 (set ver (run rexx-command "bin/anysum" result-string amitcp-versionfile))
 (if (< ver 30) (abort-corrupt))
 (if (= ver 30)
     (abort "This patch can be installed over " app-name " only."))
 (set amitcp-versionline (- ver 31)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; MultiPatcher

(set mpatch-result-file "RAM:NSDi_patched"
     failed-count 0)

(procedure 
 run-multi-spatch
 (working "Patching " mpatch-file history-message)
 (set result (execute "bin/Patcher" rexx-command mpatch-file mpatch-sumsfile))
 (if (= result 0)
     (
      (set old-file (cat mpatch-file ".old"))
      (rename mpatch-file old-file)
      (copyfiles
       (prompt "Copying patched " mpatch-file " in place.")
       (source mpatch-result-file)
       (dest (pathonly mpatch-file))
       (newname (fileonly mpatch-file))
       (files))
      (set wmsg (cat "Patched " mpatch-file)))
; else
   (if (>= result 10)
       (set failed-count (+ 1 failed-count)
	    wmsg (cat "Could not patch " mpatch-file))
; else
     (set wmsg (cat mpatch-file " already patched."))))
 
 (update-history))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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."
	 )

(message "\n\nARE YOU SURE YOU HAVE COPIED "
	 "AmiTCP/IP 4.2 TREE TO A NEW DESTINATION DIRECTORY")

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

(complete 05) (if (= @pretend 0) (check-amitcp-version))

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

;;;; Patch binaries.

;test
;(set bin-dir "ram:bin") (set atcp-assign "ram:")

;;; AmiTCP
(set mpatch-file (tackon atcp-assign "AmiTCP"))
(select (in amitcp-versionline 0)
	(set mpatch-sumsfile "AmiTCP.sums")
	(set mpatch-sumsfile "AmiTCP.020.sums")
	)

(run-multi-spatch)
(complete 20)
(set comp 20)

(procedure 
 get-string1
 (set s (select n 
		"bin/bootpconfig"	"bootpconfig.sums"
		"bin/arp"		"arp.sums"
		"bin/ifconfig"		"ifconfig.sums"
		"bin/napsaterm"		"napsaterm.sums"
		"bin/ncftp"		"ncftp.sums"
		"bin/inetd"		"inetd.sums"
		"bin/online"		"online.sums"
		"bin/login"		"login.sums"
		"help/AmiTCP.guide"	"AmiTCP.guide.sums"
		"Config_AmiTCP"		"Config_AmiTCP.sums"
		"")))

(set n 0) (get-string1) 
(while s
  (set mpatch-file s  n (+ 1 n)) (get-string1)
  (set mpatch-sumsfile s  n (+ 1 n)) (get-string1)
  (set mpatch-file (tackon atcp-assign mpatch-file))
  (run-multi-spatch)
  (set comp (+ 10 comp))
  (complete comp))

(delete (tackon bin-dir "offline") (optional force))
(run "copy" (tackon bin-dir "online") (tackon bin-dir "offline"))

(makedir (tackon atcp-assign "debug"))

(procedure 
 get-string2
 (set s (select n 
		"Start AmiTCP"		atcp-assign
		"Start AmiTCP.info"	atcp-assign
		"parseconfig"		(tackon atcp-assign "util")
		"ifconfig.library"	(tackon atcp-assign "libs")
		"serscript.library"	(tackon atcp-assign "libs")
		"socket.library"	(tackon atcp-assign "libs")
		"debugsocket.library"	(tackon atcp-assign "debug/libs")
		"aslip.device"		"Devs:Networks"
		"")))

(set n 0) (get-string2)
(while s
  (set filename s  n (+ 1 n)) (get-string2) 
  (set ddirname s  n (+ 1 n)) (get-string2) 
  ((copyfiles
    (prompt "Copying " filename "." history-message)
    (source (tackon "data" filename))
    (dest ddirname)
    (files))
   (set wmsg (cat "Copied " filename " to " ddirname))
   (update-history)))

(rename (tackon atcp-assign "debug/libs/debugsocket.library")
	(tackon atcp-assign "debug/libs/socket.library"))

(set n 0)
(while (set s (select n
		"providers"
		"ssrx"
		""))
  ((copyfiles
    (prompt "Copying directory " s "." history-message)
    (source (tackon "data" s))
    (dest (tackon atcp-assign s))
    (files)
    (set n (+ 1 n))
    (all))
   (set wmsg (cat "Copied directory " s "."))
   (update-history)))

(complete 150)

(if (= 0 failed-count)
    (message 
     "\nAmITCP/IP 4.2 to 4.3 beta patch applied succesfully.\n")
  (message
   "\nAmITCP/IP 4.2 to 4.3 beta patch not entirely successful.\n"
   failed-count " files not patched."))

(exit (quiet))
; EOF
