#!/usr/bin/env bash
# cyndra — launch Cyndra Agent (wraps Anthropic's claude CLI with Cyndra branding).
#
# Usage:
#   cyndra                 # interactive chat (auto-runs setup on first launch)
#   cyndra setup           # run setup skill in print mode — 100% Cyndra look
#   cyndra <any-claude-arg>  # pass through to claude

set -eu

# Resolve symlinks so SCRIPT_DIR points at the real script location
SOURCE="$0"
while [ -L "$SOURCE" ]; do
  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  case "$SOURCE" in /*) ;; *) SOURCE="$DIR/$SOURCE" ;; esac
done
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SPLASH="$SCRIPT_DIR/cyndra-splash.sh"

# Verify claude is installed
if ! command -v claude >/dev/null 2>&1; then
  echo "error: 'claude' CLI not found in PATH." >&2
  echo "       install with: npm install -g @anthropic-ai/claude-code" >&2
  exit 127
fi

# First-run detection — if Cyndra config dir doesn't exist, enter setup
if [ ! -d "$HOME/.config/cyndra" ] && [ "${1:-}" != "setup" ] && [ $# -eq 0 ]; then
  exec "$0" setup
fi

# Setup mode → splash then run the /setup skill in print mode (no Claude TUI)
if [ "${1:-}" = "setup" ]; then
  clear 2>/dev/null || true
  bash "$SPLASH"
  shift
  exec claude --print "/setup" "$@"
fi

# All other invocations → splash then interactive claude
bash "$SPLASH"
exec claude "$@"
