#!/bin/bash

jshint_alt="node_modules/.bin/jshint jshint"

for c in '.jshintrc' "`dirname $0`/../jshint"; do
    if [ -e "$c" ]; then
        config="$c"
    fi
done

function run_jshint {
    if [ -n "$config" ]; then
        $jshint --config "${config}" ${@}
    else
        $jshint ${@}
    fi
}

function changed_files {
    git diff --cached --name-only --diff-filter=ACM
}

function write_staged {
    while read file; do
        name=`basename $file`
        tmpfile=`mktemp /tmp/XXXXX-${name}`
        git show ":$file" > $tmpfile
        echo "$tmpfile"
    done
}

case "${1}" in
    --about )
        if [ -z "$config" ]; then
            config='default config'
        fi
        echo "Perform static analysis of js source files (${config})"
        ;;
    * )
        jshint=`which $jshint_alt 2>/dev/null | head -n 1`

        # call out to tools
        files=`changed_files | grep -e '\.js$' | write_staged`

        if [ -z "$files" ]; then
            exit 0
        fi

        if test -z "$jshint"; then
            echo "Please install jshint: npm install -g jshint"
            exit 0
        fi

        run_jshint $files
        status=$?
        rm $files
        exit $status
        ;;
esac
