#!/bin/sh
# Enable this pre-commit hook by executing the following from the project root directory
# $ ln -s $PWD/scripts/pre-commit .git/hooks/pre-commit
if git diff --name-only --cached | grep -P '\.less$' ; then
	make checkless || exit 1
fi

make jshint || exit 1

if git diff --name-only --cached | grep -P '\.js$' ; then
	make qunit || exit 1
fi

if git diff --name-only --cached | grep -P '\.*php$' ; then
	make phplint || exit 1
fi

if git diff --name-only --cached | grep -P 'includes/skins/.*php$' ; then
	make validatehtml > $result
fi

for file in `git diff --cached --name-only | grep -i ".png\$"`
do
	echo "Compressing $file"
	if [ -f $file ]; then
		optipng -q -o7 "$file" && advpng -z -4 "$file" && advdef -z -4 "$file" | grep "Output"
		git add "$file"
	else
		echo 'file was removed'
	fi
done

for file in `git diff --cached --name-only | grep -i ".svg\$"`
do
	make nodecheck
	echo "Compressing $file"
	if [ -f $file ]; then
		node_modules/.bin/svgo --config=.svgo.yml "$file"
		git add "$file"
	else
		echo 'file was removed'
	fi
done
