1#!/usr/bin/env bash
 2
 3# This script manages prompt overrides for the Zed editor.
 4#
 5# It provides functionality to:
 6# 1. Link the current repository's prompt templates to Zed's configuration.
 7# 2. Create and link a separate Git worktree for prompt management.
 8# 3. Unlink previously linked prompt overrides.
 9#
10# Usage:
11#   ./script_name.sh link             # Link current repo's prompts
12#   ./script_name.sh link --worktree  # Create and link a separate worktree
13#   ./script_name.sh unlink           # Remove existing prompt override link
14#
15# The script ensures proper Git branch and worktree setup when using the
16# --worktree option. It also provides informative output and error handling.
17
18if [ "$1" = "link" ]; then
19    # Remove existing link (or directory)
20    rm -rf ~/.config/zed/prompt_overrides
21    if [ "$2" = "--worktree" ]; then
22        # Check if 'prompts' branch exists, create if not
23        if ! git show-ref --quiet refs/heads/prompts; then
24            git branch prompts
25        fi
26        # Check if 'prompts' worktree exists
27        if git worktree list | grep -q "../zed_prompts"; then
28            echo "Worktree already exists at ../zed_prompts."
29        else
30            # Create worktree if it doesn't exist
31            git worktree add ../zed_prompts prompts || git worktree add ../zed_prompts -b prompts
32        fi
33        ln -sf "$(realpath "$(pwd)/../zed_prompts/assets/prompts")" ~/.config/zed/prompt_overrides
34        echo "Linked $(realpath "$(pwd)/../zed_prompts/assets/prompts") to ~/.config/zed/prompt_overrides"
35        echo -e "\033[0;33mDon't forget you have it linked, or your prompts will go stale\033[0m"
36    else
37        ln -sf "$(pwd)/assets/prompts" ~/.config/zed/prompt_overrides
38        echo "Linked $(pwd)/assets/prompts to ~/.config/zed/prompt_overrides"
39    fi
40elif [ "$1" = "unlink" ]; then
41    if [ -e ~/.config/zed/prompt_overrides ]; then
42        # Remove symbolic link
43        rm -rf ~/.config/zed/prompt_overrides
44        echo "Unlinked ~/.config/zed/prompt_overrides"
45    else
46        echo -e "\033[33mWarning: No file exists at ~/.config/zed/prompt_overrides\033[0m"
47    fi
48else
49    echo "This script helps you manage prompt overrides for Zed."
50    echo "You can link this directory to have Zed use the contents of your current repo templates as your active prompts,"
51    echo "or store your modifications in a separate Git worktree."
52    echo
53    echo "Usage: $0 [link [--worktree]|unlink]"
54    echo
55    echo "Options:"
56    echo "  link               Create a symbolic link from ./assets/prompts to ~/.config/zed/prompt_overrides"
57    echo "  link --worktree    Create a 'prompts' Git worktree in ../prompts, then link ../prompts/assets/prompts"
58    echo "                     to ~/.config/zed/prompt_overrides"
59    echo "  unlink             Remove the symbolic link at ~/.config/zed/prompt_overrides"
60    exit 1
61fi