#!/bin/bash
# TODO Add support for command line arguments
#usage:
# hgdumpchangesets [<int> [<int>]]

# defaults
rev=0
tip=`hg tip`
max=`echo ${tip} | awk '{ print $2 }' | sed -e 's/:.*//g'`

# parse command line options, if specified
if [ "$1" != "" ] ; then
	if [ $1 -gt 0 ] ; then
		rev=$1
	fi
	if [ "$2" != "" ] ; then
		if [ $2 -gt $1 ] ; then
			max=$2
		fi
	fi
fi

if [ $rev -gt $max ] ; then
	echo 'A revision greater than the working repository was specified. Exiting ...'
	exit 1
fi

output='dumped_changesets'
if  [ -d ${output} ] ; then
	# be happy.
	echo ''
else
	mkdir ${output}
fi
while [ $rev -le $max ] ; do
	echo "Creating a logged patch for ${rev} ..."
	patchfile="${output}/hg-${rev}.patch"
	#echo "hg -v log -r ${rev} > ${patchfile}"
	#echo "hg diff -r ${rev} >> ${patchfile}"
	hg -v log -r ${rev} > ${patchfile}
	hg diff -c ${rev} >> ${patchfile}
	rev=`expr ${rev} + 1`
done
