#!/bin/sh

# Husky Env Variables :
# HUSKY_PRE_COMMIT : if set to 0, disable pre-commit hook
# HUSKY_INPUT_ENV: if set to 0, disable the possibility of user input for the hook

npx="npx"

# For Windows
if [ "$OSTYPE" = "msys" ]; then
    export PATH="/usr/bin:/usr/local/bin:$PATH"
    npx="npx.cmd"
fi
. "$(dirname "$0")/_/husky.sh"

if [ "$HUSKY_PRE_COMMIT" != 0 ]; then
    export ESLINT_FAST=1
    $npx lint-staged

    # Check console, debugger|useFakeTimers and useFakeTimers leftover statements
    consoleregexp='console\.|debugger|useFakeTimers'
    if test $(git diff --cached | grep -nE $consoleregexp | wc -l) != 0
    then
        if [ "$HUSKY_INPUT_ENV" != 0 ] && [ "$OSTYPE" != "msys" ]; then
            # activate user inputs
            exec < /dev/tty
            exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
            read -p "There are some occurrences of forbidden patterns at your modification. Are you sure want to continue? (y/n)" yn
            echo $yn | grep ^[Yy]$
            if [ $? -ne 0 ]; then
                exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
            fi
        else
            echo -e "\033[1;31mWARNING: Some occurences of forbidden patterns were detected \033[0m"
            exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
        fi
    fi

    # Css wrap check
    node .husky/css_check.js
fi
