#!/usr/bin/env bash # Start MoneyFlix lab services (CRM PHP + Node redirector) set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" ENV_FILE="${MONEYFLIX_LAB_ENV:-$ROOT/.env.lab}" PID_DIR="$ROOT/tools/lab/.pids" LOG_DIR="$ROOT/tools/lab/.logs" mkdir -p "$PID_DIR" "$LOG_DIR" if [[ ! -f "$ENV_FILE" ]]; then echo "Run bootstrap first: bash tools/lab/bootstrap.sh" >&2 exit 1 fi # shellcheck disable=SC1090 source "$ENV_FILE" stop_if_running() { local name=$1 local pidfile="$PID_DIR/$name.pid" if [[ -f "$pidfile" ]]; then local pid pid="$(cat "$pidfile")" if kill -0 "$pid" 2>/dev/null; then echo "Stopping existing $name (pid $pid)" kill "$pid" 2>/dev/null || true sleep 1 fi rm -f "$pidfile" fi } stop_if_running crm stop_if_running redirector echo "Starting CRM on :${CRM_PORT}..." php -S "127.0.0.1:${CRM_PORT}" -t "$ROOT/crm" "$ROOT/crm/index.php" \ > "$LOG_DIR/crm.log" 2>&1 & echo $! > "$PID_DIR/crm.pid" echo "Starting redirector on :${REDIRECTOR_PORT}..." (cd "$ROOT/nodejs-redirector" && node index.mjs) > "$LOG_DIR/redirector.log" 2>&1 & echo $! > "$PID_DIR/redirector.pid" sleep 3 if ! kill -0 "$(cat "$PID_DIR/crm.pid")" 2>/dev/null; then echo "CRM failed to start — see $LOG_DIR/crm.log" >&2 tail -30 "$LOG_DIR/crm.log" >&2 || true exit 1 fi if ! kill -0 "$(cat "$PID_DIR/redirector.pid")" 2>/dev/null; then echo "Redirector failed to start — see $LOG_DIR/redirector.log" >&2 tail -30 "$LOG_DIR/redirector.log" >&2 || true exit 1 fi echo "Lab stack running:" echo " CRM: http://127.0.0.1:${CRM_PORT}/system/status" echo " Redirector: http://127.0.0.1:${REDIRECTOR_PORT}/health" echo " Logs: $LOG_DIR/"