#!/bin/sh
# =============================================================================
# frontend/entrypoint.sh
# =============================================================================
# Docker container entrypoint for TFM aInventory frontend.
# Fixes permissions for the logs volume and starts the Node.js server.
# =============================================================================

set -e

# Fix permissions for the logs directory (useful if mounted as a volume)
if [ -d "/app/logs" ]; then
    echo "🐳 [Docker] Fixing /app/logs permissions..."
    chown -R nextjs:nodejs /app/logs
fi

# Generate network.json for frontend runtime discovery
echo "🐳 [Docker] Generating public/network.json..."
cat <<EOF > /app/public/network.json
{
  "SERVER_IP": "${SERVER_IP:-localhost}",
  "BACKEND_PORT": ${BACKEND_PORT:-8000},
  "BACKEND_SSL_PORT": ${BACKEND_SSL_PORT:-8908}
}
EOF
chown nextjs:nodejs /app/public/network.json

# Hand off to the application server as the nextjs user
echo "🐳 [Docker] Starting Next.js standalone server as nextjs user..."
exec su-exec nextjs node server.js
