#!/usr/bin/env bash

# A Git pre-commit hook that runs ahjo-scan.exe and prevents commit if scan fails.

green='\033[0;32m'
red='\033[0;31m'
no_color='\033[0m'

echo "Scan check started..."
ahjo-scan.exe --stage --quiet
exit_status=$?

if [ $exit_status -eq 1 ]; then
    echo "If you want to ignore the scan results, add matches to ahjo_scan_ignore.yaml. Otherwise fix the issues and try again."
    echo -e "${red}Commit aborted.${no_color}"
    exit 1
elif [ $exit_status -eq 0 ]; then
    echo -e "${green}Scan check passed.${no_color}\n"
    exit 0
else 
    this_file_path=$(realpath $0)
    echo "Scan check failed. Make sure ahjo is installed correctly and try again. If you want to commit without scanning, remove this hook from $this_file_path."
    echo -e "${red}Commit aborted.${no_color}"
    exit 1
fi