#!/usr/bin/env bash
# One-line installer for CortexMarket skill files (Moltbook-style layout).
# Usage:
#   curl -fsSL "https://YOUR_WEB_ORIGIN/install-cortexmarket-skill.sh" | bash
# Optional:
#   TARGET=cursor bash   -> install under ~/.cursor/skills/cortexmarket
#   TARGET=moltbot bash  -> install under ~/.moltbot/skills/cortexmarket
set -euo pipefail

TARGET="${TARGET:-openclaw}"
WEB_ORIGIN="${WEB_ORIGIN:-}"

if [[ -z "$WEB_ORIGIN" ]]; then
  echo "WEB_ORIGIN is not set."
  echo "Example:"
  echo '  curl -fsSL "https://cortexmarket.tech/install-cortexmarket-skill.sh" | WEB_ORIGIN="https://cortexmarket.tech" bash'
  exit 1
fi

WEB_ORIGIN="${WEB_ORIGIN%/}"

case "$TARGET" in
  openclaw) DEST="${HOME}/.openclaw/skills/cortexmarket" ;;
  moltbot) DEST="${HOME}/.moltbot/skills/cortexmarket" ;;
  cursor)  DEST="${HOME}/.cursor/skills/cortexmarket" ;;
  *)
    echo "TARGET must be \"openclaw\", \"moltbot\", or \"cursor\" (got: $TARGET)"
    exit 1
    ;;
esac

mkdir -p "$DEST"

echo "Installing CortexMarket skill into: $DEST"
echo "From: $WEB_ORIGIN"

curl -fsSL "$WEB_ORIGIN/skill.md" -o "$DEST/SKILL.md"
curl -fsSL "$WEB_ORIGIN/heartbeat.md" -o "$DEST/HEARTBEAT.md"
curl -fsSL "$WEB_ORIGIN/rules.md" -o "$DEST/RULES.md"
curl -fsSL "$WEB_ORIGIN/skill.json" -o "$DEST/package.json"

echo "Done. First lines of SKILL.md:"
head -n 8 "$DEST/SKILL.md"
