#!/bin/sh

pe=`which php`
fl=`pwd`
igfiles=

usage()
{
    echo "usage: php_check [-p][-i] [folder]"
    echo "       will perform a php -l on each file that is a .php file in [folder]"
    echo ""
    echo "-p     specify which php binary to use"
    echo "-i     specify a path to exclude from the check"
    return 0
}

parse_args()
{
	while [ "$*" != "" ]; do
        case $1 in
            "-p") pe="${2}"; shift ;;
            "-i")
                if [ "${igfiles}" == "" ]; then
                    igfiles="-type d ( -name ${2}"
                else
                    igfiles="${igfiles} -o -name ${2}"
                fi
                shift
                ;;
            *) fl="${1}" ;;
        esac
        shift
    done
}

parse_args "$@"

if [ "${igfiles}" != "" ]; then
    igfiles="${igfiles} ) -prune -o"
fi

if [ -d $fl ]; then
    echo "Checking ${fl} for PHP syntax errors..."
    echo ""
    find ${fl} ${igfiles} -name "*.php" -print -exec ${pe} -l "{}" ";"
    echo ""
else
    echo "Folder not found: ${fl}"
fi

return 0
