#!/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$`
STAGED_RST_FILES=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.rst$`

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..."

    # Run on whole codebase
    if [ -d /proc/cygdrive ]; then
        ./vendor/bin/phpstan analyse
    else
        php ./vendor/bin/phpstan analyse
    fi

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

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

    # Run on whole codebase to skip on unnecessary filtering
    # Run first on app, admin, public
    if [ -d /proc/cygdrive ]; then
        ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff --config=.php-cs-fixer.no-header.php
    else
        php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff --config=.php-cs-fixer.no-header.php
    fi

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

    # Next, run on system, tests, utils, and root PHP files
    if [ -d /proc/cygdrive ]; then
        ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff
    else
        php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff
    fi

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

    # Next, run on user_guide_src/source PHP files
    if [ -d /proc/cygdrive ]; then
        ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff --config=.php-cs-fixer.user-guide.php
    else
        php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff --config=.php-cs-fixer.user-guide.php
    fi

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

if [ "$STAGED_RST_FILES" != "" ]; then
    echo "Checking for tabs in RST files"
    php ./utils/check_tabs_in_rst.php
fi

exit $?
