#!/bin/bash

CONFIG=vendor/altelma/cs-fixer/src/.php_cs
PATHS=("bin/php-cs-fixer" "vendor/bin/php-cs-fixer");

echo "[GIT COMMIT HOOK] Run PHP-FIXER"

for i in ${PATHS[@]}; do
    [ -f $i ] && CMD=$i
done

[ -z $CMD ] && echo "Error: php-cs-fixer not found in " && for i in ${PATHS[@]}; do echo "$i"; done && exit 1

function show_help() {
    echo -e "\n Usage: cs-fixer <command> \n\n Available commands: \n
        cs                   - run php-cs-fixer in dry-run mode \n
        cs-diff              - run php-cs-fixer in dry-run mode with --diff option\n
        cs-stop-on-violation - run php-cs-fixer in dry-run mode and stop on fist violation \n
        cs-fix               - run php-cs-fixer and fix the issues \n
        help                 - print this help \n"
}

if [ "$1" == "cs" ]; then
    $CMD fix -v --config $CONFIG --dry-run

    exit
elif [ "$1" == "cs-diff" ]; then
    $CMD fix -v --config $CONFIG --dry-run --diff

    exit
elif [ "$1" == "cs-fix" ]; then
    $CMD fix -v --config $CONFIG

    exit
elif [ "$1" == "cs-stop-on-violation" ]; then
    $CMD fix -v --config $CONFIG --dry-run --stop-on-violation

    exit
elif [ "$1" == "help" ]; then
    show_help

    exit
fi

show_help
