#!/bin/sh

Red="\[\033[0;31m\]"
Yellow="\[\033[0;33m\]"

echo "${Yellow}* Running Laravel pint to check code formatting..."

# store all changes that have been staged, excluding deleted files
changes=$(git diff --name-only --staged --diff-filter=d)

if [ ${#changes} -gt 0 ]; then
    # check the formatting of all the files staged so far, excluding deleted files
    ./vendor/bin/pint --test $changes

    if [ $? -ne 0 ]; then
        echo "${Red}* Error: your staged codes do not follow the guidelines of PSR-12. Your changes can not be committed."
        exit 1
    fi
else
    echo "${Yellow}* No change was detected for pint to run (deleted files are ignored)"
fi


export TERMINAL_TYPE="${TERM:-screen}"

tput -T$TERMINAL_TYPE init