#!/usr/bin/env bash
# ------------------------------------------------------------------
# Point the Open Graph / WhatsApp share tags at whatever domain this
# copy of the site is actually deployed on.
#
#   ./set-og-domain.sh https://preview.paradisepaddlecayman.com
#   ./set-og-domain.sh https://www.paradisepaddlecayman.com  --live
#
# Without --live it also stamps <meta name="robots" content="noindex">
# on every page, so a staging subdomain never gets indexed by Google.
# ------------------------------------------------------------------
set -euo pipefail

NEW="${1:-}"
MODE="${2:-}"

if [ -z "$NEW" ]; then
  echo "usage: ./set-og-domain.sh https://your-subdomain.tld [--live]" >&2
  exit 1
fi

NEW="${NEW%/}"                                   # strip trailing slash
CUR=$(grep -m1 -oE 'OG_BASE: https?://[^ ]+' index.html | sed 's/OG_BASE: //')

echo "Rewriting share URLs:"
echo "  from  $CUR"
echo "  to    $NEW"

for f in index.html rentals.html about.html contact.html; do
  sed -i "s|${CUR}|${NEW}|g" "$f"

  # robots tag: noindex on staging, remove it when going live
  sed -i '/<meta name="robots" content="noindex,nofollow">/d' "$f"
  if [ "$MODE" != "--live" ]; then
    sed -i 's|<meta name="theme-color"|<meta name="robots" content="noindex,nofollow">\n<meta name="theme-color"|' "$f"
  fi

  echo "  updated $f"
done

echo
if [ "$MODE" = "--live" ]; then
  echo "Done - live mode, pages are indexable."
else
  echo "Done - staging mode, pages carry noindex,nofollow."
fi
echo "Re-scrape the preview after deploying:"
echo "  https://developers.facebook.com/tools/debug/?q=${NEW}"
