#!/usr/bin/env bash
# Usage: verify.sh <url> [--allow-content-disposition]
# Fail non-200 responses or Content-Disposition, except the explicit FC default-domain allowance.
# Write failure reasons to stderr for the daemon error summary.
set -euo pipefail
URL="${1:?用法: verify.sh <url> [--allow-content-disposition]}"
ALLOW_CD=0
[ "${2:-}" = "--allow-content-disposition" ] && ALLOW_CD=1

echo "== 健康检查: $URL =="
code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 30 "$URL/" || echo 000)
ctype=$(curl -s -o /dev/null -w "%{content_type}" --max-time 30 "$URL/" || echo "")
echo "首页: HTTP $code  ($ctype)"

hdr=$(curl -s -D - -o /dev/null --max-time 30 "$URL/" || true)
has_cd=0
if echo "$hdr" | grep -qi 'content-disposition'; then
  has_cd=1
  if [ "$ALLOW_CD" = "1" ]; then
    echo "⚠️  检测到 Content-Disposition —— 浏览器会下载而非渲染。这是 FC 默认域名(*.fcapp.run)的平台行为,"
    echo "    使用者自带 key 履约下没有自定义域名可绑,按验收口径只警告不判失败。要浏览器直出得绑自定义域名。"
  else
    echo "❌ 检测到 Content-Disposition —— 浏览器会下载而非渲染!确认走的是 fc3-domain 自定义域名,而不是 fcapp.run" >&2
  fi
else
  echo "✓ 无 Content-Disposition,浏览器可正常渲染网页"
fi

if [ "$code" = "200" ] && { [ "$has_cd" = "0" ] || [ "$ALLOW_CD" = "1" ]; }; then
  echo "✓ 全链路 OK"
  exit 0
fi

if [ "$code" != "200" ]; then
  echo "❌ verify failed: 首页 HTTP $code (期望 200)；查 s logs 找原因" >&2
fi
exit 1
