#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SELF_PATH="$(readlink -f "${BASH_SOURCE[0]}")" if [[ -d "$SCRIPT_DIR/polyguard-rl" ]]; then APP_DIR="$SCRIPT_DIR/polyguard-rl" else APP_DIR="$SCRIPT_DIR" fi run_service() { local service="${1:-}" local title script case "$service" in env) title="PolyGuard Backend Env" script="scripts/run_env_local.sh" ;; api) title="PolyGuard Backend API" script="scripts/run_api_local.sh" ;; ui|frontend) title="PolyGuard Frontend" script="scripts/run_ui_local.sh" ;; *) echo "Unknown service: ${service:-}" echo "Expected one of: env, api, ui" exit 1 ;; esac cd "$APP_DIR" echo "[$title] Starting from $APP_DIR" echo "[$title] Running: bash $script" echo set +e bash "$script" local status=$? set -e echo echo "[$title] exited with status $status." if [[ -t 0 ]]; then read -r -p "Press Enter to close this terminal..." _ fi exit "$status" } launch_terminal() { local service="$1" local title="$2" if command -v gnome-terminal >/dev/null 2>&1; then gnome-terminal --title="$title" -- bash "$SELF_PATH" --run-service "$service" & elif command -v kgx >/dev/null 2>&1; then kgx --title "$title" -- bash "$SELF_PATH" --run-service "$service" & elif command -v konsole >/dev/null 2>&1; then konsole --workdir "$APP_DIR" -p tabtitle="$title" -e bash "$SELF_PATH" --run-service "$service" & elif command -v xfce4-terminal >/dev/null 2>&1; then xfce4-terminal --title="$title" --working-directory="$APP_DIR" --execute bash "$SELF_PATH" --run-service "$service" & elif command -v mate-terminal >/dev/null 2>&1; then mate-terminal --title="$title" --working-directory="$APP_DIR" -- bash "$SELF_PATH" --run-service "$service" & elif command -v xterm >/dev/null 2>&1; then xterm -T "$title" -e bash "$SELF_PATH" --run-service "$service" & else echo "No supported terminal emulator found." echo echo "Open three terminals manually and run:" echo " cd \"$APP_DIR\" && bash scripts/run_env_local.sh" echo " cd \"$APP_DIR\" && bash scripts/run_api_local.sh" echo " cd \"$APP_DIR\" && bash scripts/run_ui_local.sh" exit 1 fi } usage() { cat <