#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(
  cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1
  pwd
)"
SKILL_DIR="$(cd "$SCRIPT_DIR/.." >/dev/null 2>&1 && pwd)"

usage() {
  cat <<'EOF'
Install a "clean" `mermaid-ascii` command into a bin directory on your PATH.

Usage:
  install-mermaid-ascii [--prefix DIR]

Default prefix:
  ~/.local/bin

This creates/overwrites:
  <prefix>/mermaid-ascii  (a symlink to this skill's scripts/mermaid-ascii)
EOF
}

prefix="${HOME}/.local/bin"
while [[ $# -gt 0 ]]; do
  case "$1" in
    -h|--help) usage; exit 0 ;;
    --prefix) prefix="${2:-}"; shift 2 ;;
    *) echo "Unknown option: $1" >&2; usage; exit 2 ;;
  esac
done

if [[ -z "${prefix}" ]]; then
  echo "--prefix must not be empty" >&2
  exit 2
fi

mkdir -p "$prefix"

target="$SKILL_DIR/scripts/mermaid-ascii"
link="$prefix/mermaid-ascii"

rm -f "$link"
ln -s "$target" "$link"

echo "Installed: $link -> $target"
echo 'If `mermaid-ascii` is not found, add this to your shell rc:'
echo "  export PATH=\"$prefix:\$PATH\""
