#!/bin/sh

EXCLUDE=" * Match window and terminal background colours"

prev=HEAD
use_grep=1

# Merge x.y.0 entries with their subsequent release
all=`git tag | grep '^[0-9]' | sort -r`
first=`for i in $all; do echo $i; done | head -n 1`
rest=`for i in $all; do echo $i; done | tail -n +2 | grep -v '\.0$'`
if echo "$first" | grep -q '\.0$' ; then
    first=`echo $rest | sed 's/ .*//'`
    rest=`echo $rest | sed 's/^[^ ]* //'`
fi

for i in $first $rest; do
    [ "$prev" = "2.3.1" ] && use_grep=0
    if [ -n "`git log $i..$prev`" ] ; then
        printf "Version "
        if [ "$prev" = "HEAD" ] ; then
            git describe --match '[0-9]*' | sed 's/-/./;s/-/~/'
        else
            echo $prev
        fi
        # Need echo because if there's only one commit head strips trailing \n
        (git log '--pretty=format:Author: %an <%ae>%nDate:   %aD' $i..$prev ; \
        echo ) | head -n 2
        if [ $use_grep = 1 ] ; then
            git log '--pretty=format:%s' --grep='^\*' \
                    $i..$prev | sed "s/^\*/ */; s/[:.]$//; /$EXCLUDE/d"
        else
            git log '--pretty=format:%s' $i..$prev | sed 's/^/ * /; s/[:.]$//'
        fi
        echo
        echo
    fi
    prev=$i
done
cat `dirname $0`/ChangeLog.old
