;Script to apply patches
;$VER: Patcher 5.03 (1.9.92)
;Copyright © 1992 CONSULTRON.  All right reserved
; Use of this script in commercial products is expressly forbidden without
; written permission.
;
; The program LPatch is provided by SAS Institute.  They retain all copyrights
; to that program.  It is available for commercial distribution only to those
; users who have purchased their SAS C Compiler and are registered with them.

;***************************************************************************

;Copy a new file to the product disk
;Input: newfile (path and name of new file)

(procedure copynew
	(
		(copyfiles
			(source
				(cat version
					(cat "/" newfile)
				)
			)
			(dest
				(pathonly
					(cat prod newfile)
				)
			)
		)
	)
)

;***************************************************************************

;Perform an LPatch
;Input: file (path and file to patch)

(procedure patch
	(
		(set patchfile
			(cat version
				(cat "/" file)
			)
		)
		(set prodfile
			(cat prod file)
		)
		(set prodfile
			(substr prodfile 0
				(-
					(strlen prodfile)
					4
				)
			)
		)

		(set tempdir "ram:")
		(set temp (cat tempdir (fileonly prodfile)))
		(run
			(cat lpatch " "
				(cat "-o"
					(cat temp
						(cat " -p"
							(cat patchfile
								(cat " " prodfile)
							)
						)
					)
				)
			)
		)

		;Replace original file with temp file
		(if
			(>
				(getsize temp)
				10
			)
			(
				(copyfiles
					(source temp)
					(dest
						(pathonly prodfile)
					)
					(newname
						(fileonly prodfile)
					)
				)
			)
		)

		;Delete temporary file
		(delete temp)

	)
)

;***************************************************************************
;***************************** MAIN ****************************************
;***************************************************************************

;This can be replaced by checks for "copy of CrossDOS_Plus" etc.
(set prod "CrossDOS_Plus:")

;Starting version number
(set version 501)

;Force user level to 1 or 2. This is unfortuntely required because otherwise
;we can't present a menu of choices to the user.
(if
	(= @user-level 0)
	(
		(user 1)
		(set @user-level 0)
	)
)

; Put diag/LPatch into ram:.  Since this program is to be used quite frequently
(set lpatch "ram:LPatch")
(copyfiles
	(source (cat prod "diag/LPatch"))
	(dest
		(pathonly lpatch)
	)
	(newname
		(fileonly lpatch)
	)
)

;Apply the LPatch stuff
(while
	(exists
		(cat "" version)
	)
	(
		(foreach
			(cat "" version)
			"#?"
			(
				(if
					(< @each-type 0)	;check for file
					(
						(if
							(= ".pch"
								(substr @each-name
									(-
										(strlen @each-name)
										4
									)
								)
							)
							(
								;Apply patches with LPatch
								(set file @each-name)
								(patch)
							)
							(
								;Copy the file directly
								(set newfile @each-name)
								(copynew)
							)
						)
					)
					(
						;@each-name is a directory
						(set dir @each-name)

						(foreach
							(cat version
								(cat "/" dir)
							)
							"#?"
							(
								(if
									(< @each-type 0)	;check for file
									(
										(if
											(= ".pch"
												(substr @each-name
													(-
														(strlen @each-name)
														4
													)
												)
											)
											(
												;Apply patches with LPatch
												(set file
													(tackon dir @each-name)
												)
												(patch)
											)
											(
												;Copy the file directly
												(set newfile
													(tackon dir @each-name)
												)
												(copynew)
											)
										)
									)
									(
										;@each-name is a directory
										(set dir2
											(tackon dir @each-name)
										)

										(foreach
											(cat version
												(cat "/" dir2)
											)
											"#?"
											(
												(if
													(< @each-type 0)	;check for file
													(
														(if
															(= ".pch"
																(substr @each-name
																	(-
																		(strlen @each-name)
																		4
																	)
																)
															)
															(
																;Apply patches with LPatch
																(set file
																	(tackon dir2 @each-name)
																)
																(patch)
															)
															(
																;Copy the file directly
																(set newfile
																	(tackon dir2 @each-name)
																)
																(copynew)
															)
														)
													)
												)
											)
										)
									)
								)
							)
						)
					)
				)
			)
		)

		(set version
			(+ version 1)
		)
	)
)

(set version
	(- version 1)
)

;Delete temporary LPatch file
(delete lpatch)

(message
	(cat "Your CrossDOS Plus disk has been updated to version 5."
		(cat
			(substr
				(cat "" version)
				1
			)
			"."
		)
	)
)

(exit
	(quiet)
)
