#!/bin/sh

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

# Redirect output to stderr.
exec 1>&2

# Get changed files
changed_files=$(git diff-index --name-only ${against})

# Verify files content
for file in ${changed_files}; do

    if grep -qiE 'skyreach_(system|activation|api)_key' "${file}"; then
    
        # verify key
        key=$(grep -iE 'skyreach_(system|activation|api)_key' "${file}" | grep -woiE '[a-z0-9]{32}')
        if [ -n "${key}" ]; then
            echo "Error: you are about to commit a secret key in file: ${file}"
            echo "Please remove it before committing."
            echo -
            grep -iE 'skyreach_(system|activation|api)_key' "${file}" | grep -iE '[a-z0-9]{32}'
            echo -
            exit 1
        fi
    
    fi

done

# vim:ft=sh