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

(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: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."))
)

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

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


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

(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 "/#? Quiet Force"))
	       (run "delete" 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)
      ))
 )

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

(run "run" pager-cmd (cat "\"" release-note-file "\"") (safe))
(message "\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."
	 )

(complete 00) (transcript "*** Patching " app-name ".")
(complete 01) (check-user-level)
(complete 02) (check-atcp-assign)

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

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

(procedure 
 run-multi-spatch
 (working "Patching " mpatch-file history-message)
 (if (= 1 (exists env-result-file)) (delete (cat "ENV:" result-file)))
 (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)
 )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Installatation sequence
;;;
    
(set comp 8)
(complete comp) (if (= @pretend 0) (check-amitcp-version))

;; Copy new AmiTCP/IP distribution files.

(if (> (strlen current-atcp-directory) 0)
    (run "Copy patch/AmiTCP_info" (cat current-atcp-directory ".info")))

(copyfiles
 (source "bin/Installer")
 (dest bin-dir)
 (files)
 (optional force))
(set comp (+ 2 comp)) (complete comp)

(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 (+ 3 comp)) (complete comp)

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

(copyfiles
 (source "Read Me First")
 (dest atcp-assign)
 (files)
 (optional force))
(set comp (+ 2 comp)) (complete comp)

(procedure 
 create-missing-atcp-dir
 (if (= 0 (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)

(copyfiles
 (prompt "Copying new AmiTCP distribution files and binaries." history-message)
 (source "patch/amitcp")
 (dest atcp-assign)
 (files)
 (infos)
 (all)
 (optional force))

(set tmp (tackon atcp-assign ".info"))
(if (= 1 (exists tmp))
    (delete tmp (optional force)))

(set wmsg "Copied new AmiTCP files")
(update-history)
 
(set comp (+ 25 comp)) (complete comp)

;;;; Patch binaries.

(procedure 
 get-string1
 (set s (select n
		"Config AmiTCP"		"Config_AmiTCP.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 (+ 2 comp)) (complete comp)
  )

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

(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")))

(complete 100)

(message std-msg
	 "\nYour old `startnet' exists on the current"
	 " 4.3 configuration, but it will be overwritten next time you run"
	 " `Config AmiTCP'."
	 "\nActually, you should run `Config AmiTCP' and"
	 " Update system startup options if you have '020 or higher processor"
	 " in your machine."
	 "\nPlease close the drawer that contains AmiTCP icon (if open) so"
	 " that new AmiTCP drawer coordinates become active.")

(cleanup)
(exit (quiet))
; EOF
