#!/usr/bin/env bash set -euo pipefail OPENSHELL_LOCAL_DIR="${OPENSHELL_LOCAL_DIR:-$HOME/.openshell-local}" OPENSHELL_GATEWAY_PORT="${OPENSHELL_GATEWAY_PORT:-17670}" OPENSHELL_GATEWAY_ENDPOINT="http://127.0.0.1:${OPENSHELL_GATEWAY_PORT}" JWT_DIR="${OPENSHELL_LOCAL_DIR}/jwt" GATEWAY_CONFIG="${OPENSHELL_LOCAL_DIR}/gateway.toml" GATEWAY_LOG="${OPENSHELL_LOCAL_DIR}/gateway.log" mkdir -p "${JWT_DIR}" if [[ ! -f "${JWT_DIR}/signing.pem" ]]; then openssl genpkey -algorithm ed25519 -out "${JWT_DIR}/signing.pem" fi if [[ ! -f "${JWT_DIR}/public.pem" ]]; then openssl pkey -in "${JWT_DIR}/signing.pem" -pubout -out "${JWT_DIR}/public.pem" fi printf 'local-dev\n' > "${JWT_DIR}/kid" cat > "${GATEWAY_CONFIG}" </dev/null; then echo "OpenShell gateway already appears to be running on port ${OPENSHELL_GATEWAY_PORT}." else : > "${GATEWAY_LOG}" nohup openshell-gateway --config "${GATEWAY_CONFIG}" \ --disable-tls --drivers docker --port "${OPENSHELL_GATEWAY_PORT}" \ > "${GATEWAY_LOG}" 2>&1 & fi echo "Waiting for OpenShell gateway to listen on ${OPENSHELL_GATEWAY_ENDPOINT}..." for _ in {1..60}; do if grep -q 'Server listening' "${GATEWAY_LOG}" 2>/dev/null; then break fi sleep 1 done if ! grep -q 'Server listening' "${GATEWAY_LOG}" 2>/dev/null; then echo "OpenShell gateway did not become ready. Last log lines:" >&2 tail -50 "${GATEWAY_LOG}" >&2 || true exit 1 fi openshell gateway add "${OPENSHELL_GATEWAY_ENDPOINT}" --local || true openshell status echo "Gateway log: ${GATEWAY_LOG}"