#!/bin/bash

CURRENT_DIR="$(pwd)/$(dirname $0)"
PJ="${CURRENT_DIR%/.}/../.."
ORIG_IFS=$IFS
IFS=$'\n'

for f in $(git diff --cached --name-only --diff-filter=A,M)
do
	# file is perl and exits
	if [[ "$f" =~ ^.+\.(pl|pm)$ && -e "$PJ/$f" ]]; then
		perltidy -q -b "$PJ/$f"
		if [ $? -eq 0 ]; then
			git add "$PJ/$f"
			rm -f "$PJ/$f.bak"
		fi
	fi
done

IFS=$ORIG_IFS
