#!/bin/sh
# This script creates a template to update the ChangeLog.

get_ver() {
	git describe --abbrev=0 --tags --match 'v*' "$@"
}

if [ "$1" = -h ]; then
	cat >/dev/stderr <<-!
	Usage: $0 [commit]
!
	exit 0
fi

set -- "$@" HEAD

prev="$(get_ver "$1")"

git shortlog --format='* %s' -w80,0 "$prev".."$1" |
awk '
!/^\*/ && /:$/ {
	$NF=""
	author = $0
	next
}
!/^$/ {
	print author, ">", $0
}
' | sort |
awk '
BEGIN {
	getline < "VERSION"
	sub(/T.*/, "", $2)
	printf "Version: %s, %s\n", $1, $2
	FS="  > "
}
END {
	print "\n\n"
}
author != $1 {
       print "\n  [", $1, "]\n"
       author = $1
}
{
	sub(/^[^>]*> /, "")
	print
}
'
