#!/bin/bash # Plan Index Manager - Installation Script set -e echo "πŸ“¦ Plan Index Manager - Installation" echo "====================================" echo "" # Detect language preference detect_language() { if [[ "$LANG" == *"zh"* ]] || [[ "$LC_ALL" == *"zh"* ]]; then echo "zh" else echo "en" fi } DEFAULT_LANG=$(detect_language) # Check prerequisites check_prerequisites() { echo "πŸ” Checking prerequisites..." # Check if Claude Code is installed if [[ ! -d "$HOME/.claude" ]]; then echo "❌ Error: Claude Code not found at ~/.claude/" echo " Please install Claude Code first: https://github.com/anthropics/claude-code" exit 1 fi # Check bash version if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then echo "⚠️ Warning: Bash 4.0+ recommended (you have ${BASH_VERSION})" fi echo "βœ… Prerequisites OK" echo "" } # Create directories create_directories() { echo "πŸ“ Creating directories..." mkdir -p "$HOME/.claude/hooks" mkdir -p "$HOME/.claude/hooks/templates" mkdir -p "$HOME/.claude/plans" echo "βœ… Directories created" echo "" } # Copy files copy_files() { echo "πŸ“‹ Copying files..." local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Copy main script cp "$script_dir/scripts/plans-sync.sh" "$HOME/.claude/hooks/plans-sync.sh" chmod +x "$HOME/.claude/hooks/plans-sync.sh" echo " βœ“ plans-sync.sh" # Copy templates cp "$script_dir/templates/INDEX.en.md" "$HOME/.claude/hooks/templates/INDEX.en.md" cp "$script_dir/templates/INDEX.zh.md" "$HOME/.claude/hooks/templates/INDEX.zh.md" echo " βœ“ Templates (en, zh)" # Create config file if [[ ! -f "$HOME/.claude/hooks/plans-sync-config.sh" ]]; then cat > "$HOME/.claude/hooks/plans-sync-config.sh" </dev/null; then # Check if plans-sync.sh is already in the hook if grep -q 'plans-sync.sh' "$settings_file"; then echo " ⏭ Hook already configured (skipped)" return 0 else echo " ⚠️ PlanModeEnd hook exists but doesn't include plans-sync.sh" echo " Please manually add to your settings.json:" echo ' {"type": "command", "command": "$HOME/.claude/hooks/plans-sync.sh", "timeout": 10000}' return 0 fi fi # Check if settings.json exists and has hooks section if [[ ! -f "$settings_file" ]]; then # Create new settings.json cat > "$settings_file" <<'EOF' { "hooks": { "PlanModeEnd": [ { "hooks": [ { "type": "command", "command": "$HOME/.claude/hooks/plans-sync.sh", "timeout": 10000 } ] } ] } } EOF echo " βœ“ Created settings.json with PlanModeEnd hook" else # Add PlanModeEnd hook to existing settings.json # This is a simple approach - for complex JSON, consider using jq if grep -q '"hooks"' "$settings_file"; then # Hooks section exists, add PlanModeEnd echo " ⚠️ Manual configuration required" echo " Please add this to your settings.json hooks section:" cat <<'EOF' "PlanModeEnd": [ { "hooks": [ { "type": "command", "command": "$HOME/.claude/hooks/plans-sync.sh", "timeout": 10000 } ] } ] EOF else echo " ⚠️ No hooks section found in settings.json" echo " Please manually add hooks configuration" fi fi echo "βœ… Hook configuration completed" echo "" } # Generate initial INDEX.md generate_initial_index() { echo "πŸ”„ Generating initial INDEX.md..." if bash "$HOME/.claude/hooks/plans-sync.sh"; then echo "βœ… Initial INDEX.md generated" else echo "⚠️ Failed to generate INDEX.md (you can run it manually later)" fi echo "" } # Print success message print_success() { echo "✨ Installation completed!" echo "" echo "πŸ“š Usage:" echo " Automatic: INDEX.md updates when you exit Plan Mode" echo " Manual: bash ~/.claude/hooks/plans-sync.sh" echo "" echo "🌍 Language:" echo " Current: $DEFAULT_LANG" echo " Switch: bash ~/.claude/hooks/plans-sync.sh --lang [en|zh]" echo "" echo "πŸ“– Documentation:" echo " English: README.md" echo " δΈ­ζ–‡: README_ZH.md" echo "" echo "πŸŽ‰ Enjoy organized planning with Claude Code!" } # Main installation flow main() { check_prerequisites create_directories copy_files configure_hook generate_initial_index print_success } # Execute main