#!/usr/bin/env bash
# Run on Gentoo FE and Alpine BE to diagnose build proxy (paste output when asking for help).
set -u
echo "=== $(hostname) build proxy diagnostic ==="
echo

if [[ -f /etc/nginx/nginx.conf ]]; then
  echo "--- FE: androidcast locations in nginx.conf ---"
  grep -n 'androidcast_project' /etc/nginx/nginx.conf | head -20
  echo
fi
if [[ -f /etc/nginx/apps.conf ]]; then
  echo "--- FE: apps.conf ---"
  cat /etc/nginx/apps.conf
  echo
fi
if [[ -f /etc/nginx/conf.d/apps_builder.frag ]]; then
  echo "--- BE: apps_builder.frag ---"
  cat /etc/nginx/conf.d/apps_builder.frag
  echo
fi
if [[ -f /etc/nginx/conf.d/apps.conf ]]; then
  echo "--- BE: apps.conf listen 80 (first 25 lines) ---"
  sed -n '1,25p' /etc/nginx/conf.d/apps.conf
  echo
fi

echo "--- DNS artc0 ---"
getent hosts artc0.intra.raptor.org 2>/dev/null || true

echo "--- Local curls ---"
for url in \
  "http://127.0.0.1/app/androidcast_project/build/" \
  "http://artc0.intra.raptor.org/app/androidcast_project/build/" \
  "https://artc0.intra.raptor.org/app/androidcast_project/build/"; do
  code=$(curl -sS -o /dev/null -w "%{http_code}" -H "Host: apps.f0xx.org" -k "$url" 2>/dev/null || echo "fail")
  echo "$code  $url"
done

echo "--- PHP index exists? ---"
ls -la /var/www/localhost/htdocs/apps/app/androidcast_project/android_cast/examples/build_console/backend/public/index.php 2>&1 || true
