#!/usr/bin/env bash
# The host0 skill installer, served at /install.sh with its origin filled in:
#
#   curl -fsSL https://host0.ai/install.sh | bash
#   curl -fsSL https://host0.ai/install.sh | bash -s -- --agent codex --force
#
# Downloads the same two files as /.well-known/skills/index.json lists (SKILL.md,
# scripts/publish.sh) into your agent's skills folder under $HOME. It never uses
# sudo, never touches a shell profile or ~/.host0, and never overwrites a copy you
# have changed unless you pass --force. Everything sits in main(), so a download
# cut off halfway runs nothing.

main() {
  set -euo pipefail
  local origin="https://host0.ai"
  local force=0 dir="" agents=""

  usage() {
    cat <<USAGE
Usage: install.sh [--agent claude,codex,cursor] [--dir PATH] [--force]

  --agent LIST  Where to install: claude (~/.claude/skills), codex (~/.agents/skills),
                cursor (~/.cursor/skills). Default: every agent found in your home folder.
  --dir PATH    Install into PATH (the folder that will hold SKILL.md) instead.
  --force       Replace a copy that differs. The old one is kept next to it as host0.bak.<time>.
USAGE
  }

  while [ $# -gt 0 ]; do
    case "$1" in
      --agent) [ $# -ge 2 ] || { usage >&2; exit 2; }; agents="$2"; shift 2 ;;
      --dir) [ $# -ge 2 ] || { usage >&2; exit 2; }; dir="$2"; shift 2 ;;
      --force) force=1; shift ;;
      -h|--help) usage; exit 0 ;;
      *) echo "host0 install: unknown option $1" >&2; usage >&2; exit 2 ;;
    esac
  done

  command -v curl >/dev/null 2>&1 || { echo "host0 install: curl is required" >&2; exit 1; }
  [ -n "${HOME:-}" ] || [ -n "$dir" ] || { echo "host0 install: HOME is not set; pass --dir" >&2; exit 1; }

  local targets=()
  if [ -n "$dir" ]; then
    targets=("$dir")
  elif [ -n "$agents" ]; then
    local agent
    for agent in $(printf '%s' "$agents" | tr ',' ' '); do
      case "$agent" in
        claude) targets+=("$HOME/.claude/skills/host0") ;;
        codex) targets+=("$HOME/.agents/skills/host0") ;;
        cursor) targets+=("$HOME/.cursor/skills/host0") ;;
        *) echo "host0 install: unknown agent $agent (use claude, codex or cursor)" >&2; exit 2 ;;
      esac
    done
  else
    # Cursor also reads ~/.claude/skills, so one folder covers both.
    if [ -d "$HOME/.claude" ]; then targets+=("$HOME/.claude/skills/host0")
    elif [ -d "$HOME/.cursor" ]; then targets+=("$HOME/.cursor/skills/host0"); fi
    if [ -d "$HOME/.codex" ] || [ -d "$HOME/.agents" ]; then targets+=("$HOME/.agents/skills/host0"); fi
    if [ "${#targets[@]}" -eq 0 ]; then targets=("$HOME/.agents/skills/host0"); fi
  fi

  # Global, not local: the trap runs after main has returned.
  tmp="$(mktemp -d)"
  trap 'rm -rf "$tmp"' EXIT
  mkdir -p "$tmp/scripts"
  curl -fsSL --retry 2 "$origin/.well-known/skills/host0/SKILL.md" -o "$tmp/SKILL.md"
  curl -fsSL --retry 2 "$origin/.well-known/skills/host0/scripts/publish.sh" -o "$tmp/scripts/publish.sh"
  head -n 1 "$tmp/SKILL.md" | grep -q '^---' || { echo "host0 install: $origin didn't send a skill file" >&2; exit 1; }
  head -n 1 "$tmp/scripts/publish.sh" | grep -q '^#!' || { echo "host0 install: $origin didn't send a script" >&2; exit 1; }
  chmod 644 "$tmp/SKILL.md"
  chmod 755 "$tmp/scripts/publish.sh"

  # Check every target before writing to any, so a refusal leaves nothing half done.
  local target
  if [ "$force" -eq 0 ]; then
    for target in "${targets[@]}"; do
      if [ -e "$target" ] && ! same_files "$tmp" "$target"; then
        echo "host0 install: $target exists and differs from the current skill. Nothing was changed." >&2
        echo "To replace it (the old copy is kept next to it), run the same command with --force:" >&2
        echo "  curl -fsSL $origin/install.sh | bash -s -- --force" >&2
        exit 1
      fi
    done
  fi

  echo "host0 skill from $origin"
  for target in "${targets[@]}"; do
    if [ -e "$target" ] && same_files "$tmp" "$target"; then
      echo "  already up to date: $target"
      continue
    fi
    local action="installed"
    if [ -e "$target" ]; then
      local backup="$target.bak.$(date +%Y%m%d%H%M%S)"
      mv "$target" "$backup"
      echo "  kept the old copy:  $backup"
      action="replaced"
    fi
    mkdir -p "$target/scripts"
    cp "$tmp/SKILL.md" "$target/SKILL.md"
    cp "$tmp/scripts/publish.sh" "$target/scripts/publish.sh"
    chmod 755 "$target/scripts/publish.sh"
    echo "  $action: $target (SKILL.md, scripts/publish.sh)"
  done
  echo
  echo "Next: ask your agent to \"deploy this to host0\". The first publish shows a link and a"
  echo "code to approve in your browser. No sudo was used, and no key or shell profile was touched."
}

same_files() {
  cmp -s "$1/SKILL.md" "$2/SKILL.md" && cmp -s "$1/scripts/publish.sh" "$2/scripts/publish.sh"
}

main "$@"
