#!/bin/sh

export LC_ALL=C NO_XFAIL=1

############################### JS Lint #####################################

LINTED_DIRECTORIES="apps shared build/test/unit dev_apps/demo-keyboard tv_apps"
JSHINT_EXECUTABLE="node_modules/.bin/jshint"
JSHINT_XFAIL_LIST="build/jshint/xfail.list"
JSHINT_ARGS="--reporter=build/jshint/xfail $JSHINT_ARGS"
excluded_list=".jshintignore"

jshint_information() {
  echo "Please read https://github.com/mozilla-b2g/gaia/tree/master/build/jshint/README.md for more information.\n"
}

if [ ! -r $excluded_list ];
then
  exit 0
fi

if [ ! -r "$JSHINT_XFAIL_LIST" ];
then
  # early bailout, this commit hook is newer than the gaia code it runs in
  exit 0
fi

hash gjslint > /dev/null 2>&1
if [ $? -eq 1 ];
then
  echo >&2 "You should install gjslint to lint your patch"
  echo >&2 "https://developers.google.com/closure/utilities/docs/linter_howto"
  exit 1
fi

hash node > /dev/null 2>&1
if [ $? -eq 1 ];
then
  echo >&2 "You should install node to lint your patch"
  echo >&2 "http://nodejs.org/download/"
  exit 1
fi

if [ ! -x "$JSHINT_EXECUTABLE" ];
then
  echo >&2 'You should run `npm install` to lint your patch'
  exit 1
fi

js_changed_files=`git diff --staged --name-only --diff-filter=ACMRT -- $LINTED_DIRECTORIES | grep '\.js$'`
if [ -n "$js_changed_files" ]
then
  # gjslint: only test files xfailed in jshint
  # echo with the quotes keeps the whitespace -- comm needs newlines, but
  # LINTED_FILES needs to be space-separated
  # Note also that xfail.list _must_ be sorted.
  xfailed=`echo "$js_changed_files" | sort | comm -12 "$JSHINT_XFAIL_LIST" - | tr '[:space:]' ' '`

  gjslint_result=0
  if [ -n "$xfailed" ] ; then
    echo "gjslint check:"
    MAKE_CMD=make
    if [ `uname` = "FreeBSD" ]; then
      MAKE_CMD=gmake
    fi
    LINTED_FILES="$xfailed" $MAKE_CMD -s gjslint
    gjslint_result=$?
    echo
  fi

  echo "jshint check:"
  # warn about files being xfailed
  for file in $js_changed_files ; do
    one_was_ignored=""
    if grep -q $file "$JSHINT_XFAIL_LIST" ; then
      one_was_ignored="1"
      echo "$file: jshint errors are ignored. Please consider fixing lint errors and removing it from $JSHINT_XFAIL_LIST."
    fi
  done

  test -n "$one_was_ignored" && jshint_information

  # using the real jshint instead of the make target to prevent running npm
  # install at commit time
  $JSHINT_EXECUTABLE $JSHINT_ARGS $js_changed_files
  jshint_result=$?

  if [ $gjslint_result -ne 0 -o $jshint_result -ne 0 ] ; then
    echo "There were errors while linting the files, please see above."
    test $jshint_result -ne 0 && jshint_information
    exit 1
  fi
fi


############################### CSS Lint #####################################

css_changed_files=`git diff --staged --name-only --diff-filter=ACMRT -- apps shared/ | grep '\.css$' | tr '\n' ' '`
if [ -n "$css_changed_files" ]
then
  MAKE_CMD=make
  if [ `uname` = "FreeBSD" ]; then
    MAKE_CMD=gmake
  fi
  XULRUNNER_SDK=$(${MAKE_CMD} xulrunner_sdk)
  XPCSHELL_SDK=$(${MAKE_CMD} xpcshell_sdk)

  # $XULRUNNER_SDK is an environment variable assignment on some platforms, for
  # example: LD_LIBRARY_PATH=/xxx/xxx/xxx
  # so we need to use "env" to properly execute this command expanding this
  # variable assignment as intended.
  env GAIA_DIR=$(pwd) $XULRUNNER_SDK $XPCSHELL_SDK \
    -f build/xpcshell-commonjs.js \
    -e "quit(require('csslint').lint('$(pwd)', '$css_changed_files'));"

  if [ $? -ne 0 ]
  then
    echo "There were errors while linting the files, please see above."
    exit 1
  fi
fi

echo "No errors - committing"
