#!/bin/bash
# use this script to edit ChangeLog
#
# if you don't want your sf mail address, put yours into a file with name
# "mail"
#
# Copyright (C) 2002 Ludwig Nussel
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

#
# $Id$
#

export LANG=en_US.UTF-8
export TZ=UTC

if [ -r .sflogin ]; then
	mailaddr=`cat .sflogin`
else
	REALNAME=`getent passwd|awk -v UID=$UID  -F : '$3==UID{FS=",";$0=$5;print $1;exit 0}'`
	mailaddr="$REALNAME <$USER@users.sourceforge.net>"
	echo "please add your name and email to the file '.sflogin', e.g."
	echo "echo '$mailaddr' > .sflogin"
	exit 1
fi;

EDITOR=${EDITOR:-vim}
DATE=`date '+%b %d, %Y:'`
changelog=ChangeLog

if ! which mktemp > /dev/null 2>&1; then
	echo "mktemp is required for this script to work"
	exit 1
fi

case "$1" in
	--help)
		echo "Usage: $0 [Changelog filename]"
		echo
		echo "Will use '$mailaddr' for ChangeLog entries"
		exit 0
	;;
esac

[ -n "$1" ] && changelog="$1"

if [ ! -e "$changelog" ]; then
	echo "No "$changelog" file found"
	exit 1
fi

TMPFILE=`mktemp -q /tmp/xqf-ChangeLog.XXXXXX`
if [ $? -ne 0 ]; then
	echo "$0: Can't create temp file, exiting..."
	exit 1
fi
TMPFILE2=`mktemp -q /tmp/xqf-ChangeLog.XXXXXX`
if [ $? -ne 0 ]; then
	echo "$0: Can't create temp file, exiting..."
	exit 1
fi
trap "rm -f $TMPFILE $TMPFILE2" EXIT

set +e

echo $DATE $mailaddr >> $TMPFILE
echo "- " >> $TMPFILE
echo >> $TMPFILE

if [ "`head -n1 $changelog`" = "`head -n1 $TMPFILE`" ]; then
	cat $changelog > $TMPFILE
else
	cat $changelog >> $TMPFILE
fi

cat $TMPFILE >> $TMPFILE2
$EDITOR $TMPFILE

if ! diff -q $TMPFILE $TMPFILE2; then
	cp $TMPFILE "$changelog"
else
	echo "no changes made"
fi
