#!/usr/bin/env sh
# hstack installer · h.computer
# usage: curl -fsSL https://h.computer/install.sh | sh
# opts:  HSTACK_DIR=~/.hstack  HSTACK_BASE=https://h.computer
set -e

DIR="${HSTACK_DIR:-./hstack}"
BASE="${HSTACK_BASE:-https://h.computer}"

printf '\n  ┌─────────────────────────────────────────┐\n'
printf   '  │  hstack · h.computer skill + MCP bundle │\n'
printf   '  └─────────────────────────────────────────┘\n\n'

echo "→ installing into $DIR"
mkdir -p "$DIR/skills" "$DIR/mcp" "$DIR/openapi"

# ─────────────────────── AGENTS.md (read first by Claude Code, Codex, Cursor)
cat > "$DIR/AGENTS.md" <<EOF
# hstack — h.computer

Portable skill + MCP bundle for h.computer. Any local CLI agent that reads
\`AGENTS.md\` (Claude Code, Codex, Cursor, Aider, Continue, OpenCode, Pi,
Hermes, Zed) picks this up automatically.

## Quickstart for the user

If \`\$H_API_KEY\` is not set, walk the human through these 3 steps:

  1. Open $BASE/auth and sign in (email or SMS, no password).
  2. Open $BASE/admin and reveal an \`hk_…\` key.
  3. Export it:   \`export H_API_KEY=hk_xxx\`

Then verify with a read call (no key required):

    curl -s $BASE/api/v1/now | jq .

And a write call (uses \$H_API_KEY):

    curl -s -X POST $BASE/api/v1/status \\
      -H "X-H-Api-Key: \$H_API_KEY" -H "Content-Type: application/json" \\
      -d '{"text":"hello from my CLI agent","kind":"status"}'

## What's in here

- \`skills/\` — one markdown file per task (post-status, queue-blog-idea,
  append-journal, recompile-journal, search). Load these as Claude Skills,
  Cursor rules, or just \`cat\` them into a system prompt.
- \`mcp/\` — ready-to-paste MCP configs for Claude Desktop, Claude Code,
  Cursor, Zed, Codex, and the raw \`mcp.json\` wire format.
- \`openapi/openapi.json\` — full OpenAPI spec, generated from $BASE.
- \`SETUP.md\` — same 3-step quickstart, human-readable.

## Endpoints

Base URL: $BASE/api/v1   ·   MCP: $BASE/api/public/mcp   ·   Docs: $BASE/docs/hstack

Public reads (no key):  /now /today /feed /blog /papers /journal /stats /search /digests
Owner writes (hk_ key): POST /status, POST /post-idea, POST /log,
                        PATCH /journal, DELETE /journal,
                        POST /journal/recompile
EOF

# ─────────────────────── SETUP.md (the human-facing version)
cat > "$DIR/SETUP.md" <<EOF
# hstack setup

1. **Sign in** — open $BASE/auth (email or SMS code, no password).
2. **Make a key** — open $BASE/admin and reveal an \`hk_…\` key.
3. **Export it** — \`export H_API_KEY=hk_xxx\` (add to your shell rc).

Test:    \`curl -s $BASE/api/v1/now | jq .\`

Your CLI agent can now read this folder and call the API on your behalf.
EOF

# ─────────────────────── MCP configs
cat > "$DIR/mcp/h.computer.json" <<EOF
{
  "mcpServers": {
    "h.computer": {
      "url": "$BASE/api/public/mcp",
      "headers": {
        "X-H-Api-Key": "\${H_API_KEY}",
        "Accept": "application/json, text/event-stream"
      }
    }
  }
}
EOF

cat > "$DIR/mcp/claude-desktop.json" <<EOF
{
  "mcpServers": {
    "h.computer": {
      "command": "npx",
      "args": ["-y", "mcp-remote", "$BASE/api/public/mcp",
               "--header", "X-H-Api-Key:\${H_API_KEY}"],
      "env": { "H_API_KEY": "hk_xxx_replace_me" }
    }
  }
}
EOF

cat > "$DIR/mcp/cursor.json" <<EOF
{
  "mcpServers": {
    "h.computer": {
      "url": "$BASE/api/public/mcp",
      "headers": { "X-H-Api-Key": "\${H_API_KEY}" }
    }
  }
}
EOF

# ─────────────────────── Skills
cat > "$DIR/skills/post-status.md" <<EOF
---
name: post-status
description: Push a status update to h.computer's living feed.
auth: hk_ key required
---
POST $BASE/api/v1/status

curl -X POST $BASE/api/v1/status \\
  -H "X-H-Api-Key: \$H_API_KEY" -H "Content-Type: application/json" \\
  -d '{"text":"shipped X","kind":"status"}'
EOF

cat > "$DIR/skills/queue-blog-idea.md" <<EOF
---
name: queue-blog-idea
description: Drop a blog idea into the daily draft queue.
auth: hk_ key required
---
POST $BASE/api/v1/post-idea

curl -X POST $BASE/api/v1/post-idea \\
  -H "X-H-Api-Key: \$H_API_KEY" -H "Content-Type: application/json" \\
  -d '{"idea":"why agents need taste","tags":["agents"]}'
EOF

cat > "$DIR/skills/append-journal.md" <<EOF
---
name: append-journal
description: Append a non-destructive note to today's journal entry.
auth: hk_ key required
---
POST $BASE/api/v1/log

curl -X POST $BASE/api/v1/log \\
  -H "X-H-Api-Key: \$H_API_KEY" -H "Content-Type: application/json" \\
  -d '{"heading":"midday","text":"shipped the docs revamp","source":"cli"}'
EOF

cat > "$DIR/skills/recompile-journal.md" <<EOF
---
name: recompile-journal
description: Re-run the AI compiler for a given day. Rewrites title/summary/body.
auth: hk_ key required
---
POST $BASE/api/v1/journal/recompile

curl -X POST $BASE/api/v1/journal/recompile \\
  -H "X-H-Api-Key: \$H_API_KEY" -H "Content-Type: application/json" \\
  -d '{"date":"YYYY-MM-DD","force":true}'
EOF

cat > "$DIR/skills/search.md" <<EOF
---
name: search
description: Full-text search across blog, journal, and papers. No key needed.
---
GET $BASE/api/v1/search?q=YOUR_QUERY&kinds=blog,journal,paper
EOF

# ─────────────────────── OpenAPI snapshot
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$BASE/api/v1/openapi.json" -o "$DIR/openapi/openapi.json" 2>/dev/null || true
fi

# ─────────────────────── Done
printf '\n✓ installed at %s\n\n' "$DIR"
printf 'next:\n'
printf '  1. open %s/auth   (sign in — email or SMS, no password)\n' "$BASE"
printf '  2. open %s/admin  (reveal an hk_ key)\n' "$BASE"
printf '  3. export H_API_KEY=hk_xxx\n\n'
printf 'verify:\n'
printf '  curl -s %s/api/v1/now | jq .\n\n' "$BASE"
printf 'docs:  %s/docs/hstack\n' "$BASE"
printf 'mcp:   %s/mcp/h.computer.json (paste into your agent config)\n\n' "$DIR"
