#!/bin/sh
# by Martin Seeler, and
# by Jorge Javier Araya Navarro

# destination of the final changelog file
OUTPUT_FILE=CHANGELOG.rst

# generate the changelog
if ! type gitchangelog > /dev/null; then
    echo "ERROR: Please install gitchangelog"
    exit 1
fi

gitchangelog > $OUTPUT_FILE

# prevent recursion!
# since a 'commit --amend' will trigger the post-commit script again
# we have to check if the changelog file has changed or not
res=$(git status --porcelain | grep $OUTPUT_FILE | wc -l)
if [ "$res" -gt 0 ]; then
  git add $OUTPUT_FILE
  git commit --amend --no-edit
  echo "Populated Changelog in $OUTPUT_FILE"
fi

