#!/usr/bin/env bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

# Read eirene version from version.txt file
ver=$(head -n 1 version.txt)

echo "Running pre-commit hook for EIRENE version" $EIRENE_VERSION

# Set the version text in modules/eirmod_parmmod.F to the value in version.txt
src_file="src/modules/eirmod_parmmod.F"
src_ver=$(awk 'BEGIN { FS = "=" }/EIRENE_VERSION_STRING/{ print $2 }' $src_file )
src_ver=${src_ver:1:-1}
if [ -z $src_ver ]; then
    echo "No version found in ${src_file}. Please update."
    retval=2
elif [ "$src_ver" != "$ver" ]; then
    echo "Updating source $src_ver to $ver"
    replace=$(sed -i "s/$src_ver/$ver/" $src_file)
    retval=1
fi

# Set the version text in EPL.md to the value in version.txt
epl_file="EPL.md"
epl_ver=$(awk '/Version:/{ print $2 }' $epl_file )
if [ -z $epl_ver ]; then
    echo "No version found in ${epl_file}. Please update."
    retval=2
elif [ "$ver" != "$epl_ver" ]; then
    echo "Updating license $epl_ver to $ver"
    replace=$(sed -i "s/$epl_ver/$ver/" $epl_file)
    retval=1
fi

# Set the version text in eirene.tex to the value in version.txt
man_file="Manual/eirene.tex"
man_ver=$(awk '/Manual version/{print $4 }' $man_file )
man_ver=${man_ver::-2}
if [ -z $man_ver ]; then
    echo "No version found in ${man_file}. Please update."
    retval=2
elif [ "$man_ver" != "$ver" ]; then
    echo "Updating manual ${man_ver} to $ver"
    replace=$(sed -i "s/$man_ver/$ver/" $man_file)
    retval=1
fi

# Set the version text in coding_rules.md to the value in version.txt
cod_file="coding_rules.md"
cod_ver=$(awk '/Version:/{ print $2 }' $cod_file )
if [ -z $cod_ver ]; then
    echo "No version found in ${cod_file}. Please update."
    retval=2
elif [ "$cod_ver" != "$ver" ]; then
    echo "Updating coding rules ${cod_ver} to $ver"
    replace=$(sed -i "s/$cod_ver/$ver/" $cod_file)
    retval=1
fi

if [ "$retval" == "1" ]; then
    echo "Version numbers did not match, they have been replaced however the commit did not proceeed. Please commit again."
    exit 1
elif [ "$retval" == "2" ]; then
    echo "A Version number was not found in one of the files. Please update the file and commit again."
    exit 2
fi

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

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

# Redirect output to stderr.
exec 1>&2

# 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' | wc -c) != 0
then
	cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
	exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
