#!/usr/bin/env bash
#
# install.sh — install the marquee-share skill into your personal Claude Code.
#
# Claude Code discovers skills by reading SKILL.md from each subdirectory of
# ~/.claude/skills/. This script symlinks ~/.claude/skills/marquee-share to
# this repo's skill/ directory, so the skill you run is always the source in
# this checkout — pull the branch and the skill updates with it.
#
# Usage:
#   ./install.sh              install (idempotent — safe to re-run)
#   ./install.sh --uninstall  remove the symlink
#   ./install.sh --help       show this message
#
# The skill's canonical source lives in M-KOPA/marquee under skill/. This is
# the personal-developer install path. M-KOPA-wide distribution goes through
# the Launchpad CLI skills bundle.

set -euo pipefail

# Resolve the directory this script lives in (the skill/ root), following
# symlinks so it works whether invoked directly or via an installed link.
script_source="${BASH_SOURCE[0]}"
while [ -L "$script_source" ]; do
  dir="$(cd -P "$(dirname "$script_source")" >/dev/null 2>&1 && pwd)"
  script_source="$(readlink "$script_source")"
  [[ "$script_source" != /* ]] && script_source="$dir/$script_source"
done
SKILL_DIR="$(cd -P "$(dirname "$script_source")" >/dev/null 2>&1 && pwd)"

SKILLS_HOME="${CLAUDE_SKILLS_HOME:-$HOME/.claude/skills}"
LINK_PATH="$SKILLS_HOME/marquee-share"

usage() {
  sed -n '3,17p' "$0" | sed 's/^# \{0,1\}//'
}

uninstall() {
  if [ -L "$LINK_PATH" ]; then
    rm "$LINK_PATH"
    echo "removed: $LINK_PATH"
  elif [ -e "$LINK_PATH" ]; then
    echo "error: $LINK_PATH exists but is not a symlink — not removing it." >&2
    echo "       Inspect and remove it by hand if you want it gone." >&2
    exit 1
  else
    echo "nothing to do: $LINK_PATH does not exist"
  fi
}

install() {
  if [ ! -f "$SKILL_DIR/SKILL.md" ]; then
    echo "error: SKILL.md not found in $SKILL_DIR — run this from the skill/ dir." >&2
    exit 1
  fi

  mkdir -p "$SKILLS_HOME"

  # Idempotent: if the link already points where we want, do nothing.
  if [ -L "$LINK_PATH" ]; then
    current="$(readlink "$LINK_PATH")"
    if [ "$current" = "$SKILL_DIR" ]; then
      echo "already installed: $LINK_PATH -> $SKILL_DIR"
      post_install_note
      return
    fi
    echo "updating existing link ($current -> $SKILL_DIR)"
    rm "$LINK_PATH"
  elif [ -e "$LINK_PATH" ]; then
    echo "error: $LINK_PATH exists and is not a symlink." >&2
    echo "       Move or remove it, then re-run ./install.sh." >&2
    exit 1
  fi

  ln -s "$SKILL_DIR" "$LINK_PATH"
  echo "installed: $LINK_PATH -> $SKILL_DIR"
  post_install_note
}

post_install_note() {
  # The skill runs from the pre-built, dependency-free bundle
  # dist/cli.js — no `npm install` is needed to use it. Only warn if
  # the committed build artefact is somehow missing from the checkout.
  if [ ! -f "$SKILL_DIR/dist/cli.js" ]; then
    echo
    echo "warning: $SKILL_DIR/dist/cli.js is missing — the skill needs"
    echo "         the built bundle to run. Rebuild it with —"
    echo "         (cd '$SKILL_DIR' && npm install && npm run build)"
  fi
  echo
  echo "Restart Claude Code (or start a new session) to pick up the skill."
}

case "${1:-}" in
  ""|install)        install ;;
  --uninstall|-u)    uninstall ;;
  --help|-h)         usage ;;
  *)
    echo "error: unknown argument '$1'" >&2
    echo "run './install.sh --help' for usage." >&2
    exit 1
    ;;
esac
