#!/bin/sh
# Frontend Container Startup Logger
# Logs container lifecycle events for debugging restarts

echo "=========================================="
echo "[CONTAINER-STARTUP] $(date '+%Y-%m-%d %H:%M:%S')"
echo "Container: igny8_frontend"
echo "Hostname: $(hostname)"
echo "PID: $$"
echo "=========================================="

# Log environment info
echo "[INFO] Node version: $(node --version 2>&1)"
echo "[INFO] NPM version: $(npm --version 2>&1)"
echo "[INFO] Vite backend URL: ${VITE_BACKEND_URL:-not set}"
echo "[INFO] Working directory: $(pwd)"

# Check if this is a restart
if [ -f /tmp/container_pid ]; then
    PREV_PID=$(cat /tmp/container_pid)
    echo "[WARNING] Previous container PID found: $PREV_PID"
    echo "[WARNING] This appears to be a RESTART event"
    echo "[WARNING] Check Docker logs for SIGTERM/SIGKILL signals"
else
    echo "[INFO] First startup (no previous PID file found)"
fi

# Save current PID
echo $$ > /tmp/container_pid

# Check for git directory changes (common restart trigger)
if [ -d "/app/.git" ]; then
    echo "[INFO] Git directory detected in /app/.git"
    echo "[WARNING] Git operations may trigger container restarts due to Vite file watching"
    echo "[INFO] Last git commit: $(cd /app && git log -1 --format='%h %s' 2>/dev/null || echo 'N/A')"
fi

echo "=========================================="
echo "[CONTAINER-STARTUP] Initialization complete"
echo "[CONTAINER-STARTUP] Starting Vite dev server..."
echo "=========================================="

# Execute the CMD passed to the script
exec "$@"
