;;; 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
;;;
;test
;(message "When testing, remember to assing AmiTCP:\nto a safe place")

(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 source-dir
 )

(set release-note-file "Read Me First")

(set result-file "installer-result"
     env-result-file (cat "ENV:" result-file)
     result 0
     quiet-mode 0 ; are some operations quiet or not.
     flag 0 ; it is usually easier to set flag than try to return some value.
     )


(set total-mem (+ (database "total-mem")))
;; Memory requirements
(if (< total-mem 300000)
    (abort "To run this patch release you need at least 500 kilobytes of "
	   "memory. Please free some memory and start again"))

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

(set path1 "SYS:C"
     path2 "SYS:Utilities"
     path3 "SYS:System"
     path4 "SYS:Rexxc"
     pathn 4)

(procedure 
 search-command
 (set n 0 
      command-path "")
 (while (< n pathn)
   ((set dir (select n path1 path2 path3 path4)
	 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 ", " path3 " or " path4 ".\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."))
)

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

;;; 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)
(if (run rexx-command "'Say Make sure RexxMast is running'")
    ((set cmd "RexxMast")
     (search-command)
     (run command-path)))

;;;;;;;;;;,;;;;;;;;;;,;;;;;;;;;;,;;;;;;;;;;,;;;;;;;;;;,;;;;;;;;;;,;;;;;;;;;;

(procedure 
 cleanup

;; Clean up temporary storage (if any).
 (set len (strlen (cat tmpfile-directory)))
 (if (> len 0)
     (
      (if (> (strlen (cat old-t-assign)) 0)
	   (makeassign "t" old-t-assign))

      (if (<> ":" (substr tmpfile-directory (- len 1) 1))
	  (if (= 2 (exists tmpfile-directory))
	      ((run "delete" (cat tmpfile-directory "/#?")
		    tmpfile-directory "Quiet Force"))))
      ))
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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 "*** Patching aborted due to lacking AmiTCP: assign.")
    (abort "AmiTCP assign not found.\n"
	   "Please install AmiTCP from the AmiTCP/IP installation disk."
	   ))))

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

; At this time 400 kilobytes of memory is already used.
(set required-tmp-ramdspace 1200000
     required-tmp-tdskspace 600000
     required-tmp-diskspace 3000000)
     
(procedure 
 get-patch-tmpfile-directory
 (set tmpfile-directory "RAM:")
 (if (< total-mem required-tmp-ramdspace)
     (while (< (getdiskspace tmpfile-directory) required-tmp-diskspace)
       ((set tmpfile-directory
	     (askdir
	      (prompt "You don't have enough memory to run patch entirely on "
		      "memory.\n Please choose a disk that have at least 3 "
		      "Megabytes of free memory for temporary storage.\n"
		      "(Use `Show Drives' to choose a destination disk)")
	      (help "Only drive names are important here.\n"
		    "So drive list is all you need to choose from.")
	      (disk)
	      (default
		(if (= 2 (exists "Work:" (noreq)))
		    "Work:"
		  (cat (getdevice "SYS:") ":")))
	      ))
	(if (< (getdiskspace tmpfile-directory) required-tmp-diskspace)
	    (message "Not enough disk space on chosen disk.\n"
		     "Please choose another.")))))
 (set tmpfile-directory (cat (getdevice tmpfile-directory) ":NSDI_pch"))
 (makedir tmpfile-directory)
 (if (< total-mem required-tmp-tdskspace)
     ((set old-t-assign (getassign "t"))
      (makeassign "t" tmpfile-directory)
      ))
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set comp8 0)

(procedure
 add-complete
 (set comp8 (+ comp comp8))
 (complete (shiftright comp8 3)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Installatation initialization
;;;

(set release-note-msg
     (cat "\nThis patch release patches AmiTCP/IP 4.1 or 4.2 to \n"
     "AmiTCP/IP Pro version 4.3\n"
     "\n    Please read the release notes from the patch archive. "
     "They contain valuable info you cannot afford to miss."))

(working release-note-msg)
(run "run" pager-cmd (cat "\"" release-note-file "\"") (safe))

(transcript "*** Patching " app-name ".")
(check-user-level)
(check-atcp-assign)
(set comp 8) (add-complete)

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

(get-patch-tmpfile-directory)
(onerror (cleanup))

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

(procedure 
 pathname
 (set len (strlen pname))
 (if (<> ":" (substr pname (- len 1) 1))
     (set pname (cat pname "/")))
 )

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


(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.1 or 4.2
;;;

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

(procedure 
 check-amitcp-version
 (set result
      (execute "bin/chkatcptype" rexx-command))
 (if (<> result 0)
     (abort (getenv "installer-result")
	    "\nPlease install correct AmiTCP enviroment and/or "
	    "this patch release archive."))
 )
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; copy files only if different.
;;;

(procedure 
 compare-and-copy
 (set source-file (cat "\"" source-file "\"")
      dest-file (cat "\"" dest-file "\""))
 (if (< 0 (run "bin/cmp" source-file dest-file))
     ((if (= 0 quiet-mode) (working release-note-msg "\n\n Copying " dest-file))
      (execute "bin/forcecopy" source-file dest-file)
      (if (= 0 quiet-mode) (working release-note-msg)))))

(procedure
 copy-dir2
 (foreach
  copy-source2 "#?"
  ((set source-file (tackon copy-source2 @each-name)
	dest-file (tackon copy-dest2 @each-name)
	)
   (compare-and-copy)
   (add-complete))
   ))

(procedure
 copy-dir1
 (set comp 8) ; stays the same all the time.
 (foreach
  copy-source "#?"
  ((set source-file (tackon copy-source @each-name)
	dest-file (tackon copy-dest @each-name))
   (if (< @each-type 0)
       ((compare-and-copy)
	(add-complete))
     ((set copy-source2 source-file
	   copy-dest2 dest-file)
      (copy-dir2))))))

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

(set mpatch-result-file (tackon tmpfile-directory "NSDi_patched")
     failed-count 0)

(procedure 
 run-multi-spatch
 (working "Patching " mpatch-file history-message)
 (set pname tmpfile-directory)
 (pathname)
 (set result 
      (execute "bin/Patcher" rexx-command pname 
	       (cat "\"" 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))
      (delete old-file (optional force))
      (set wmsg (cat "Patched " mpatch-file)))
; else
   (if (>= result 10)
       ((set failed-count (+ 1 failed-count)
	     wmsg (cat "Could not patch " mpatch-file))
	(if (= 1 (exists env-result-file))
	    (transcript "*** Patch failed: " (getenv result-file))))
; else
     (set wmsg (cat mpatch-file " already patched."))))
 
 (update-history)
 )


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

(procedure 
 run-patch
 (if (= 1 (exists patch-file))
     ((execute "bin/forcecopy" patch-file mpatch-result-file)
      (run "bin/patch -N" mpatch-result-file "<" patchdata-file)
      (set source-file mpatch-result-file
	   dest-file patch-file)
      (compare-and-copy))
   (transcript "*** File" patch-file "does not exists. Can not patch.")
   ))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Installatation sequence
;;;
    
(if (= @pretend 0) (check-amitcp-version))

;; Copy new AmiTCP/IP distribution files.

(if (> (strlen current-atcp-directory) 0)
    ((set source-file "patch/AmiTCP_info" 
	  dest-file (cat current-atcp-directory ".info"))
     (compare-and-copy)))

(set source-file "bin/Installer"
     dest-file (tackon bin-dir "Installer"))
(compare-and-copy)

(set comp 12) (add-complete)

(copylib
 (prompt "Do you wan't to copy latest SANA-II slip device `aslip.device' "
	 "to Devs:Networks?")
 (help "Aslip.device replaces rh[c]slip devices you currently may be using."
       "\nIf you use slip interface as your network connectionn you have to "
       "install this device since new AmiTCP/IP configuration requires it.")
 (source "patch/aslip.device")
 (dest "Devs:Networks")
 (confirm "average"))
(set comp 12) (add-complete)

(transcript "*** Deleting obsolete AmiTCP distribution files.")
(execute "bin/rmsomefiles")

(set source-file "Read Me First"
     dest-file (tackon atcp-assign source-file))
(compare-and-copy)

(set comp 12) (add-complete)

(procedure 
 create-missing-atcp-dir
 (if (= 0 (exists (tackon atcp-assign new-atcp-dir)))
     (makedir (tackon atcp-assign new-atcp-dir))))

(set new-atcp-dir "util") (create-missing-atcp-dir)
(set new-atcp-dir "ssrx") (create-missing-atcp-dir)
(set new-atcp-dir "providers") (create-missing-atcp-dir)

(set copy-source "patch/amitcp"
     copy-dest atcp-assign)
(copy-dir1)

(if (NOT (IN (protect (tackon bin-dir "stopnet")) 6))
    (protect (tackon bin-dir "stopnet") "+s"))

(if (NOT (IN (protect (tackon bin-dir "SynClock")) 6))
    (protect (tackon bin-dir "SynClock") "+s"))

(set wmsg "Copied changed AmiTCP files")
(update-history)
 
;;;; Patch binaries.

(procedure 
 get-string1
 (set s (select n
		"Config AmiTCP"		"Config_AmiTCP.sums"
		"AmiTCP"		"AmiTCP.000.sums"
		"AmiTCP.020"		"AmiTCP.020.sums"
		"help/AmiTCP.guide"	"AmiTCP.guide.sums"
		"bin/arp"		"arp.sums"
		"bin/askhost"		"askhost.sums"
		"bin/bootpconfig"	"bootpconfig.sums"
		"bin/finger"		"finger.sums"
		"bin/hostname"		"hostname.sums"
		"bin/id"		"id.sums"
		"bin/ifconfig"		"ifconfig.sums"
		"bin/inetd"		"inetd.sums"
		"bin/letnet"		"letnet.sums"
		"bin/login"		"login.sums"
		"bin/ls"		"ls.sums"
		"bin/napsaterm"		"napsaterm.sums"
		"bin/ncftp"		"ncftp.sums"
		"bin/online"		"online.sums"
		"bin/passwd"		"passwd.sums"
		"bin/ping"		"ping.sums"
		"bin/portmap"		"portmap.sums"
		"bin/resolve"		"resolve.sums"
		"bin/route"		"route.sums"
		"bin/rpcinfo"		"rpcinfo.sums"
		"bin/rsh"		"rsh.sums"
		"bin/showmount"		"showmount.sums"
		"bin/traceroute"	"traceroute.sums"
		"bin/umask"		"umask.sums"
		"bin/whoami"		"whoami.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 14) (add-complete)
  )

(set quiet-mode 1)

(set source-file (tackon bin-dir "online")
     dest-file (tackon bin-dir "offline"))
(compare-and-copy)

; Patching old startnet to use .020 version of AmiTCP if CPU is >= 68020
(set cpu-type (database "cpu"))
(if (AND (<> cpu-type "68000")
	 (<> cpu-type "68010"))
    ((set patch-file (tackon bin-dir "startnet")
	  patchdata-file "patch/startnet.patch")
     (run-patch)

     (if (NOT (IN (protect patch-file) 6))
	 (protect patch-file "+s"))
     ))

(set comp 8)(add-complete)

(set patch-file (tackon conf-dir "interfaces")
     patchdata-file "patch/interfaces.patch")
(run-patch)

(set comp 8)(add-complete)

(if (= 0 failed-count)
    (set std-msg "\nAmiTCP/IP 4.3 patch applied succesfully.\n")
  (set std-msg (cat "\nAmITCP/IP 4.3 patch not entirely successful.\n"  
		    failed-count " files not patched.\n")))

(message std-msg
	 "\nYour old `startnet' exists on the current"
	 " 4.3 configuration, but it will be overwritten next time you run"
	 " `Config AmiTCP'."
	 "\nPlease close the drawer that contains AmiTCP icon (if open) so"
	 " that new AmiTCP drawer coordinates become active.")

(cleanup)
(exit (quiet))
; EOF
