#!/usr/bin/env bash
# Sync multipage landing from repo to BE hub root.
# Usage:
#   ./scripts/sync-landing-to-be.sh
#   ./scripts/sync-landing-to-be.sh /path/to/BE/.../androidcast_project
#   REMOTE=alpine-be ./scripts/sync-landing-to-be.sh
#
# After sync, verify:
#   curl -sS 'https://apps.f0xx.org/app/androidcast_project/' | head -20
# Expect: title "Android Cast — Landing", link hub-landing.css, body hub-landing
set -euo pipefail

SRC="$(cd "$(dirname "$0")/.." && pwd)"
REPO="$(cd "$SRC/../.." && pwd)"
DEFAULT_DEST="$REPO/tmp/BE_alpine/var/www/localhost/htdocs/apps/app/androidcast_project"
DEST="${1:-$DEFAULT_DEST}"
REMOTE="${REMOTE:-}"

hub_files=(
  index.php
  index.html
  hub.css
  hub-logo-layers.css
  hub-landing.css
  landing-pages.inc.php
)

asset_files=(
  assets/hub-landing.js
  assets/hub-i18n.js
  assets/hub-globe.js
  assets/analytics.config.js
  assets/i18n/hub-en.json
  assets/i18n/hub-ru.json
)

sync_local() {
  local dest="$1"
  if [[ ! -d "$dest" ]]; then
    echo "DEST not found: $dest" >&2
    echo "Mount BE sshfs or pass the BE androidcast_project path as arg 1." >&2
    exit 1
  fi
  mkdir -p "$dest/assets/i18n" "$dest/partials"
  for f in "${hub_files[@]}"; do
    install -m 0644 "$SRC/$f" "$dest/$f"
    echo "  $f"
  done
  install -D -m 0644 "$SRC/partials/cookie_consent.php" "$dest/partials/cookie_consent.php"
  echo "  partials/cookie_consent.php"
  for f in "${asset_files[@]}"; do
    install -D -m 0644 "$SRC/$f" "$dest/$f"
    echo "  $f"
  done
  PLATFORM_SRC="$REPO/examples/platform"
  PLATFORM_DEST="$dest/android_cast/examples/platform"
  if [[ -d "$PLATFORM_SRC" ]]; then
    mkdir -p "$PLATFORM_DEST"
    rsync -a --no-group --no-owner "$PLATFORM_SRC/" "$PLATFORM_DEST/"
    echo "  platform/ (footer)"
  fi
}

verify_hint() {
  cat <<'EOF'

Verify on BE or public URL:
  curl -sS 'https://apps.f0xx.org/app/androidcast_project/' | grep -E 'Landing|hub-landing|landing-top'
  curl -sS -o /dev/null -w '%{http_code}\n' 'https://apps.f0xx.org/app/androidcast_project/hub-landing.css'

Expect: title "Android Cast — Landing", HTTP 200 for hub-landing.css.
Then hard-refresh the browser (Ctrl+Shift+R).
EOF
}

echo "Sync landing hub → $DEST"
if [[ -n "$REMOTE" ]]; then
  tmp="$(mktemp -d)"
  trap 'rm -rf "$tmp"' EXIT
  for f in "${hub_files[@]}"; do cp "$SRC/$f" "$tmp/"; done
  mkdir -p "$tmp/assets/i18n"
  for f in "${asset_files[@]}"; do
    mkdir -p "$tmp/$(dirname "$f")"
    cp "$SRC/$f" "$tmp/$f"
  done
  scp "$tmp/index.php" "$tmp/index.html" "$tmp/hub.css" "$tmp/hub-logo-layers.css" \
      "$tmp/hub-landing.css" "$tmp/landing-pages.inc.php" \
      "$REMOTE:/var/www/localhost/htdocs/apps/app/androidcast_project/"
  scp "$SRC/partials/cookie_consent.php" \
      "$REMOTE:/var/www/localhost/htdocs/apps/app/androidcast_project/partials/"
  scp -r "$tmp/assets/hub-landing.js" "$tmp/assets/hub-i18n.js" "$tmp/assets/hub-globe.js" \
      "$tmp/assets/analytics.config.js" "$tmp/assets/i18n" \
      "$REMOTE:/var/www/localhost/htdocs/apps/app/androidcast_project/assets/"
  echo "Remote sync via $REMOTE complete."
else
  sync_local "$DEST"
  echo "Local sync complete."
fi

verify_hint
