#!/bin/bash
# MCP-ZERO Burn Command Installer
# Adds the burn command to the user's shell environment

set -e

# Color definitions
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Get the absolute path to the burn script
BURN_SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/burn.sh"

# Print colored message
print_message() {
  local color=$1
  local message=$2
  echo -e "${color}${message}${NC}"
}

# Add burn command to shell profile
add_burn_command() {
  local profile_file="$1"
  
  print_message "${BLUE}" "Adding burn command to ${profile_file}..."
  
  # Check if already added
  if grep -q "# MCP-ZERO Burn Command" "${profile_file}" 2>/dev/null; then
    print_message "${YELLOW}" "Burn command already exists in ${profile_file}. Updating..."
    # Remove existing burn command
    sed -i '/# MCP-ZERO Burn Command/,/# End MCP-ZERO Burn Command/d' "${profile_file}"
  fi
  
  # Add the burn command function
  cat >> "${profile_file}" << EOF

# MCP-ZERO Burn Command
burn() {
  # Check if no arguments provided
  if [ \$# -eq 0 ]; then
    echo "Usage: burn [OPTIONS] TARGET_DIR"
    echo "Run MCP-ZERO burn folder generator. See 'burn --help' for more information."
    return 1
  fi
  
  # Execute the burn script with all arguments
  "${BURN_SCRIPT_PATH}" "\$@"
}

# Bash completion for burn command
_burn_completion() {
  local cur="\${COMP_WORDS[COMP_CWORD]}"
  local opts="--help --offline --docker --agents --db-only --api-port --dashboard-port --rpc-port"
  
  if [[ \$cur == -* ]]; then
    COMPREPLY=( \$(compgen -W "\${opts}" -- "\${cur}") )
    return 0
  fi
  
  # Complete directories for target dir
  COMPREPLY=( \$(compgen -d -- "\${cur}") )
}
complete -F _burn_completion burn
# End MCP-ZERO Burn Command
EOF
  
  print_message "${GREEN}" "Burn command added successfully to ${profile_file}"
}

# Main installation
main() {
  print_message "${BLUE}" "MCP-ZERO Burn Command Installer"
  print_message "${BLUE}" "============================="
  
  # Make sure burn script exists and is executable
  if [ ! -f "${BURN_SCRIPT_PATH}" ]; then
    print_message "${RED}" "Error: Burn script not found at ${BURN_SCRIPT_PATH}"
    exit 1
  fi
  
  chmod +x "${BURN_SCRIPT_PATH}"
  
  # Determine which profile files to update
  local profile_files=()
  
  # Add bash profile files
  if [ -f "$HOME/.bashrc" ]; then
    profile_files+=("$HOME/.bashrc")
  fi
  
  if [ -f "$HOME/.bash_profile" ]; then
    profile_files+=("$HOME/.bash_profile")
  fi
  
  # Add zsh profile if it exists
  if [ -f "$HOME/.zshrc" ]; then
    profile_files+=("$HOME/.zshrc")
  fi
  
  # If no profile files found, create .bashrc
  if [ ${#profile_files[@]} -eq 0 ]; then
    touch "$HOME/.bashrc"
    profile_files+=("$HOME/.bashrc")
  fi
  
  # Add burn command to each profile file
  for profile in "${profile_files[@]}"; do
    add_burn_command "${profile}"
  done
  
  print_message "${GREEN}" "Installation complete!"
  print_message "${YELLOW}" "To start using the 'burn' command, either:"
  print_message "${YELLOW}" "1. Run 'source ${profile_files[0]}'"
  print_message "${YELLOW}" "2. Start a new terminal session"
}

main
