#!/bin/sh

set -e
set -u

wd="$(cd "$(dirname "$0")" && pwd)";
flakes="$(cd "${wd}/../pyflakes-0.4.0" && pwd)";

export PYTHONPATH="${flakes}:${PYTHONPATH:-}";

if [ $# -eq 0 ]; then
  set - calendarserver twext twisted twistedcaldav txdav contrib;
fi;

tmp="$(mktemp "/tmp/pyflakes.XXXXX")";

cd "${wd}" && "${flakes}/bin/pyflakes" "$@" | sed                     \
  -e "/undefined name '_'/d"                                          \
  -e "/undefined name 'CalDAVFile'/d"                                 \
  -e "/redefinition of unused/d"                                      \
  -e "/'from .* import \\*' used; unable to detect undefined names/d" \
  -e "/redefinition of function/d"                                    \
  -e "/i[a-z]*store.py:[0-9][0-9]*: '.*' imported but unused/d"       \
  -e "/xmlext.py:[0-9][0-9]*: /d"                                     \
  | tee "${tmp}";

if [ -s "${tmp}" ]; then error="true"; else error="false"; fi;

rm -f "${tmp}";

if "${error}"; then exit 1; fi;
