#!/bin/bash

# A pre-commit hook script to:
#  - run linting and formatting on staged files based on the lint-staged,
#    prettier and eslint configurations in src/node/desktop.
#  - run detect-secrets on staged files to check for secrets.
# This script will run all applicable hooks and fails the commit afterwards if any of the hooks fail.

color_error='\033[1;31m' # red
color_highlight='\033[1;95m' # magenta
color_none='\033[0m' # no color
repo_root=$(git rev-parse --show-toplevel)

fail_precommit=false # will be set to true if the pre-commit hook should fail at the end

hook_setup() {
    cd "$repo_root"
    # Print a separator line before the output of each hook
    echo -e "${color_highlight}------${color_none}"
}

precommit_exit() {
    hook_setup
    if [ "$fail_precommit" = true ]; then
        echo -e >&2 "${color_error}pre-commit hook failed.${color_none}"
        exit 1
    else
        echo -e "${color_highlight}pre-commit hook complete.${color_none}"
        exit 0
    fi
}

echo -e "${color_highlight}Running pre-commit hook...${color_none}"

#####################################
#          Add hooks below          #
#####################################

detect_secrets_command() {
    if command -v detect-secrets &> /dev/null; then
        ./git_hooks/secrets/run-detect-secrets run-hook
    else
        docker compose -f ./git_hooks/secrets/docker-compose.yml --progress quiet run run-detect-secrets run-hook
    fi
}

detect_secrets_installed() {
    if command -v detect-secrets &> /dev/null; then
        return 0
    elif command -v docker &> /dev/null; then
        return 0
    else
        return 1
    fi
}

# Linting and formatting on RStudio Desktop IDE files
if test -f ./src/node/desktop/.lintstagedrc
then
    hook_setup
    if git diff --staged --name-only | grep -qE '^src/node/desktop/.*'
    then
        cd ./src/node/desktop && npx lint-staged
        if [ $? -ne 0 ]; then
            fail_precommit=true
        fi
    fi
else
    echo -e >&2 "${color_highlight}Note: src/node/desktop linting pre-commit hook is not being run because ${color_none}.lintstagedrc${color_highlight} was not found.${color_none}"
fi

# Secret scanning on staged files
if ! detect_secrets_installed;
then
    echo -e >&2 "${color_highlight}Note: secret scanning pre-commit hook is not being run because detect-secrets is not installed.${color_none}"
    echo -e >&2 "\tInstall ${color_highlight}detect-secrets${color_none} with 'pip install detect-secrets' or 'brew install detect-secrets'."
else
    hook_setup
    echo "Running detect-secrets-hook on staged files..."
    secrets_results=$(detect_secrets_command)
    if [ -n "$secrets_results" ]; then
        fail_precommit=true
        echo -e "\n${color_error}Uh oh! It looks like you have secrets in your code. Please remove them before committing.${color_none}"
        echo -e "If you are certain that these are false positives, see git_hooks/secrets/README.md for instructions on how to mark them as such.\n"
        echo "$secrets_results"
    else
        echo "🎉 No secrets found!"
    fi
fi

#####################################
#          Add hooks above          #
#####################################

# This should be the last line of this file
precommit_exit
