#!/bin/bash
#
# An example hook script that is called after a successful
# commit is made.
#
# To enable this hook, rename this file to "post-commit".

BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH" == "master" ]; then
    # don't bump version on master branch (will be done manually)
    exit;
fi

# only bump version if taskopen script has been modified
git show --pretty="format:" --name-only | grep ^taskopen$ || exit

REV=$(git rev-list HEAD | wc -l)
HASH=$(git log -1 --pretty=%h)
VER=$(git describe --tags)

LAST_MSG=$(git log -1 --pretty=%s)
if [[ $LAST_MSG =~ ^Auto-commit ]]; then
    exit;
fi

# save changes
git diff taskopen > taskopen.orig
patch -p1 -R < taskopen.orig

sed "s/VERSION=.*;/VERSION=\"$VER\";/" -i taskopen
sed "s/REVNUM=.*;/REVNUM=\"$REV\";/" -i taskopen
sed "s/REVHASH=.*;/REVHASH=\"$HASH\";/" -i taskopen

git add taskopen
git commit -m "Auto-commit (bumped revision)"

patch -p1 < taskopen.orig
rm taskopen.orig
