#!/bin/sh

# List of Go files that have been modified
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.go$')

# No Go files were modified
if [ -z "$FILES" ]; then
    echo "No Go files were modified"
    exit 0
fi

# Flag whether formatting was done
FORMATTED=false

# Iterate and format each Go file
for FILE in $FILES; do
    # Modify swag comment, trigger swag fmt formatting
    sed -i 's/@Router/ @Router /g' $FILE
    swag fmt $FILE
    # Check if the file has been formatted by swag fmt
    if ! git diff --quiet "$FILE"; then
        FORMATTED=true
        # Add the formatted Go file to the staging area
        git add "$FILE"
        echo "$FILE was formatted"
    fi
done

if $FORMATTED; then
    echo -e "\033[31m[Error] \033[0mSome files have been formatted. Please commit again"
    exit 1
fi

# If formatting is complete, continue with the original commit
exit 0
