#!/bin/sh

PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_PHP_FILES=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php$`

echo "Starting CodeIgniter precommit..."

if [ "$STAGED_PHP_FILES" != "" ]; then
    echo "Linting PHP code..."
    for FILE in $STAGED_PHP_FILES; do
        php -l -d display_errors=0 "$PROJECT/$FILE"

        if [ $? != 0 ]; then
            echo "Fix the error(s) before commit."
            exit 1
        fi

        FILES="$FILES $FILE"
    done
fi

if [ "$FILES" != "" ]; then
    echo "Running PHPStan..."

    php ./vendor/bin/phpstan analyse

    if [ $? != 0 ]; then
        echo "Fix the phpstan error(s) before commit."
        exit 1
    fi
fi

if [ "$FILES" != "" ]; then
    echo "Running PHP CS Fixer..."

    php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff

    if [ $? != 0 ]; then
        echo "Files are not following the coding standards. Please fix them before commit."
        exit 1
    fi
fi

exit $?
