#!/usr/bin/env bash

# Verify that the commit was actually signed
echo "=== post-commit: verifying commit signature ===" >&2

COMMIT_HASH=$(git rev-parse HEAD 2>/dev/null) || {
    echo "Error: Failed to get commit hash." >&2
    exit 1
}

if [ -z "$COMMIT_HASH" ]; then
    echo "Error: Failed to get commit hash." >&2
    exit 1
fi

# Remove any Co-Authored-By lines from the commit message
COMMIT_MSG=$(git log -1 --format='%B' "$COMMIT_HASH" 2>/dev/null) || {
    echo "Error: Failed to get commit message." >&2
    exit 1
}

CLEAN_MSG=$(echo "$COMMIT_MSG" | grep -v -i '^Co-Authored-By:' || true)
if [ "$COMMIT_MSG" != "$CLEAN_MSG" ]; then
    echo "Removing Co-Authored-By lines from commit message..." >&2
    # Remove trailing blank lines
    CLEAN_MSG=$(echo "$CLEAN_MSG" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
    git commit --amend --no-verify -m "$CLEAN_MSG" >/dev/null 2>&1 || {
        echo "Error: Failed to amend commit to remove Co-Authored-By lines." >&2
        exit 1
    }
    COMMIT_HASH=$(git rev-parse HEAD 2>/dev/null) || {
        echo "Error: Failed to get amended commit hash." >&2
        exit 1
    }
fi

SIG_STATUS=$(git show --pretty=format:"%G?" --no-patch "$COMMIT_HASH" 2>/dev/null || echo "N")

case "$SIG_STATUS" in
    G)
        echo "✓ Commit is properly signed" >&2
        ;;
    B)
        echo "ERROR: Bad signature on commit $COMMIT_HASH" >&2
        echo "The commit signature is invalid. Please check your signing key configuration." >&2
        exit 1
        ;;
    X)
        echo "ERROR: Expired key used to sign commit $COMMIT_HASH" >&2
        exit 1
        ;;
    Y)
        echo "ERROR: Expired signature on commit $COMMIT_HASH" >&2
        exit 1
        ;;
    R)
        echo "ERROR: Revoked key used to sign commit $COMMIT_HASH" >&2
        exit 1
        ;;
    E)
        echo "ERROR: Cannot verify signature on commit $COMMIT_HASH" >&2
        echo "Please check your signing key configuration and allowed_signers file." >&2
        exit 1
        ;;
    N)
        echo "ERROR: Commit $COMMIT_HASH is not signed!" >&2
        echo "All commits must be signed. The commit was created but is unsigned." >&2
        echo "To fix this commit, run: git commit --amend --no-edit" >&2
        exit 1
        ;;
    *)
        echo "ERROR: Unknown signature status '$SIG_STATUS' for commit $COMMIT_HASH" >&2
        exit 1
        ;;
esac

exit 0
