#!/usr/bin/env bash
# -*-shell-script-*-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.

usage() {
    cat >&2 <<-EOF
	$0: unknown option $1
	Usage: $0 [options] [log message]
	Valid options are:
	--logfile=filename (-f filename)
	--commit
	If no log message, one will be prompted for.
	EOF
    exit 1
}

CL=
nocommit=true
while [ $# -gt 0 ]; do
    case "$1" in
    --logfile=?*)
	CL=${1#--logfile=}
	shift
	;;
    --logfile|-f)
	if [ $# -lt 2 ]; then
	    usage
	fi
	CL=$2
	shift
	shift
	;;
    -f?*)
	CL=${1#-f}
	shift
	;;
    --commit)
	nocommit=
	shift
	;;
    --)
	shift
	break
	;;
    -*)
	usage
	;;
    *)
	break
	;;
    esac
done

if ! hg root >& /dev/null ; then
    echo "Cannot use this script outside a Mercurial repository" >&2
    exit -1
fi

tag=$(hg branch)

case "$CL" in
'')
    d=.
    while [ ! -f $d/vertoo.data -a ! -f $d/ChangeLog -a ! -f $d/ChangeLog.$tag ]; do
	if [ $d -ef / ]; then
	    echo "$0: cannot find top directory of package" >&2
	    exit 1
	fi
	d=../$d
    done
    CL=$d/ChangeLog
    case $tag in
    default)
	;;
    *)
	CL=$CL.${tag}
	;;
    esac
    dir=$(cd $d && \pwd -P)
    ;;
*/*)
    dir=$(cd "${CL%/*}"; \pwd -P)
    ;;
*)
    dir=$(\pwd -P)
    ;;
esac

PROJECT=${dir##*/}
IDENTITY=$(hg showconfig ui.username)

if [ $# -gt 0 ]; then
    msg=$(echo "  $*" | fmt)
    msg=${msg#  }
else
    nl=$'\n  '
    echo "Log message (end with ^D or empty line):"
    msg=
    tmpmsg=
    while read -r line && [ "$line" ]; do
	case "$line" in
	[a-zA-Z0-9_\'\"\#\$\(\)]*)
	    case "$tmpmsg" in
	    *[.!:])
		s='  '
		;;
	    *)
		s=' '
		;;
	    esac
	    tmpmsg=$(echo "  ${tmpmsg}${tmpmsg:+$s}$line" | fmt)
	    tmpmsg=${tmpmsg#  }
	    ;;
	*)
	    if [ "$tmpmsg" ]; then
		msg="${msg:+$msg$nl}$tmpmsg"
		tmpmsg=
	    fi
	    msg="${msg:+$msg$nl}$line"
	    ;;
	esac
    done
    if [ "$tmpmsg" ]; then
	msg="${msg:+$msg$nl}$tmpmsg"
	tmpmsg=
    fi
fi

file=ChangeLog.$RANDOM

case "$CL" in
*/*)
    cd "${CL%/*}"
    CL=${CL##*/}
    ;;
esac

if [ -f "$CL" ]; then
    sed 's/^/X/' "$CL" | {
	date=
	user=
	first=true
	while read -r line; do
	    line=${line#X}
	    case "$line" in
	    '* '???\ ???\ [\ 0123][0-9]\ 20[0-9][0-9]\ *\<*@*\>)
		if [ "$first" = true ]; then
		    date=$(expr "$line" : '\* \(... ... .. ....\) .*')
		    user=$(expr "$line" : '\* ... ... .. .... \(.*\)')
		    if [ "$date" = "$(date +'%a %b %_d %Y')" -a "$user" = "$IDENTITY" ]; then
			cat <<EOF
$line
- $msg
EOF
		    else
			cat <<EOF
* $(date +'%a %b %_d %Y') $IDENTITY
- $msg

$line
EOF
		    fi
		    first=false
		else
		    cat <<EOF
$line
EOF
		fi
		;;
	    *)
		cat <<EOF
$line
EOF
		;;
	    esac
	done
	if [ "$first" = true ]; then
	    cat <<EOF
* $(date +'%a %b %_d %Y') $IDENTITY
- $msg

EOF
	fi
    }
else
    cat <<EOF
# ChangeLog file for $PROJECT
# This file is updated with Maddlog

* $(date +'%a %b %_d %Y') $IDENTITY
- $msg

EOF
fi > $file

mv $file "$CL"

hg add "$CL" >& /dev/null

if [ ! "$nocommit" ]; then
    hg commit -m "${msg//
  /
}" "$CL"
else
    hg diff "$CL"
    echo "Don't forget to COMMIT."
fi
echo "Don't forget to PUSH."
