: merge - three-way file merge
: '$Id: merge.sh,v 5.3 1990/11/01 05:03:32 eggert Exp $'

DIFF=/usr/local/bin/diff
DIFF3=/usr/local/bin/diff3
PATH=/bin:/usr/bin
labels=0 p=w say=echo
while
	case $1 in
	-p)
		p='1,$p';;
	-q)
		say=:;;
	-L)
		case $labels in
		0) l1=$2 labels=1;;
		1) l3=$2 labels=2;;
		*) echo "merge: too many -L options"; exit 2
		esac
		case $# in
		1) ;;
		*) shift
		esac;;
	-*)
		echo "merge: $1: unknown option"; exit 2;;
	*)
		break
	esac
do shift
done

case $# in
3) ;;
*)
	echo >&2 'merge: usage: merge [-p] [-q] [-L label1 [-L label3]] file1 file2 file3'
	exit 2
esac

f1=$1 f2=$2 f3=$3
case $1 in +*) f1=./$1;; esac
case $2 in +*|-*) f2=./$2;; esac
case $3 in +*|-*) f3=./$3;; esac

case $labels in
0) l3=$3 l1=$1;;
1) l3=$3
esac

case $p in
w)
	if test ! -w "$f1"
	then
		echo >&2 "merge: $1 not writeable"
		exit 2
	fi
esac

status=2
temps=
trap '
	case $temps in
	?*) rm -f $temps || status=2
	esac
	exit $status
' 0
trap exit 1 2 3 13 15
umask 077

t=/tmp/d3t$$

case $p in
w) temps=$t;;
*) t=
esac

$DIFF3 -am -L "$l1" -L "$l3" "$f1" "$f2" "$f3" >$t
s=$?

case $s in
0) ;;
1) $say >&2 "merge: overlaps during merge";;
*) exit
esac

case $p in
w) cp $t "$f1" || s=2
esac

status=$s
