#
#	MergeBranch - Merge a branch revision onto the trunk.
#
#	Usage:	MergeBranch file
#
#	MergeBranch renames the branch revision "file" to "file",branch and
#	checks out the latest revision on the revision trunk for modification.
#
#		If there are no trunk revisions newer than the branch, then the branch
#		is checked in as the latest trunk revision.
#
#		Otherwise MergeBranch brings up both revisions on the screen and
#		puts them side by side.  A menu is appended to the menu bar to assist
#		the user in the merge process.  Checkin the new trunk revision "file"
#		when you finish merging the changes in "file",branch into "file".
#
#	To add MergeBranch to a menu, execute the following command:
#
#		AddMenu Project 'MergeBranch' 'MergeBranch "{Active}" ∑∑ "{WorkSheet}"'
#
#	This script creates three temporary files which are deleted on exit or abort.
#	{ShellDirectory}Compare_File, {ShellDirectory}Compare_State and file,<rev>,
#
#	MergeBranch calls the script CompareFiles.
#
#	© Apple Computer, Inc. 1988 - 1993
#	All rights reserved.

Set Exit 0
Begin
	
	#	Check parameters.
	
	If {#} ≠ 1
		Echo "### Usage - {0} file"
		Exit 0
	End
	
	#	Make sure the file exists

	If "" == "`Exists -f -q "{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 `Files -f "{dir}{f}"`

	#	Get project information on the file.
	
	Set info1 "``projectinfo -comments "{path}"``"

	If "{{info1}}" =~ /[∂']*(≈)®1,([¬ ∂']+)®2[∂']* ≈Project: (≈∫)®3≈    Task: (≈)®4[ ]«5»Comment: (≈)®5/
		Set file "{®1}"
		Set rev "{®2}"
		Set proj "{®3}"
		Set task "{{®4}}"
		Set comment "Merged from branch {rev}.  {{®5}}"
	End
	
	#	Make sure the file is a branch revision.

	If "{rev}" !~ /≈[a-z]≈/
		Alert "File ∂"{f}∂" is not on a branch."
		Exit 4
	End
	
	#	If the branch rev is checked out for modification, check it in
	# 	and call projectinfo again to get the branch's new revision number.
	
	If "{rev}" =~ /≈∂+/ OR "{rev}" =~ /≈∂*/
		CheckIn -y "{path}" ≥dev:null				
		Set info1 "``projectinfo "{path}"``"
		If "{{info1}}" =~ /[∂']*(≈)®1,([¬ ∂']+)®2[∂']* ≈Project: (≈∫)®3≈/
			Set file "{®1}"
			Set rev "{®2}"
			Set proj "{®3}"
		End
	End
	
	#	Get projectinfo on the file's latest trunk revision.
	
	Set info2 "``projectinfo -latest -s -project "{proj}" "{f}" ≥ dev:null``"
	If "{{info2}}" == ""
		Alert "Project ∂"{proj}∂" is not mounted."		
		Exit 4
	End
	
	If "{{info2}}" =~ /[∂']*(≈)®1,([¬ ∂']+)®2[∂']*/
		Set file2 "{®1}"
		Set rev2 "{®2}"
	Else
		Alert "Latest rev of File ∂"{f}∂" is checked out for modification.  Cannot merge at this time."		
		Exit 4
	End
	
	#	Is the file open as a window?
	
	Set wind 0
	Unset CaseSensitive
	For i in `Windows`
		If "{path}" =~ /{i}/
			Set wind 1
			Break
		End
	End

	#	Rename the branch revision and checkout the trunk revision for modification.

	Set branchf "{path},{rev}"
	Rename -y "{path}" "{branchf}"
	CheckOut "{path}" -m -t "{{task}}" -cs "{{comment}}" -project "{proj}"
		
	#	If the revisions on the trunk are older than the branch rev then
	#  		checkin the branch on the trunk (we don't need to merge).
	# 	Otherwise call comparefiles and let the user do the merge and checkin.
	
	If "{rev}" =~ /{rev2}≈/
	
		Echo "Include ∂"{path}∂" 'ckid';" | Rez -m -a -o "{branchf}" -noResolve output
	
		Rename -y "{branchf}" "{path}"
		
		If "{wind}"
			If "{path}" =~ /"{active}"/
				Close "{path}"
				Open "{path}"
			Else
				Close "{path}"
				Target "{path}"
			End
		End
		
		CheckIn "{path}"

	Else
	
		If Not `Files -t TEXT "{path}"` Or `Exists -a "{path}"`
			Echo "### {0} - Unable to interactively compare non-TEXT files." ≥≥dev:stderr
			Exit 2
		End

		Open "{path}"

		CompareFiles "{branchf}" "{path}"

		If {Status} == 2
			Echo Close ∂""{branchf}"∂" >> "{ShellDirectory}Compare_State"
			Echo Delete ∂""{branchf}"∂" >> "{ShellDirectory}Compare_State"
			Exit 0
		Else If {Status} == 0
				If "{wind}"
					Close "{branchf}"
				End
				Delete "{branchf}"
				CheckOut -cancel -y "{path}"
		End
		
	End

End
