#!/usr/bin/env bash

##################################################
# Name: pre-commit
# Description: Git pre-commit hook for Rust projects
##################################################

SCRIPT=${0##*/}
ACT="${HOME}/.config/act/run.sh"

echo "Running ${SCRIPT} Git hook"

# Check with cargo
echo "Running Cargo check"
cargo check --verbose || { echo "Error running cargo check" ; exit 1 ; }

# Format with cargo
echo "Running Cargo fmt"
cargo fmt --all -- --check || { echo "Error running cargo fmt" ; exit 1 ; }

# Lint with clippy
echo "Running Cargo clippy"
cargo clippy --all --all-features -- -D warnings || { echo "Error running clippy" ; exit 1 ; }

# Run GitHub Actions locally with Act if the wrapper is present on this local system
if [ -f "${ACT}" ];
then
    if [ ! "${DISABLE_ACT:-EMPTY}" != "EMPTY" ];
    then
        echo "Running GitHub Actions locally using Act"
        "${ACT}" --dryrun || { echo "Error running GitHub Actions" ; exit 1 ; }
    else
        echo "GitHub Actions wrapper for Act has been disabled by \$DISABLE_ACT"
    fi
else
    echo "GitHub Actions wrapper for Act not present"
fi

exit 0
