#	CompareFiles - 
#	Compare two source files and show the differences
#
#	Version 3.3
#
#	Usage:
#		CompareFiles [-9 | -LC -13 | -16 | -Portrait | -TwoPage | -b x y | -r t,l,b,r] oldFile newFile	- open up the files and add menu
#			-9			use 9 inch screen (512 x 342)
#			-13			use 13 inch screen (640 x 480)
#			-16			use 16 inch screen (832 x 624)
#			-LC			use 12 inch screen (
#			-Portrait	use 15 inch screen (640 x 870)
#			-TwoPage	use 21 inch screen (1152 x 870)
#			-b x y		use screen dimensions given in x y
#			-r t,l,b,r	use screen rect given by Top,Left,Bottom,Right
#			default is -13
#		NOTE:  
#			Order of parameters is important
#			No error checking is done on x y coordinates
#
#	Returns:
#		Status = 0 - files match
#		Status = 1 - syntax error
#		Status = 2 - files differ  (this is the same status as the Compare tool)
#
#	CompareFiles will compare two files, and if they don't match it will 
#	bring them both up on the screen and put them side by side.  A menu will be 
#	appended to the menu bar to go through the changes.  When all the changes 
#	have been shown, the file windows will be restored and closed.
#
#	The differences are shown from bottom to top so that if any changes are made
#	in the files, the line offsets are not messed up.
#
#	This creates two temporary files {ShellDirectory}Compare_File and 
#	{ShellDirectory}Compare_State.
#	These are deleted on exit.
#
#	Copyright Apple Computer, Inc. 1988 - 1992
#	All rights reserved.
#
#	Version 3.3 by Peter Potrebic with enhancements by Greg Branche
#
#	Understanding the code…
#
#		CompareFiles is actually a set of scripts.  The action
#		depends on the parameters.  Here is what happens:
#
#		CompareFiles oldFile newFile
#			Do the actual compare
#			If there are differences, it will
#			Create a script to execute on quit
#			Move the windows around next to each other
#			Create the Compare Menu
#			Call CompareFiles-NextChange
#
#		CompareFiles•NextChange					- show next change
#		CompareFiles•Quit						- cleans up
#

Set Exit 0
Set CaseSensitive 0
Set ErrorMessage 	"### CompareFiles - Incorrect order, or missing or illegal option"
Set Usage 			"# Usage - CompareFiles [-9 | -LC | -13 | -16 | -Portrait | -TwoPage | -b x y | -r t,l,b,r] oldFile newFile"

If "{CompareFilesTileRectangle}" == ""
	Set Left 7										# default the left edge of the windows
	If "{1}" =~ /-Kong/ || "{1}" =~ /-TwoPage/		# Apple Two-Page Monochrome Monitor
		Set CompareFilesTileRectangle "0, 5, 749, 1148"
		Set Bottom 750
		Set Right 1138
		Shift 1
	else if "{1}" =~ /-SkyHawk/ || "{1}" =~ /-Portrait/	# Apple Macintosh Portrait Display
		Set CompareFilesTileRectangle "0, 5, 749, 635"
		Set Bottom 750
		Set Right 626
		Shift 1
	else if "{1}" == "-16"							# Apple 16" screen
		Set CompareFilesTileRectangle "0, 5, 482, 828"
		Set Bottom 483
		Set Right 819
		Shift 1
	else if "{1}" =~ /-13/							# MacII screen
		Set CompareFilesTileRectangle "0, 5, 344, 635"
		Set Bottom 345
		Set Right 626
		Shift 1
	else if "{1}" =~ /-LC/							# LC 12" RGB screen
		Set CompareFilesTileRectangle "0, 3, 240, 510"
		Set Bottom 241
		Set Right 500
		Shift 1
	else if "{1}" == "-9"							# Mac+ and MacSE screen
		Set CompareFilesTileRectangle "0, 3, 198, 510"
		Set Bottom 199
		Set Right 500
		Shift 1
	else if "{1}" == "-b" || "{1}" == "-B"			# user specified size
		Evaluate Right = {2} - 5
		Evaluate Bottom = {3} - 140
		Set CompareFilesTileRectangle "0,5,{Bottom},{Right}"
		Evaluate Bottom = {Bottom} + 1
		Evaluate Right = {Right} - 9
		Shift 3
	else if "{1}" == "-r" || "{1}" == "-R"			# custom user-specified rect
		(Evaluate "{2}" =~ /(≈)®1,(≈)®2,(≈)®3,(≈)®4/) >Dev:Null
		Set Top "{®1}"
		Set Left "{®2}"
		Set Bottom "{®3}"
		Set Right "{®4}"
		Evaluate Bottom = {Bottom} - 120
		Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
		Evaluate Bottom = {Bottom} + 1
		Evaluate Left = {Left} + 2
		Evaluate Right = {Right} - {Left} - 2
		Shift 2
	else											# default
		Set CompareFilesTileRectangle "0, 5, 344, 635"
		Set Bottom 345
		Set Right 626
	end
End

if {#} ≠ 2
	Echo "{ErrorMessage}"
	Echo "{Usage}"
	Exit 1
end >Dev:StdErr

Set CompareStatus 0
Compare -g 3 -b -m -t "{{1}}" "{{2}}" > "{ShellDirectory}"Compare_File || Set CompareStatus {Status}

if {CompareStatus} == 0
	Echo "### files are equal (ignoring blanks): {1} - {2}"
	Delete -i "{ShellDirectory}Compare_File" ≥ dev:null
else if {CompareStatus} == 2					# files don’t match - set everything up
	StreamEdit "{{ShellDirectory}}"Compare_File -o "{{ShellDirectory}}"Compare_File ∂
		-e '/•[ ∂t]*File (≈)®1; Line (≈)®2; File (≈)®3; Line (≈)®4/ Change "	Find "®2" "®1"; Find "®4" "®3'
	Set NothingMatches 1
	Search -s /•∂*∂*∂* Nothing seems to match ∂*∂*∂*/ "{{ShellDirectory}}"Compare_File ∑∑dev:null || Set NothingMatches 0
	If {NothingMatches}
		Echo "### nothing matches: {1} - {2}"
		Delete -i "{{ShellDirectory}}"Compare_File
		Exit 0
	End

	Set 1 "``Files -f -q "{{1}}"``"				# get full pathnames
	Set 2 "``Files -f -q "{{2}}"``"

	Unset Close1 Close2 				# remember if either file is already open
	Set openWindows " ``Windows`` "
	If "{{openWindows}}" !~ /≈ [∂']*"{{1}}"[∂']* ≈/; Open "{{1}}"; Set Close1 1; End
	If "{{openWindows}}" !~ /≈ [∂']*"{{2}}"[∂']* ≈/; Open "{{2}}"; Set Close2 1; End
	
	Begin										# create end script
		MoveWindow "{{1}}"; SizeWindow "{{1}}"
		Quote Find • "{{1}}"
		If {Close1}; Quote Close "{{1}}"; End
		MoveWindow "{{2}}"; SizeWindow "{{2}}"
		Quote Find • "{{2}}"
		If {Close2}; Quote Close "{{2}}"; End
	End > "{{ShellDirectory}}Compare_State"

	TileWindows -v -r "{CompareFilesTileRectangle}" "{{1}}" "{{2}}"
	Open -t "{{ShellDirectory}}"Compare_File
	MoveWindow {Left} {Bottom} "{ShellDirectory}"Compare_File
	SizeWindow {Right} 75 "{ShellDirectory}"Compare_File
	Find ∞ "{{ShellDirectory}}"Compare_File

	AddMenu Compare "Find Next Change/ƒ"	"CompareFiles•NextChange"
	AddMenu	Compare "(-1"					""
	AddMenu Compare "Copy Selection »»"		"Catenate ∂"{{1}}∂".§ > ∂"{{2}}∂".§; #CompareFiles•NextChange"
	AddMenu Compare "«« Copy Selection"		"Catenate ∂"{{2}}∂".§ > ∂"{{1}}∂".§; #CompareFiles•NextChange"
	AddMenu	Compare "(-2"					""
	AddMenu	Compare "Done"					"CompareFiles•Quit"

	CompareFiles•NextChange

end

Exit {CompareStatus}
