#!/bin/bash

verbose=0
quiet=0
all=0
branchcheck=0
commitlog=0
debug=0
flags=""
sep=0
count=0
HELP () {
	cat <<EOF
		tomerge [opts] [branch]
		opts are 
			a => all - show branches with nothing to merge
			l => show log
			v => verbose
			g => show ancestry
			q => quiet
			c => show counts
			b => do branch check only
			d => debug
		if branch is specified, target is the current view
EOF
		exit 1
}


while getopts "avlgqcbdh" opt; do
    case $opt in
		a) all=1;;
        l) verbose=1;;
		v) verbose=1; flags="$flags -v";;
		g) verbose=1; sep=1; flags="$flags -g";;
		q) quiet=1;;
		c) count=1;;
		b) branchcheck=1;;
		d) debug=1;;
		h) HELP;;
		*) HELP;;
	esac
done
shift ` expr $OPTIND - 1 `

if [ $verbose == 1 -a $quiet == 1 ]; then
	echo you know verbose and quiet is kind of stupid, right? 
fi

rc=0
if [ $debug == 1 ]; then
	set -x
fi


function do1 () {

	if [[ "$1" =~ "^trunk" ]]; then
		from="svn://svn/zcode/$1"
	else
		from="svn://svn/zcode/branches/$1"
	fi
	if [[ "$2" =~ "^trunk" ]]; then
		to=svn://svn/zcode/$2
	elif  [ "$2" == "" ]; then
		to=""
	else
		to="svn://svn/zcode/branches/$2"
	fi
	if [ $count == 1 ]; then
		res=`svn mergeinfo --show-revs eligible $from $to | wc -l`
		if [ $res != 0 ]; then
			rc=1
		fi
		if [ $all == 1 -o $res != 0 ]; then
			printf "%6d %-40s -> %s\n" $res $from $to
		fi
		return
	fi
	res=`svn mergeinfo --show-revs eligible $from $to`
	if [ -z "$res" ]; then
		if [ $all == 1 ]; then
			echo -e "$from --> $to\nnone\n" 
		fi
	else
		rc=1
		if [ $quiet == 0 ]; then
			if [ $verbose == 1 ]; then
				# full logs
				if [ -n "$2" ]; then
						echo -e "$from --> $to\n"
				fi
				for rev in $res; do
					svn log -$rev $flags $from
					if [ $sep == 1 ]; then
						echo -en "\n====================================\n\n"
					fi
				done
				echo -en "\n"
			else
				# standard response
				echo -e "$from --> $to\n$res\n"
			fi
		fi
	fi
	# echo done with $1 $2 rc is $rc
}

if [ -n "$1" ]; then
	if [ -z "$2" ]; then
		pwv >&2 || exit 1
	fi
	do1 $*
    exit $?
fi

function domany () (

	while read line; do
		if [[ $line =~ "^#" ]]; then
			continue
		fi
		if [[ $line =~ "retired|ABANDONED" ]]; then
			continue
		fi
		set $line
		do1 $1 $2
		# echo done with $1 $2 rc is $rc
	done
	return $rc
)

if [ $branchcheck = 1 ]; then
	if [ -f $TOP/BRANCHES -a "`svn info $TOP/BRANCHES 2>/dev/null | grep '^URL'`" == "URL: svn://svn/zcode/trunk/BRANCHES" ]; then
		echo USING $TOP/BRANCHES >&2
		svn up $TOP/BRANCHES >&2
		svn status $TOP/BRANCHES >&2
		cut -f1 <$TOP/BRANCHES | grep -v '#' | sort >.b1
	else
		echo RETRIEVING BRANCHES FROM SVN >&2
		svn cat svn://svn/zcode/trunk/BRANCHES  | cut -f1 | grep -v '^#' | sort >.b1
	fi
	svn ls svn://svn/zcode/branches | cut -f1 -d/ | sort >.b2
	echo "Branches not listed in BRANCHES file:" >&2
	comm -23 .b2 .b1
	rm -f .b1 .b2
	exit
fi

if [ -f $TOP/BRANCHES -a "`svn info $TOP/BRANCHES 2>/dev/null | grep '^URL'`" == "URL: svn://svn/zcode/trunk/BRANCHES" ]; then
	echo USING $TOP/BRANCHES >&2
	svn up $TOP/BRANCHES >&2
	svn status $TOP/BRANCHES >&2
	domany <$TOP/BRANCHES
	rc=$?
else

	echo RETRIEVING BRANCHES FROM SVN >&2
	svn cat svn://svn/zcode/trunk/BRANCHES  | domany
	rc=$?
fi

echo rc is $rc
exit $rc
