#!/bin/sh
# 2003-2006, 2008 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain

# Make patch for file in $1 against CVS/SVN repository or .orig file

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common

if [ -f Makefile ] ; then
	DD1=`grep ^TOPOBJDIR Makefile | sed -e "s| ||g"`
	DD2=`grep ^top_builddir Makefile | sed -e "s| ||g"`
	DD3=`grep ^top_srcdir Makefile | sed -e "s| ||g"`
elif [ -f Makefile.in ] ; then
	DD1=`grep ^TOPOBJDIR Makefile.in | sed -e "s| ||g"`
	DD2=`grep ^top_builddir Makefile.in | sed -e "s| ||g"`
	DD3=`grep ^top_srcdir Makefile.in | sed -e "s| ||g"`
else
	warning "Can't get topdir from Makefile.in"
fi

if [ -n "$DD1" ] ; then
	export $DD1
	TOPDIR=$TOPOBJDIR
fi

if [ -n "$DD2" ] ; then
	export $DD2
	TOPDIR=$top_builddir
fi

if [ -n "$DD3" ] ; then
	export $DD3
	TOPDIR=$top_srcdir
fi

#test -z "$TOPDIR" && exit 1
#echo "TOPDIR: $TOPDIR"
export TOPDIR

which cvs >/dev/null && CVS=cvs
which svn >/dev/null && SVN=svn
which git >/dev/null && GIT=git


if [ -f $1.orig ] ; then
	DIFILE=`realpath $1`
	[ -n "$TOPDIR" ] && cd "$TOPDIR"
	DIFILE=${DIFILE/`realpath $PWD`\//}
	#echo 2 $DIFILE $PWD
	diff -purN $DIFILE.orig $DIFILE > $DIFILE.patch
	exit 0
fi


if [ -n "$CVS" -a -d CVS ] ; then
	DIFILE=`realpath $1`
	[ -n "$TOPDIR" ] && cd "$TOPDIR"
	DIFILE=${DIFILE/`realpath $PWD`\//}
	#echo 1 $DIFILE $PWD
	$CVS diff -u $DIFILE > $DIFILE.patch
	exit 0
fi

if [ -n "$SVN" -a -d .svn ] ; then
	DIFILE=`realpath $1`
	[ -n "$TOPDIR" ] && cd "$TOPDIR"
	DIFILE=${DIFILE/`realpath $PWD`\//}
	#echo 2 $DIFILE $PWD
	$SVN diff $DIFILE > $DIFILE.patch
	exit 0
fi

if [ -n "$GIT" ] ; then
	DIFILE=`realpath $1`
	[ -n "$TOPDIR" ] && cd "$TOPDIR"
	DIFILE=${DIFILE/`realpath $PWD`\//}
	#echo 2 $DIFILE $PWD
	$GIT diff $DIFILE | sed -e "s|^--- a/|--- |g" | sed -e "s|^\+\+\+ b/|\+\+\+ |g" > $DIFILE.patch
	exit 0
fi