#!/bin/bash

export ERRFILE=$(mktemp)
export PATH="/usr/bin:/bin"
export REPOS="$1"
export TMPFILE=$(mktemp)
export TXN="$2"

svnlook changed -t "$TXN" "$REPOS" \
| awk '{print $2}' \
| \
while read LINE; do
svnlook cat -t "$TXN" "$REPOS" "$LINE" > "$TMPFILE"

if [ $? -ne 0 ]; then
echo "Warning: Failed to checkout $LINE" >&2
exit 1
fi

EXT=$(echo $LINE | awk -F'.' '{ print $NF }')

case "$EXT" in
"erb")
erb -x -T '-' $TMPFILE | ruby -c

if [ $? -ne 0 ]; then
echo "ERB parsing error in $LINE:" >&2
cat $ERRFILE >&2

exit 2
fi
;;
"pp")
/usr/bin/puppet --color=false --parseonly $TMPFILE >$ERRFILE 2>/dev/null

if [ $? -ne 0 ]; then
echo "Puppet syntax error in $LINE:" >&2
cat $ERRFILE >&2

exit 2
fi
;;
"rb")
ruby -c $TMPFILE >$ERRFILE 2>/dev/null

if [ $? -ne 0 ]; then
echo "Bash syntax error in $LINE:" >&2
cat $ERRFILE >&2

exit 2
fi
;;
"sh")
bash -n $TMPFILE >$ERRFILE 2>/dev/null

if [ $? -ne 0 ]; then
echo "Bash syntax error in $LINE:" >&2
cat $ERRFILE >&2

exit 2
fi
;;
*)
continue
;;
esac

rm -f "$ERRFILE" "$TMPFILE"
exit 0
done