#!/bin/bash
set -e

PKG_DIR="$(cd "$(dirname "$0")" && pwd)"

# 1. Symlink the CLI onto PATH. Symlinking (not copying) means a reinstall/update
# never leaves a stale binary behind.
BIN_DIR="${HOME}/.local/bin"
mkdir -p "$BIN_DIR"
chmod +x "$PKG_DIR/bin/prose-lint" 2>/dev/null || true
ln -sfn "$PKG_DIR/bin/prose-lint" "$BIN_DIR/prose-lint"

# 2. Deliver the skill so Claude/Codex discover it (same pattern as core skills).
SKILLS_DIR="${HOME}/.claude/skills"
mkdir -p "$SKILLS_DIR/prose-lint"
[ -f "$PKG_DIR/SKILL.md" ] && cp "$PKG_DIR/SKILL.md" "$SKILLS_DIR/prose-lint/SKILL.md"
if [ -d "${HOME}/.codex/skills" ]; then
  mkdir -p "${HOME}/.codex/skills/prose-lint"
  [ -f "$PKG_DIR/SKILL.md" ] && cp "$PKG_DIR/SKILL.md" "${HOME}/.codex/skills/prose-lint/SKILL.md"
fi

if ! command -v node >/dev/null 2>&1; then
  echo "prose-lint installed, but 'node' was not found on PATH. The CLI needs Node 16+ to run."
else
  echo "prose-lint installed -> $BIN_DIR/prose-lint"
fi