#!/bin/sh

commit=1

echo ""
echo "* Checking if environment is sane"

if test "${ARCH_ARCHIVE}" = ""; then
    echo "      - ARCH_ARCHIVE is not set"
    commit=0
fi

if test "${ARCH_REVISION}" = ""; then
    echo "      - ARCH_REVISION is not set"
    commit=0
fi

if test "${ARCH_TREE_ROOT}" = ""; then
    echo "      - ARCH_TREE_ROOT is not set"
    commit=0
fi

if test "${commit}" = "1"; then
    echo "  => Environment OK"
else
    echo "  => Aborting now"
    exit 16
fi

cd ${ARCH_TREE_ROOT}

echo "* Checking if source tree meets ABINIT requirements"

tree_lint=`baz lint 2>&1`
if test "${tree_lint}" != ""; then
    echo "      - tree-lint FAILED (please inform baz of added/removed files)"
    commit=0
fi

log_file=`baz make-log 2> /dev/null`
if test -s "${log_file}"; then
    log_lint=`grep '^Changes:' ${log_file}`
    if test "${log_lint}" = ""; then
        log_lint=`grep '^Patches applied:' ${log_file}`
    fi
    if test "${log_lint}" = ""; then
        echo "      - log file parsing FAILED, 'Changes:' or 'Patches applied:' missing (please conform to ABINIT coding standards)"
        commit=0
    fi
else
    echo "      - log file parsing FAILED (file not found)"
    commit=0
fi

if test "${commit}" = "1"; then
    echo "  => Source tree is ready for commit"
    echo ""
    exit 0
else
    echo "  => Source tree is NOT ready for commit"
    echo ""
    exit 1
fi
