#	CompareFiles - 
#	Compare two source files and show the differences
#
#	Version 3.2.2
#
#	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.
#
#
#	Understanding the code…
#
#		CompareFiles is actually a few scripts rolled into one.  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 itself with -n
#		CompareFiles -n							- show next change
#		CompareFiles -old						- Paste change from old into new
#		CompareFiles -new						- Paste change from new into old
#		CompareFiles -q							- execute quit script
#

Set Exit 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"

#	Get rid of display spec if entering the script from a menu command
#	This is to allow aliasing of "CompareFiles" to "CompareFiles screenSpec"
If	((								∂
		   "{1}" =~ /-b/ 			∂
	)								∂
	&&								∂
	(								∂
		   "{4}" =~ /-n/ 			∂
		|| "{4}" =~ /-old/ 			∂
		|| "{4}" =~ /-new/ 			∂
		|| "{4}" =~ /-q/ 			∂
	))

	Shift 3							# Remove "-b x y" from command line parameters

Else If	((							∂
		   "{1}" =~ /-r/ 			∂
	)								∂
	&&								∂
	(								∂
		   "{3}" =~ /-n/ 			∂
		|| "{3}" =~ /-old/ 			∂
		|| "{3}" =~ /-new/ 			∂
		|| "{3}" =~ /-q/ 			∂
	))

	Shift 2							# Remove "-r t,l,b,r" from command line parameters

Else If								∂
    ((								∂
		   "{1}" =~ /-9/ 			∂
		|| "{1}" =~ /-LC/ 			∂
		|| "{1}" =~ /-13/ 			∂
		|| "{1}" =~ /-16/			∂
		|| "{1}" =~ /-Portrait/		∂
		|| "{1}" =~ /-SkyHawk/ 		∂
		|| "{1}" =~ /-TwoPage/ 		∂
		|| "{1}" =~ /-Kong/ 		∂
	) 								∂
    &&								∂
    ( 								∂
		   "{2}" =~ /-n/ 			∂
		|| "{2}" =~ /-old/ 			∂
		|| "{2}" =~ /-new/ 			∂
		|| "{2}" =~ /-q/ 			∂
	))

   Shift
  
End

if "{1}" == "-n"				#### Find next change				
	Find ¡1	"{ShellDirectory}"Compare_File				# get past current line
	Set MyStatus {Status}		
	if {MyStatus} != 0
		Exit {MyStatus}
	end
	Find \###\ "{ShellDirectory}"Compare_File
	if {Status} != 0									# end of change log
		Beep
		Confirm "No more changes.  Are you done comparing these files?"
		if {Status} == 0
			DeleteMenu Compare
			Close -n "{ShellDirectory}Compare_File" ≥ dev:null
			"{ShellDirectory}Compare_State" ≥ dev:null
			Delete "{ShellDirectory}Compare_File" ≥ dev:null
			Delete "{ShellDirectory}Compare_State" ≥ dev:null
		end
	else
		Find !1 "{ShellDirectory}"Compare_File			# select entire line
		Mark -y § Current_Line "{ShellDirectory}"Compare_File
		Execute "{ShellDirectory}Compare_File.§"
	end
else if "{1}" == "-q"									#### Quit
	DeleteMenu Compare
	Close -n "{ShellDirectory}Compare_File" ≥ dev:null
	"{ShellDirectory}Compare_State" ≥ dev:null
	Delete "{ShellDirectory}Compare_File" ≥ dev:null
	Delete "{ShellDirectory}Compare_State" ≥ dev:null
else if "{1}" == "-old"									#### Copy selection from old to new (target to active)
	Find Current_Line "{ShellDirectory}"Compare_File	# re-sync the two files
	Set MyStatus {Status}	 
	if {MyStatus} != 0
		Exit {MyStatus}
	end
	Execute "{ShellDirectory}Compare_File.§"
	Set selChars `Position -c "{Target}"`				# see if we need to copy/paste or just cut
	(Evaluate {selChars} =~ /([0-9]+)®1,([0-9]+)®2/ ) ∑ dev:null
	if {®1} == {®2}
		Cut § "{Active}"
	else
		Copy § "{Target}"
		Mark -y § Compare_Mark "{Active}"
		Paste § "{Active}"
		Find Compare_Mark "{Active}"
		Unmark Compare_Mark "{Active}"
	end
else if "{1}" == "-new"									#### Copy selection from new to old (active to target)
	Find Current_Line "{ShellDirectory}"Compare_File	# re-sync the two files
	Set MyStatus {Status}	 
	if {MyStatus} != 0
		Exit {MyStatus}
	end
	Execute "{ShellDirectory}Compare_File.§"
	Set selChars `Position -c "{Active}"`				# see if we need to copy/paste or just cut
	(Evaluate {selChars} =~ /([0-9]+)®1,([0-9]+)®2/ ) ∑ dev:null
	if {®1} == {®2}
		Cut § "{Target}"
	else
		Copy § "{Active}"
		Mark -y § Compare_Mark "{Target}"
		Paste § "{Target}"
		Find Compare_Mark "{Target}"
		Unmark Compare_Mark "{Target}"
	end
else													#### Compare the files and set up menu
	Set Left 7											# default the left edge of the windows
	if "{1}" == "-b" || "{1}" == "-B"					# user specified size
		if {#} != 5
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr   
		Evaluate Right = {2} - 5
		Evaluate Bottom = {3} - 140
		Set TileRectangle "0,5,{Bottom},{Right}"
		Evaluate Bottom = {Bottom} + 1
		Evaluate Right = {Right} - 9
		Shift 3
	else if "{1}" == "-r"|| "{1}" == "-R"				# custom user-specified rect
		if {#} != 4
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr
		(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 TileRectangle "{Top}, {Left}, {Bottom}, {Right}"
		Evaluate Bottom = {Bottom} + 1
		Evaluate Left = {Left} + 2
		Evaluate Right = {Right} - {Left} - 2
		Shift 2
	else if "{1}" == "-9"								# Mac+ and MacSE screen
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 3, 198, 510"
		Set Bottom 199
		Set Right 500
		Shift 1
	else if "{1}" =~ /-LC/								# LC 12" RGB screen
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 3, 240, 510"
		Set Bottom 241
		Set Right 500
		Shift 1
	else if "{1}" == "-13"								# MacII screen
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 5, 344, 635"
		Set Bottom 345
		Set Right 626
		Shift 1
	else if "{1}" == "-16"								# Apple 16" screen
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 5, 482, 828"
		Set Bottom 483
		Set Right 819
		Shift 1
	else if "{1}" =~ /-SkyHawk/ || "{1}" =~ /-Portrait/	# Apple Macintosh Portrait Display
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 5, 749, 635"
		Set Bottom 750
		Set Right 626
		Shift 1
	else if "{1}" =~ /-Kong/ || "{1}" =~ /-TwoPage/		# Apple Two-Page Monochrome Monitor
		if {#} != 3
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 5, 749, 1148"
		Set Bottom 750
		Set Right 1138
		Shift 1
	else												# MacII screen (default)
		if {#} != 2
			echo "{ErrorMessage}"
			echo "{Usage}"
			Exit 1
		end > Dev:StdErr	
		Set TileRectangle "0, 5, 344, 635"
		Set Bottom 345
		Set Right 626
	end
	Compare -b -m "{1}" "{2}" > "{ShellDirectory}"Compare_File
	Set MyStatus {Status}
	if {MyStatus} == 2									# Files don't match - set everything up
		Set 1 "`Files -f -q "{1}"`"
		Set 2 "`Files -f -q "{2}"`"
		Set openFile1 0
		Set openFile2 0
		for i in  `windows`								# Remember if either file is already open
			if "{i}" == "{1}"
				Set openFile1 1
			else if "{i}" == "{2}"
				Set openFile2 1
			end
		end
		if {openFile1} == 0
			Open "{1}"
		end
		if {openFile2} == 0
			Open "{2}"
		end 
		Begin											# Create end script
			MoveWindow "{1}"							# Get the current window states
			SizeWindow "{1}"
			MoveWindow "{2}"
			SizeWindow "{2}"
			if {openFile1} == 0
				Echo Close ∂""{1}"∂"
			end
			if {openFile2} == 0
				Echo Close ∂""{2}"∂"
			end
		End > "{ShellDirectory}"Compare_State
		AddMenu Compare 'Find Next Change/ƒ' 'CompareFiles "-n"'
		AddMenu Compare '(-1' ''
		AddMenu Compare 'Copy Selection »»' 'CompareFiles "-old"'
		AddMenu Compare '«« Copy Selection ' 'CompareFiles "-new"'
		AddMenu Compare '(-2' ''
		AddMenu	Compare 'Done'	'CompareFiles "-q"'
		Open "{ShellDirectory}"Compare_File
		Find ∞ "{ShellDirectory}"Compare_File
		TileWindows -v -r "{TileRectangle}" "{1}" "{2}"
		MoveWindow {Left} {Bottom} "{ShellDirectory}"Compare_File
		SizeWindow {Right} 75 "{ShellDirectory}"Compare_File
		CompareFiles "-n"
	else if {MyStatus} == 0
		Echo "### Files are equal: {1} - {2}"
		Delete -i "{ShellDirectory}Compare_File" ≥ dev:null
	end > Dev:StdOut

	Exit "{MyStatus}"
end
