#!/bin/sh
set -e

# 目標檔案
ENV_FILE=/usr/share/nginx/html/env.js

# 確保目錄存在
mkdir -p /usr/share/nginx/html

# 寫入環境變數
cat <<EOF > "$ENV_FILE"
window.env = {
    VITE_API_BASEURL: "${VITE_API_BASEURL:-http://localhost:8080}",
    VITE_FILE_API_BASEURL: "${VITE_FILE_API_BASEURL:-http://localhost:8081}",
    VITE_MQTT_BASEURL: "${VITE_MQTT_BASEURL:-ws://localhost:1883}",
    VITE_APP_TITLE: "${VITE_APP_TITLE:-MyApp}"
};
EOF

echo "[Entrypoint] Generated $ENV_FILE:"
cat "$ENV_FILE"

# 執行傳入的 CMD，例如 "nginx -g 'daemon off;'"
exec "$@"
