#!/bin/sh
# OmniForge · VibeShield v7.0 Zero-Install Terminal Security & Launch Auditor
# Source: https://api.shipbastion.com/cli.sh
# Inspectable POSIX sh script: sends only the target domain you provide to https://api.shipbastion.com/api/scan-cli.
set -eu

API_ORIGIN="https://api.shipbastion.com"
TARGET=""
SUITE=0
FORMAT="text"
COLOR_MODE="auto"
FAIL_BELOW=""
PASS_TOKEN="${VIBESHIELD_PASS:-}"

print_usage() {
  cat <<'EOF'
OmniForge · VibeShield v7.0 — Zero-Install Terminal Security Audit (6 Phases, 116+ Checks)

Usage:
  curl -fsSL https://api.shipbastion.com/cli.sh | sh -s -- [options] <domain-or-url>

Options:
  --suite, --all         Run all 3 engines in parallel (VibeShield + InboxShield + CiteReady)
  --json                 Print raw JSON scan payload (pipe cleanly to jq)
  --fail-below <0-100>   CI/CD gate: exit with code 2 if security score is below threshold
  --pass <token>         Unlock Pro remediation code in terminal (or set VIBESHIELD_PASS)
  --color / --no-color   Force or disable ANSI terminal colors (honors NO_COLOR)
  -h, --help             Show this help message

Examples:
  curl -fsSL https://api.shipbastion.com/cli.sh | sh -s -- supabase.com
  curl -fsSL https://shipbastion.com/cli.sh | sh -s -- --fail-below 80 myapp.com
  curl -fsSL https://api.shipbastion.com/cli.sh | sh -s -- --suite myapp.com
  curl -fsSL https://shipbastion.com/cli.sh | sh -s -- --json myapp.com | jq '.assessment'
EOF
}

if ! command -v curl >/dev/null 2>&1; then
  printf 'Error: curl is required to run OmniForge CLI.
' >&2
  exit 1
fi

while [ "$#" -gt 0 ]; do
  case "$1" in
    -h|--help)
      print_usage
      exit 0
      ;;
    --suite|--all)
      SUITE=1
      shift
      ;;
    --json)
      FORMAT="json"
      shift
      ;;
    --no-color)
      COLOR_MODE="off"
      shift
      ;;
    --color)
      COLOR_MODE="on"
      shift
      ;;
    --fail-below)
      if [ "$#" -lt 2 ]; then
        printf 'Error: --fail-below requires an integer (0-100)
' >&2
        exit 2
      fi
      FAIL_BELOW="$2"
      shift 2
      ;;
    --fail-below=*)
      FAIL_BELOW="${1#*=}"
      shift
      ;;
    --pass)
      if [ "$#" -lt 2 ]; then
        printf 'Error: --pass requires a Pro Pass token
' >&2
        exit 2
      fi
      PASS_TOKEN="$2"
      shift 2
      ;;
    --pass=*)
      PASS_TOKEN="${1#*=}"
      shift
      ;;
    --)
      shift
      if [ "$#" -gt 0 ] && [ -z "$TARGET" ]; then
        TARGET="$1"
        shift
      fi
      ;;
    -*)
      printf 'Error: unknown option: %s
Run with --help for usage.
' "$1" >&2
      exit 2
      ;;
    *)
      if [ -z "$TARGET" ]; then
        TARGET="$1"
      fi
      shift
      ;;
  esac
done

if [ -z "$TARGET" ]; then
  print_usage
  exit 0
fi

if [ -n "$FAIL_BELOW" ]; then
  case "$FAIL_BELOW" in
    *[!0-9]*)
      printf 'Error: --fail-below must be an integer between 0 and 100 (got: %s)
' "$FAIL_BELOW" >&2
      exit 2
      ;;
  esac
  if [ "$FAIL_BELOW" -gt 100 ]; then
    printf 'Error: --fail-below must be <= 100
' >&2
    exit 2
  fi
fi

USE_COLOR=1
if [ "$COLOR_MODE" = "off" ]; then
  USE_COLOR=0
elif [ "$COLOR_MODE" = "on" ]; then
  USE_COLOR=1
else
  if [ -n "${NO_COLOR:-}" ] || [ ! -t 1 ]; then
    USE_COLOR=0
  fi
fi

HDR_FILE="$(mktemp "${TMPDIR:-/tmp}/omniforge-cli-hdr.XXXXXX")"
BODY_FILE="$(mktemp "${TMPDIR:-/tmp}/omniforge-cli-body.XXXXXX")"
trap 'rm -f "$HDR_FILE" "$BODY_FILE"' EXIT INT TERM

if [ "$FORMAT" != "json" ] && [ -t 2 ]; then
  if [ "$SUITE" -eq 1 ]; then
    printf 'Scanning %s across VibeShield v7.0 + InboxShield + CiteReady...
' "$TARGET" >&2
  else
    printf 'Scanning %s with VibeShield v7.0 (6 phases, 116+ checks)...
' "$TARGET" >&2
  fi
fi

set -- -sS --max-time 28 -D "$HDR_FILE" -o "$BODY_FILE" -G
set -- "$@" --data-urlencode "url=$TARGET"
set -- "$@" --data-urlencode "color=$USE_COLOR"
set -- "$@" --data-urlencode "suite=$SUITE"
set -- "$@" --data-urlencode "format=$FORMAT"
if [ -n "$PASS_TOKEN" ]; then
  set -- "$@" -H "X-VibeShield-Pass: $PASS_TOKEN"
fi

if ! curl "$@" "${API_ORIGIN}/api/scan-cli"; then
  printf 'Error: failed to reach %s/api/scan-cli
' "$API_ORIGIN" >&2
  exit 1
fi

HTTP_CODE="$(awk '/^HTTP\// {code=$2} END {print code}' "$HDR_FILE" | tr -d '\r\n')"
cat "$BODY_FILE"

if [ -n "$HTTP_CODE" ] && [ "$HTTP_CODE" -ge 400 ] 2>/dev/null; then
  exit 1
fi

if [ -n "$FAIL_BELOW" ]; then
  SCORE="$(grep -i '^x-vibeshield-score:' "$HDR_FILE" | tail -n 1 | cut -d':' -f2 | tr -d ' \r\n' || true)"
  case "$SCORE" in
    ''|*[!0-9]*)
      printf 'CI Gate Warning: could not parse X-VibeShield-Score header.
' >&2
      exit 2
      ;;
  esac
  if [ "$SCORE" -lt "$FAIL_BELOW" ]; then
    printf 'CI Gate FAILED: VibeShield score %s/100 is below threshold %s/100.
' "$SCORE" "$FAIL_BELOW" >&2
    exit 2
  fi
fi
