#!/bin/sh

set -e
set -u

wd="$(cd "$(dirname "$0")" && pwd)";

if type -P pyflakes > /dev/null; then
    pyflakes="pyflakes";
else
    flakes="$(cd "${wd}/../pyflakes-0.6.1" && pwd)";
    export PYTHONPATH="${flakes}:${PYTHONPATH:-}";
    pyflakes="${flakes}/bin/pyflakes";
fi;

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

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

cd "${wd}" && "${pyflakes}" "$@" | sed                                    \
  -e "/'from ctypes import \\*' used; unable to detect undefined names/d" \
  -e "/redefinition of unused/d"                                          \
  -e "/^twext\\/backport/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;
