#!/usr/bin/env bash
# inflight installer.
#
#   curl -fsSL https://inflight.cretezy.com/skill/install.sh | INFLIGHT_API_KEY=inf_... bash -s -- <target>
#
# Targets (default: claude):
#   claude      Claude Code skill in ~/.claude/skills/inflight
#   codex       instructions appended to ~/.codex/AGENTS.md
#   gemini      instructions appended to ~/.gemini/GEMINI.md
#   cursor      rule written to ./.cursor/rules/inflight.mdc (run inside your repo)
#   agents-md   instructions appended to ./AGENTS.md (run inside your repo)
#   none        just the `inflight` CLI
#
# Always installs the `inflight` CLI to ~/.local/bin/inflight and, if INFLIGHT_API_KEY is set,
# saves it to ~/.config/inflight/key (mode 600) so agents don't need it in their environment.
set -euo pipefail
URL="${INFLIGHT_URL:-https://inflight.cretezy.com}"
TARGET="${1:-claude}"
BIN_DIR="${INFLIGHT_BIN_DIR:-$HOME/.local/bin}"
CONF_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/inflight"
BEGIN='<!-- inflight:begin -->'
END='<!-- inflight:end -->'

fetch() { curl -fsSL "$URL/skill/$1"; }

mkdir -p "$BIN_DIR"
fetch inflight/inflight.sh > "$BIN_DIR/inflight"
chmod +x "$BIN_DIR/inflight"
echo "✓ installed CLI: $BIN_DIR/inflight"

if [[ -n "${INFLIGHT_API_KEY:-}" ]]; then
  mkdir -p "$CONF_DIR"
  (umask 077 && printf '%s\n' "$INFLIGHT_API_KEY" > "$CONF_DIR/key")
  echo "✓ saved API key to $CONF_DIR/key"
elif [[ ! -r "$CONF_DIR/key" ]]; then
  echo "! no API key saved. Create one at $URL/agents and rerun with INFLIGHT_API_KEY=... set"
fi

# Append/replace the instructions block in a markdown file, between markers (idempotent).
upsert_block() {
  local file=$1 tmp
  mkdir -p "$(dirname "$file")"
  touch "$file"
  tmp=$(mktemp)
  awk -v b="$BEGIN" -v e="$END" '$0==b{skip=1} !skip{print} $0==e{skip=0}' "$file" > "$tmp"
  { cat "$tmp"; [[ -s $tmp ]] && echo; echo "$BEGIN"; fetch instructions.md; echo "$END"; } > "$file"
  rm -f "$tmp"
  echo "✓ added inflight instructions to $file"
}

case $TARGET in
  claude)
    dest="${INFLIGHT_SKILL_DIR:-$HOME/.claude/skills/inflight}"
    mkdir -p "$dest"
    fetch inflight/SKILL.md > "$dest/SKILL.md"
    cp "$BIN_DIR/inflight" "$dest/inflight.sh"
    echo "✓ installed Claude Code skill: $dest"
    ;;
  codex) upsert_block "${CODEX_HOME:-$HOME/.codex}/AGENTS.md"
    echo "  note: Codex's sandbox blocks network by default. Allow it with"
    echo "        [sandbox_workspace_write] network_access = true  in ~/.codex/config.toml"
    ;;
  gemini) upsert_block "$HOME/.gemini/GEMINI.md" ;;
  agents-md) upsert_block "$PWD/AGENTS.md" ;;
  cursor)
    mkdir -p .cursor/rules
    { printf -- '---\ndescription: Keep the inflight board updated with what you are working on\nalwaysApply: true\n---\n\n'; fetch instructions.md; } > .cursor/rules/inflight.mdc
    echo "✓ wrote $PWD/.cursor/rules/inflight.mdc"
    ;;
  none) ;;
  *) echo "unknown target: $TARGET (claude|codex|gemini|cursor|agents-md|none)" >&2; exit 1 ;;
esac

case ":$PATH:" in *":$BIN_DIR:"*) ;; *) echo "  tip: add $BIN_DIR to PATH to run 'inflight' directly" ;; esac
