#
#	CompareRevisions - Compare the revision in the named file with another revision
#					of the same file.
#
#	Usage:	CompareRevisions file
#
#	CompareRevisions compares the revision of the named file with another revision
#	of the same file. If they don't match it brings them both up on the screen and
#	puts them side by side.  A menu is appended to the menu bar to go through
#	the changes.  When all the changes have been shown, the revision windows are
#	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.
#
#	To add CompareRevisions to a menu, execute the following command:
#
#		AddMenu Project 'CompareRevisions' 'CompareRevisions "{Active}" ∑∑ "{WorkSheet}"'
#
#	This script creates three temporary files which are deleted on exit or abort.
#	{ShellDirectory}Compare_File, {ShellDirectory}Compare_State and file,<rev>,
#
#
#	NOTE:
#	1. CompareRevisions calls the script CompareFiles.
#
#	© Apple Computer, Inc. 1988 - 1992
#	All rights reserved.

Set Exit 0
Set error 0
Begin

	#	Check parameters.
	
	If {#} ≠ 1
		Echo "### Usage - {0} file"
		Exit 0
	End	>> Dev:StdErr
	
	#	Make sure the file exists

	If "" == "`Exists -f "{1}"`"
		Echo "### {0} - File ∂"{1}∂" not found"
		Exit 4
	End
		
	#	Break apart the pathname.
	
	Set f "{1}"
	If "{f}" =~ /(≈)®1:(≈)®2/
		Set dir "{®1}:"
		Set f "{®2}"
	Else
		Set dir `directory`
		If "{dir}" =~ /(≈)®1:/
			Set dir "{®1}:"
		End
	End
	
	Set path "{dir}{f}"

	#	See if the file belongs to a project.
	#	Then find the name of the parent project.
	
	(Set info "`ProjectInfo "{path}"`" || Set error 1)
	(Evaluate "{info}" =~ /[¬,]+,([¬ ∂t+*∂']+)®4([+*]«0,1»)®1[∂']* ≈/
	Evaluate "{info}" =~ /≈Project: (≈∫)®2 +Checked out≈/ ) >dev:null
	If {error} ≠ 0 || "{info}" == ""
		Alert "∂"{path}∂" doesn’t belong to a project."
		Exit 0 
	End
	Set rev1 "{®4}"

	#	At this point:
	#		{®1}	=>	'', '+', or '*'
	#		{®2} 	=>	project path
	#		{path} 	=>	file pathname
	#		{rev1} 	=>	revision
	#		{dir} 	=>	file directory path
	#		{f} 	=>	file leaf name
	#
	#	Is the right project mounted?
	
	(Set revList "`ProjectInfo -s -project "{®2}" "{f}"`") ≥dev:null
	If "{revlist}" == ""
		Alert "Project ∂"{®2}∂" is not mounted."

		Exit 0 
	End
	
	#   Remove checked out ('+') revisions from the revision list.

	Loop
		If "`Evaluate " {revlist}" =~ /(≈)®7 (∂')«0,1»{f},[¬+]+∂+(∂')«0,1»(≈)®8/`"
			Set revlist "{®7}{®8}"
		Else 
			Break
		End
	End
	
	#   Remove the current revision from the revision list.

	If  ".{®1}" !~ /.[+*]/ && "`Evaluate " {revlist}" =~ /(≈[¬'])®7[∂']«0,1»{f},{rev1}[∂']«0,1»(≈)®8/`"
		Set revlist "{®7}{®8}"
	End > dev:null
	
	If "{revlist}" =~ / */
		Alert "No revisions to compare against."
		Exit 4				# we're done if the rev list is null
	End

	#	Request the number of the revision to be compared.

	(Set rev2 `GetListItem -s -r 8 -m "Compare ∂"{f},{rev1}{®1}∂" with:" -d {revList} ∂
		|| Set error {Status}`)

	If {error} == 0
		
		(Evaluate "{rev2}" =~ /≈,(≈)®7/ ) >dev:null
		Set rev2 "{®7}"

	#	Rename the active window so we can checkout a read-only copy of the
	#	revision we want to compare against.  We'll have to change the name back.
		
	#	Find a temporary directory where I can checkout the file
		Set coDir "{ShellDirectory}CompareRevisions.Dir:"
		Delete -i -y "{coDir}"
		NewFolder "{coDir}"
				
		(Checkout "{f},{rev2}" -d "{coDir}" -project "{®2}" || Set error {Status})
		If {error} ≠ 0
			Alert "∂"{f},{rev2}∂" can’t be checked out [{error}]."
			Rename "{path},{rev1}{®1}" "{path}"
			Delete -i -y "{coDir}"
			Exit 0 
		End
		
		Move "{coDir}{f}" "{path},{rev2}"
		Delete -i -y "{coDir}"

	#	Compare the two files and then clean up.
	
		CompareFiles "{path}" "{path},{rev2}" ∑ dev:null
		Set MyStatus {Status}
		If {MyStatus} == 2
			Echo Delete ∂""{path},{rev2}"∂" >> "{ShellDirectory}Compare_State"
		Else If {MyStatus} == 0
			Alert "Revisions match."
			Delete "{path},{rev2}"
		End
	End
End 
