#!/bin/bash
set -euo pipefail

# Make public writable so env.sh can write env-config.js
chmod -R a+rw ./public || true

# Run env.sh if present
if [ -x ./env.sh ]; then
  ./env.sh || true
fi

# Ensure node cache exists and is writable
mkdir -p /app/node_modules/.cache || true
chmod -R a+rw /app/node_modules || true

# Ensure HOST is set so CRA binds to 0.0.0.0
export HOST=${HOST:-0.0.0.0}

echo "🚀 Starting React development server..."
# Start the React development server in background
npm run dev &
DEV_PID=$!

echo "🌐 Starting nginx proxy..."
# Start nginx in foreground so container stays alive; nginx will proxy to the dev server
exec nginx -g 'daemon off;'