#!/bin/bash

# Do not run the commit hook a second time when the headers have been added
if [[ "$SKIP_POST_COMMIT" == "1" ]]; then
    exit 0
fi

# Define the formatter script
FORMATTER_SCRIPT="python ./commit-config/hooks/formatHeader.py"

# File extensions to match
EXTENSIONS="py|h|hpp|c|cpp|f|f90|build"

# Find all matching files in src which are currently staged
files=$(git diff --name-only HEAD^ | grep -E '\.('"$EXTENSIONS"')$')

# Check if any files were found
if [ -z "$files" ]; then
    echo "No matching files found."
    exit 0
fi

# Run the formatter on each file
for file in $files; do
    echo "Updating header for: $file"
    $FORMATTER_SCRIPT "$file"
done

# # Automatically stage the updated files
# git add $files

# # Optionally, commit the updated files (creates a second commit)
# SKIP_POST_COMMIT=1 git commit -m "(docs/headers) Update headers after commit" || echo "No changes to commit"

echo "Post-commit hook executed."

