#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" APP_SOURCE_DIR="${SCRIPT_DIR}/app" APP_DIR="${APP_SOURCE_DIR}/dist" PORT="8092" URL="http://127.0.0.1:${PORT}/" LOG_DIR="${HOME}/.cache/web-rtcp-5axis-sim" LOG_FILE="${LOG_DIR}/local-server.log" BUILD_LOG_FILE="${LOG_DIR}/local-build.log" mkdir -p "$LOG_DIR" if [[ ! -f "${APP_DIR}/index.html" ]]; then npm --prefix "$APP_SOURCE_DIR" run build >"$BUILD_LOG_FILE" 2>&1 || { echo "failed to build web app; see ${BUILD_LOG_FILE}" >&2 exit 1 } fi is_serving() { curl -fsS --max-time 1 "$URL" >/dev/null 2>&1 } if ! is_serving; then nohup python3 -m http.server "$PORT" --bind 127.0.0.1 --directory "$APP_DIR" >"$LOG_FILE" 2>&1 & for _ in $(seq 1 40); do if is_serving; then break fi sleep 0.1 done fi if ! is_serving; then echo "local web server failed to start at ${URL}; see ${LOG_FILE}" >&2 exit 1 fi if command -v google-chrome-stable >/dev/null 2>&1; then exec google-chrome-stable --new-window "$URL" elif command -v google-chrome >/dev/null 2>&1; then exec google-chrome --new-window "$URL" elif command -v chromium >/dev/null 2>&1; then exec chromium --new-window "$URL" elif command -v chromium-browser >/dev/null 2>&1; then exec chromium-browser --new-window "$URL" else exec xdg-open "$URL" fi