#!/bin/bash
#
# Run this ON THE PRODUCTION SERVER after you SSH in:
#
#   ssh -p 2222 -i /path/to/your/private_key respon45@199.250.194.180
#
# Then either:
#   (1) If you copied this script to the server:
#       cd /home/respon45/responsabhealth.com && chmod +x scripts/debug-production-500.sh && ./scripts/debug-production-500.sh
#   (2) Or run with explicit paths (docroot is often the "web" subdir):
#       ./scripts/debug-production-500.sh /home/respon45/responsabhealth.com /home/respon45/responsabhealth.com/web
#
# If your Apache/Nginx docroot points at the repo root (no web/), use:
#       ./scripts/debug-production-500.sh /home/respon45/responsabhealth.com /home/respon45/responsabhealth.com
#
SITE_ROOT="${1:-/home/respon45/responsabhealth.com}"
DOCROOT="${2:-$SITE_ROOT/web}"
# If web doesn't exist, assume site root IS docroot
[ ! -d "$DOCROOT" ] && [ -d "$SITE_ROOT" ] && DOCROOT="$SITE_ROOT"
echo "=== Drupal 500 error diagnostic ==="
echo "Site root: $SITE_ROOT"
echo "Docroot:   $DOCROOT"
echo ""

echo "--- 1. Directory structure ---"
ls -la "$SITE_ROOT" 2>&1
echo ""
ls -la "$DOCROOT" 2>&1 | head -25
echo ""

echo "--- 2. .htaccess in docroot ---"
if [ -f "$DOCROOT/.htaccess" ]; then
  head -80 "$DOCROOT/.htaccess"
else
  echo "MISSING: $DOCROOT/.htaccess"
fi
echo ""

echo "--- 3. index.php ---"
if [ -f "$DOCROOT/index.php" ]; then
  head -20 "$DOCROOT/index.php"
else
  echo "MISSING: $DOCROOT/index.php"
fi
echo ""

echo "--- 4. settings.php and permissions ---"
SETTINGS="$DOCROOT/sites/default/settings.php"
if [ -f "$SETTINGS" ]; then
  ls -la "$SETTINGS"
  grep -E "^\s*\\\$databases|config_sync_directory|trusted_host|error_level" "$SETTINGS" 2>/dev/null | head -20
else
  echo "MISSING: $SETTINGS"
fi
echo ""

echo "--- 5. Drupal log (recent errors) ---"
LOG="$DOCROOT/sites/default/files/log/drupal.log"
if [ -f "$LOG" ]; then
  tail -100 "$LOG"
else
  echo "No log at $LOG (or path differs)"
  find "$SITE_ROOT" -name "drupal*.log" -type f 2>/dev/null
fi
echo ""

echo "--- 6. PHP error log (if readable) ---"
for path in /var/log/php*.log /var/log/apache2/error.log /var/log/httpd/error_log "$SITE_ROOT/logs/error.log"; do
  if [ -r "$path" ]; then
    echo ">>> $path (last 30 lines)"
    tail -30 "$path"
    echo ""
  fi
done
echo ""

echo "--- 7. Web server error log (last 30 lines) ---"
for path in /var/log/apache2/error.log /var/log/httpd/error_log /var/log/nginx/error.log; do
  if [ -r "$path" ]; then
    echo ">>> $path"
    tail -30 "$path"
    echo ""
  fi
done
echo ""

echo "--- 8. Permissions on sites/default ---"
ls -la "$DOCROOT/sites/default/" 2>&1
echo ""

echo "--- 9. PHP version and CLI test ---"
php -v 2>&1
if [ -f "$DOCROOT/core/lib/Drupal.php" ]; then
  echo "Testing PHP bootstrap from docroot:"
  cd "$DOCROOT" && php -r "
    \$_SERVER['REQUEST_URI'] = '/';
    \$_SERVER['REQUEST_METHOD'] = 'GET';
    \$_SERVER['SCRIPT_NAME'] = '/index.php';
    try {
      require 'autoload.php';
      echo 'Autoload OK\n';
    } catch (Throwable \$e) {
      echo 'Autoload error: ' . \$e->getMessage() . '\n';
    }
  " 2>&1
fi
echo ""

echo "--- 10. Config sync directory ---"
SYNC=$(grep -E "config_sync_directory" "$SETTINGS" 2>/dev/null | head -1)
echo "From settings: $SYNC"
if [ -d "$SITE_ROOT/config/sync" ]; then
  ls "$SITE_ROOT/config/sync" | head -10
else
  echo "Directory $SITE_ROOT/config/sync not found or not sync dir"
fi
echo ""

echo "=== End diagnostic ==="
