#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"

ENV_FILE="$ROOT_DIR/.env"
COMPOSE_FILE="$ROOT_DIR/docker/compose.yml"

if [ ! -f "$ENV_FILE" ]; then
  echo "Error: env file not found at $ENV_FILE" >&2
  exit 1
fi

if [ ! -f "$COMPOSE_FILE" ]; then
  echo "Error: compose file not found at $COMPOSE_FILE" >&2
  exit 1
fi

set -a
source "$ENV_FILE"
set +a

translated_args=()

for arg in "$@"; do
  case "$arg" in
    backend)
      : "${BACKEND_CONTAINER_NAME:?BACKEND_CONTAINER_NAME is not set in .env}"
      translated_args+=("$BACKEND_CONTAINER_NAME")
      ;;
    dashboard)
      : "${DASHBOARD_CONTAINER_NAME:?DASHBOARD_CONTAINER_NAME is not set in .env}"
      translated_args+=("$DASHBOARD_CONTAINER_NAME")
      ;;
    next)
      : "${NEXT_CONTAINER_NAME:?NEXT_CONTAINER_NAME is not set in .env}"
      translated_args+=("$NEXT_CONTAINER_NAME")
      ;;
    *)
      translated_args+=("$arg")
      ;;
  esac
done

exec sudo docker compose \
  --env-file "$ENV_FILE" \
  -f "$COMPOSE_FILE" \
  "${translated_args[@]}"
