#!/bin/sh
#	u4merge, A script to allow using p4winmrg from Linux via Win4Lin
#
#	questions or comments to William Croft, croft@mitem.com

if [ $# -eq 0 ]
then
	cat <<-EOF
	usage: u4merge base_file their_file your_file merge_file
	usage: u4merge install
	
	"u4merge" is a script to allow using p4winmrg from Linux via Win4Lin.
	Install u4merge in your private or public bin directory. Then FIRST TIME
	ONLY, type "u4merge install", this will append to your win4lin
	~/win/autoexec.bat and put a symlink in ~/mydata. You will need to reboot
	your win4lin to pickup these changes.

	Someplace in your perforce setup put "export P4MERGE=u4merge" Now when
	you do a "p4 resolve" and type "m", u4merge will run, and it creates a
	batch file ~/mydata/u4m.bat.  In your win4lin, in a dos box, now type
	"u4m".  This will start up p4winmrg with the appropriate files.  Save
	your merge file when finished, and reply to the p4 resolve prompt to
	accept your merge file (am).

	Since the install process also enabled "doskey", the next time you want
	to do a merge, after typing the m to p4 resolve, then go to your dosbox
	and simply type two characters: <uparrow> and <enter>.  ALTERNATELY, you
	can setup a Windows shortcut to call u4m:  right click on the desktop and
	select "New Shortcut".  Enter "u4m" as the "Command Name" and then
	complete the dialog.  Now you can double click that icon.  Right click
	"properties" for your new icon and under the "program" tab, check "close
	on exit", to avoid extra dos boxes from accumulating.
	EOF
	exit
fi

#set -x
ae=~/win/autoexec.bat
my=~/mydata
out=u4m.bat
uroot=u
mydos=d:$uroot
forw=/
back=\\\\

if [ $# -eq 1 ]
then
	if [ $1x != installx ]
	then
		echo "usage: u4merge install"
		echo "       ONETIME ONLY!!"
		exit
	fi
	if [ -h $my/$uroot ]
	then
		echo "you have already initialized once!"
		exit
	fi
	echo -e doskey\\r >>$ae
	echo -e 'set PATH="%PATH%;D:\\"\r' >>$ae
	ln -s / $my/$uroot
	exit
fi

if [ $# -ne 4 ]
then
	echo "Wrong number of arguments to u4merge."
	echo "usage: u4merge base_file their_file your_file merge_file"
	exit
fi

a1=\"$mydos${1//$forw/$back}\"
a2=\"$mydos${2//$forw/$back}\"
a3=\"$mydos${3//$forw/$back}\"
a4=\"$mydos${4//$forw/$back}\"

echo -e p4winmrg -lf $a1 $a2 $a3 $a4 \\r >$my/$out
echo "Now type u4m in your win4lin dosbox."