#!/bin/bash
# Remove nutshell-skills symlinks from Claude/OpenCode/Codex global skill directories
#
# Usage: ./unsync-skills.sh
#
# Removes directory symlinks from:
#   - ~/.claude/skills/<name>/
#   - ~/.config/opencode/skills/<name>/
#   - ~/.codex/skills/<name>/

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SKILLS_DIR="${SCRIPT_DIR}/skills"

# Target directories
CLAUDE_SKILLS_DIR="${HOME}/.claude/skills"
OPENCODE_SKILLS_DIR="${HOME}/.config/opencode/skills"
CODEX_HOME="${CODEX_HOME:-${HOME}/.codex}"
CODEX_SKILLS_DIR="${CODEX_HOME}/skills"

# Returns 0 if $1 is a symlink whose resolved target is inside $SKILLS_DIR.
is_our_link() {
    local link="$1"
    [ -L "$link" ] || return 1
    local target
    target="$(readlink "$link")"
    # Resolve relative symlinks relative to the link's parent directory.
    if [[ "$target" != /* ]]; then
        target="$(cd "$(dirname "$link")" && cd "$target" && pwd 2>/dev/null)" || return 1
    fi
    [[ "$target" == "${SKILLS_DIR}"/* || "$target" == "${SKILLS_DIR}" ]]
}

echo "Removing skills symlinks from global directories"
echo ""

# Find all skill directories (those containing SKILL.md)
# Support flat (skills/*/), two-level (skills/*/*/), and three-level (skills/*/*/*/) structures
removed_count=0
skipped_count=0
for skill_path in "${SKILLS_DIR}"/*/ "${SKILLS_DIR}"/*/*/ "${SKILLS_DIR}"/*/*/*/; do
    # Skip if path doesn't exist (when one pattern has no matches)
    [ -d "$skill_path" ] || continue

    skill_name=$(basename "$skill_path")

    # Skip if no SKILL.md
    if [ ! -f "${skill_path}/SKILL.md" ]; then
        continue
    fi

    # Remove Claude skills symlink
    claude_link="${CLAUDE_SKILLS_DIR}/${skill_name}"
    if [ -L "$claude_link" ]; then
        if is_our_link "$claude_link"; then
            echo "  REMOVE: ~/.claude/skills/${skill_name}"
            rm "$claude_link"
            removed_count=$((removed_count + 1))
        else
            echo "  SKIP (foreign link): ~/.claude/skills/${skill_name}"
            skipped_count=$((skipped_count + 1))
        fi
    fi

    # Remove Codex skills symlink
    codex_link="${CODEX_SKILLS_DIR}/${skill_name}"
    if [ -L "$codex_link" ]; then
        if is_our_link "$codex_link"; then
            echo "  REMOVE: ${CODEX_SKILLS_DIR/#$HOME/~}/${skill_name}"
            rm "$codex_link"
            removed_count=$((removed_count + 1))
        else
            echo "  SKIP (foreign link): ${CODEX_SKILLS_DIR/#$HOME/~}/${skill_name}"
            skipped_count=$((skipped_count + 1))
        fi
    fi

    # Remove OpenCode skills symlink
    opencode_link="${OPENCODE_SKILLS_DIR}/${skill_name}"
    # Also clean up legacy single-file symlinks
    if [ -L "${opencode_link}.md" ]; then
        if is_our_link "${opencode_link}.md"; then
            echo "  REMOVE: ~/.config/opencode/skills/${skill_name}.md (legacy)"
            rm -f "${opencode_link}.md"
            removed_count=$((removed_count + 1))
        else
            echo "  SKIP (foreign link): ~/.config/opencode/skills/${skill_name}.md"
            skipped_count=$((skipped_count + 1))
        fi
    fi
    if [ -L "$opencode_link" ]; then
        if is_our_link "$opencode_link"; then
            echo "  REMOVE: ~/.config/opencode/skills/${skill_name}"
            rm "$opencode_link"
            removed_count=$((removed_count + 1))
        else
            echo "  SKIP (foreign link): ~/.config/opencode/skills/${skill_name}"
            skipped_count=$((skipped_count + 1))
        fi
    fi
done

echo ""
if [ $removed_count -eq 0 ]; then
    echo "No symlinks found to remove."
else
    echo "Done! Removed ${removed_count} symlink(s)."
fi
if [ $skipped_count -gt 0 ]; then
    echo "Skipped ${skipped_count} foreign symlink(s)."
fi
echo ""
