#!/usr/bin/env bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# To run, install node and npm, then install jshint:
#   sudo npm install jshint -g

WHICH_JSHINT=`which jshint 2>/dev/null`
if [ "${WHICH_JSHINT}x" == "x" ]; then
    echo "You need to have jshint installed to run this script"
    echo "  Install node.js and npm, then install jshint like this:"
    echo "  sudo npm install jshint -g"
else
    if command -v realpath>/dev/null 2>&1; then
        CURRENT_FILE=`realpath "$0"`
    else
        CURRENT_FILE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/$(basename $0)"
    fi

    STATIC_ANALYSIS_DIR=`dirname "$CURRENT_FILE"`

    if [ "$#" -eq 0 ]; then
        TESTS=package
    else
        TESTS="$@"
    fi

	JSHINTRC=$STATIC_ANALYSIS_DIR/../.jshintrc

	# Fix path for cygwin environments
	
	CYGPATH=$(which cygpath)
	if [ "${CYGPATH}x" != "x" ]; then
		JSHINTRC=$($CYGPATH -w $JSHINTRC)
	fi

    jshint --config $JSHINTRC -e .jsm $TESTS
fi
