#!/usr/bin/env bash
# Locates Apache + PHP-FPM error logs across distros, tails the recent tail,
# greps for the patterns most likely to be our bug.

set -u

cd "$(dirname "$0")/../.." || exit 1

echo "--- Searching for Apache/PHP error logs ---"
echo ""

# Common locations across distros
CANDIDATES=(
    /var/log/apache2/error.log
    /var/log/apache2/error_log
    /var/log/apache2/*error*.log
    /var/log/httpd/error_log
    /var/log/httpd/error.log
    /var/log/httpd/*error*.log
    /usr/local/apache/logs/error_log
    /var/log/php*-fpm.log
    /var/log/php-fpm/error.log
    /var/log/php-fpm/*.log
    /var/log/php/*.log
    /var/log/laravel.log
)

# Per-vhost ErrorLog directives
VHOST_LOGS=$(grep -rh "^[[:space:]]*ErrorLog" \
    /etc/apache2/sites-enabled/ \
    /etc/apache2/sites-available/ \
    /etc/httpd/conf.d/ \
    /etc/httpd/sites-enabled/ \
    2>/dev/null | awk '{print $2}' | tr -d '"' | sort -u)

if [[ -n "$VHOST_LOGS" ]]; then
    echo "Per-vhost ErrorLog paths from Apache config:"
    while IFS= read -r p; do
        echo "  $p"
        CANDIDATES+=("$p")
    done <<< "$VHOST_LOGS"
    echo ""
fi

# PHP error_log ini setting
PHP_ERROR_LOG=$(php -r 'echo ini_get("error_log") ?: "";')
if [[ -n "$PHP_ERROR_LOG" ]]; then
    echo "PHP ini error_log: $PHP_ERROR_LOG"
    CANDIDATES+=("$PHP_ERROR_LOG")
    echo ""
fi

# Tail the last 50 lines of every existing candidate, plus grep for relevant patterns
declare -A SEEN
for raw in "${CANDIDATES[@]}"; do
    # Expand globs
    for path in $raw; do
        [[ -z "$path" || "${SEEN[$path]:-}" == "1" ]] && continue
        SEEN["$path"]=1
        [[ -f "$path" && -r "$path" ]] || continue

        echo "=========================================="
        echo "  $path"
        echo "  size: $(wc -l < "$path" 2>/dev/null || echo ?) lines, "\
             "modified $(stat -c %y "$path" 2>/dev/null || stat -f %Sm "$path" 2>/dev/null || echo ?)"
        echo "=========================================="
        echo ""
        echo "--- last 40 lines ---"
        tail -40 "$path" 2>/dev/null
        echo ""
        echo "--- grep for failure signatures (last 500 lines) ---"
        tail -500 "$path" 2>/dev/null | grep -iE \
            'PHP Fatal|PHP Parse|Allowed memory|Maximum execution|Class .* not found|Call to undefined|onboarding|scribe-config|scribe|Stack trace|segfault|child pid' \
            | tail -40
        echo ""
    done
done

# Also Laravel's storage/logs/laravel.log — most relevant if framework boot completes
if [[ -f storage/logs/laravel.log ]]; then
    echo "=========================================="
    echo "  storage/logs/laravel.log (Laravel app log)"
    echo "  size: $(wc -l < storage/logs/laravel.log) lines, "\
         "modified $(stat -c %y storage/logs/laravel.log 2>/dev/null || stat -f %Sm storage/logs/laravel.log 2>/dev/null)"
    echo "=========================================="
    echo ""
    echo "--- last 40 lines ---"
    tail -40 storage/logs/laravel.log 2>/dev/null
    echo ""
    echo "--- grep for onboarding/inertia signatures ---"
    tail -1000 storage/logs/laravel.log 2>/dev/null | grep -iE \
        'onboarding|inertia|share|production\.ERROR|production\.CRITICAL|exception_class' \
        | tail -30
    echo ""
fi

echo "--- File-system-wide check: any *.log file modified in the last 5 minutes? ---"
sudo find /var/log /tmp /var/www -maxdepth 4 -type f -name '*.log' -mmin -5 2>/dev/null \
    | head -20 \
    || find /var/log /tmp /var/www -maxdepth 4 -type f -name '*.log' -mmin -5 2>/dev/null | head -20

echo ""
