#!/bin/bash
args=`echo $@`
tmpfile=${0##*/}.tmp

head -n 16 configure.ac | grep VERSION= > $tmpfile
source $tmpfile
EXTRA_VERSION=`echo $EXTRA_VERSION | awk -F'.' '{for (i=1;i<NF;i++){printf $i"."};printf $NF+1}'`
nextsversion="${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}cvs${EXTRA_VERSION}"
nextextra="$EXTRA_VERSION"
nextextratype="head"
rm -f $tmpfile

IFS='
'
filelist=`cvs status $@ configure.ac 2>/dev/null |grep ^File`

for file in $filelist; do
	merge=`echo $file | grep Merge`
	modif=`echo $file | grep Locally`
	patch=`echo $file | grep Patch`
	if [ "$patch" != "" ]; then
		echo $patch 
		echo You have to update first
		exit
	fi;
	if [ "$merge" != "" ]; then
		echo $merge 
		echo You have to update first
		exit
	fi;
	if [ "$modif" != "" ]; then
		echo $modif
	fi;
done;
patchset="("
log=""
files=`cvs diff -uN $@ 2>$tmpfile |grep ^Index`
if [ "$files" == "" ]; then
	echo Nothing to commit\!
	if [ -s $tmpfile ]; then
		echo See possible reason below:
		cat $tmpfile
	fi
	exit
fi
rm -f $tmpfile
name=`whoami`
#change if your login isn't your name
if [ "$name" == "claws" ]; then
	name="paul";
fi;
if [ "$name" == "leroyc" ]; then
	name="colin";
fi;
if [ "$name" == "torte" ]; then
	name="thorsten";
fi;
if [ "$name" == "dinh" ]; then
	name="hoa";
fi;
if [ "$name" == "f" -o "$name" == "darkok" ]; then
	name="darko";
fi;
if [ "$name" == "msp" ]; then
	name="martin";
fi;
if [ "$name" == "devel" ]; then
	name="mones";
fi;
if [ "$name" == "hb" ]; then
	name="holger";
fi;
if [ "$name" == "salvatore" ]; then
	name="iwkse";
fi;

if [ "$name" == "corn" ]; then
	name="pawel";
fi;

log="`date -u +%Y-%m-%d` [$name]\t$nextsversion\n\n"
for line in $files; do
	file=`echo $line | cut -d' ' -f2`
	dir=`dirname $file`
	filename=`basename $file`
	cvsfile="$dir/CVS/Entries"
	version=`grep "\/$filename\/" $cvsfile | cut -d'/' -f3`
	nextversion=`echo $version | awk -F'.' '{for (i=1;i<NF;i++){printf $i"."};printf $NF+1}'`
	
	log="$log\t* $file\n"
	if [ "$version" != "0" ]; then
		patchset="$patchset cvs diff -u -r $version -r $nextversion $file; "
	else
		patchset="$patchset diff -u /dev/null $file; "
	fi
done;
patchset="$patchset ) > $nextsversion.patchset"

if [ "$CVSEDITOR" == "" ]; then
	if [ "$EDITOR" == "" ]; then
		if [ "$VISUAL" != "" ]; then
			EDITOR=$VISUAL
		else
			EDITOR=vi
		fi;
	fi;
else
	EDITOR=$CVSEDITOR
fi;

echo -e "#please complete the changelog entry below" > /tmp/logentry.$$
echo -e -n $log >> /tmp/logentry.$$

$EDITOR /tmp/logentry.$$

echo "--8<----------"
grep -v "^#" /tmp/logentry.$$ > /tmp/log.tmp.$$ \
&& mv /tmp/log.tmp.$$ /tmp/logentry.$$
echo >> /tmp/logentry.$$
cat /tmp/logentry.$$

chlog="ChangeLog"

echo "--8<----------"
echo -n "Is it ok (write to $chlog and update configure.ac) [y/N]?"
read ans
if [ "$ans" == "y" ]; then
	mv $chlog $chlog.old
	cat /tmp/logentry.$$ > $chlog
	cat $chlog.old >> $chlog
	rm $chlog.old

	cat configure.ac | sed "s/^EXTRA_VERSION=.*/EXTRA_VERSION=$nextextra/" > configure.ac.new \
	&& mv configure.ac.new configure.ac ;
	
	echo "$patchset" >> PATCHSETS

	if [ "$args" != "" ]; then
		echo commiting $@ PATCHSETS $chlog configure.ac
		cvs commit -m "`cat /tmp/logentry.$$`" $@ PATCHSETS $chlog configure.ac
	else
		echo commiting recursively
		cvs commit -m "`cat /tmp/logentry.$$` "
	fi;
	rm -f /tmp/logentry.$$
	wget -O /dev/null -o /dev/null http://www.colino.net/claws-mail/update.php
fi
