#!/bin/bash
# =============================================================================
# Lilith Platform - Unified Run Command
# =============================================================================
#
# Portable Docker-based development and production orchestration.
# This is a thin wrapper that invokes the TypeScript CLI.
#
# Usage:
#   ./run dev           Start dev cluster (.local domains, HMR enabled)
#   ./run prod          Start production cluster (real domains, SSL)
#   ./run <script>      Passthrough to pnpm
#
# See ./run --help for full documentation.
# =============================================================================

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

# Ensure bun is in PATH (not sourced from profile in non-interactive shells)
export BUN_INSTALL="${BUN_INSTALL:-$HOME/.bun}"
export PATH="$BUN_INSTALL/bin:$PATH"

# Invoke the TypeScript CLI using bun's tsx
bun run tsx tooling/run/cli/index.ts "$@"
exit_code=$?

# Exit code 130 = SIGINT (Ctrl+C) - this is normal for stopping services
if [ $exit_code -eq 130 ]; then
  exit 0
fi

exit $exit_code
