#!/bin/bash
# Claude MPM Git Wrapper
# Customizes git commit and PR messages with Claude MPM branding

# Function to modify commit message
modify_commit_message() {
    local msg="$1"

    # Replace Claude Code with Claude MPM
    msg=$(echo "$msg" | sed 's/🤖 Generated with \[Claude Code\](https:\/\/claude\.ai\/code)/🤖👥 Generated with [Claude MPM](https:\/\/github.com\/bobmatnyc\/claude-mpm)/')
    msg=$(echo "$msg" | sed 's/Generated with \[Claude Code\]/Generated with [Claude MPM]/')
    msg=$(echo "$msg" | sed 's/Claude Code/Claude MPM/g')

    echo "$msg"
}

# Check if this is a commit operation
if [[ "$1" == "commit" ]] && [[ "$@" == *"-m"* ]]; then
    # Extract and modify the commit message
    args=("$@")
    modified_args=()

    for ((i=0; i<${#args[@]}; i++)); do
        if [[ "${args[$i]}" == "-m" ]]; then
            # Next argument is the message
            modified_args+=("${args[$i]}")
            i=$((i+1))
            if [[ $i -lt ${#args[@]} ]]; then
                modified_msg=$(modify_commit_message "${args[$i]}")
                modified_args+=("$modified_msg")
            fi
        else
            modified_args+=("${args[$i]}")
        fi
    done

    # Execute git with modified arguments
    git "${modified_args[@]}"
else
    # For all other git commands, pass through unchanged
    git "$@"
fi