#!/usr/bin/env bash
set -euo pipefail

slug="$1"
url="$2"

echo "[$slug] Fetching $url" >&2
html=$(curl -sL --max-time 15 "$url" 2>/dev/null) || { echo "[$slug] FETCH FAILED" >&2; echo ""; exit 0; }

# Try multiple og:image patterns
img_url=$(echo "$html" | grep -oP 'property="og:image"\s+content="[^"]*"' | sed 's/.*content="//;s/"//' | head -1)

if [ -z "$img_url" ]; then
  img_url=$(echo "$html" | grep -oP 'name="og:image"\s+content="[^"]*"' | sed 's/.*content="//;s/"//' | head -1)
fi

if [ -z "$img_url" ]; then
  img_url=$(echo "$html" | grep -oP 'content="[^"]*"[^>]*property="og:image"' | sed 's/.*content="//;s/".*//' | head -1)
fi

if [ -n "$img_url" ]; then
  echo "[$slug] Found: $img_url" >&2
  echo "$img_url"
else
  echo "[$slug] No og:image found" >&2
fi
