#!/bin/bash

# ------------------------------------------------------------------------------
# Check for errors in the Python code.
# Based on http://www.sitepoint.com/git-hooks-fun-profit/
git  diff --cached --name-status --diff-filter=ACMR | while read STATUS FILE; do
if [[ ${FILE: -3} == ".py" ]]; then
    python -m py_compile "$FILE" 1> /dev/null
    if [ $? -ne 0 ]; then
        echo "Aborting commit due to errors in Python code" >&2
        exit 1
    fi
fi
done

#-------------------------------------------------------------------------------
# Check for file names with non-ASCII characters.
# From pre-commit.sample

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# If you want to allow non-ascii filenames, set this variable to true.
allownonascii=$(git config hooks.allownonascii)

# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
	# Note that the use of brackets around a tr range is ok here, (it's
	# even required, for portability to Solaris 10's /usr/bin/tr), since
	# the square bracket bytes happen to fall in the designated range.
	test "$(git diff --cached --name-only --diff-filter=A -z $against |
	  LC_ALL=C tr -d '[ -~]\0')"
then
	echo "Error: Attempt to add a non-ascii file name."
	echo
	echo "This can cause problems if you want to work"
	echo "with people on other platforms."
	echo
	echo "To be portable it is advisable to rename the file ..."
	echo
	echo "If you know what you are doing you can disable this"
	echo "check using:"
	echo
	echo "  git config hooks.allownonascii true"
	echo
	exit 1
fi

# ------------------------------------------------------------------------------
# If there isn't a version number in modelicares/__init__.py, add a markdown
# file in the base folder.

file="UNRELEASED COPY.md"

# Get the last version.
commit=$(git rev-list --tags --max-count=1)
lastversion=$(git describe --tags $commit)
# This is simpler but doesn't always return the latest tag:
# lastversion=$(git describe --abbrev=0 --tags)

n_matches=$(grep -c '__version__ *= *None' modelicares/__init__.py)

if [ $n_matches == 1 ]; then

    echo $(date -u +"%Y-%m-%d %H:%M:%SZ") > "$file"
    echo >> "$file"
    echo $(git config user.name) >> "$file"
    echo >> "$file"
    echo "This is an unreleased copy of ModelicaRes." >> "$file"
    echo "It may contain additional features, but it may also contain bugs since it was" >> "$file"
    echo "probably not fully tested." >> "$file"
    echo >> "$file"
    echo "For a released copy, please see the " >> "$file"
    echo "[main project page](http://kdavies4.github.io/ModelicaRes/)," >> "$file"
    echo "[PyPI](https://pypi.python.org/pypi/ModelicaRes), or the release tags in the" >> "$file"
    echo "[GitHub repository](https://github.com/kdavies4/ModelicaRes)." >> "$file"
    echo "The last version was $lastversion." >> "$file"

    git add "$file"
else
    git rm --ignore-unmatch "$file"
fi
