#!/bin/sh set -eu fail() { printf '%s\n' "PCB backend: $*" >&2 exit 1 } # Allow diagnostics such as `docker compose run --rm pcb-backend codex --version`. if [ "${1:-}" != python ] || [ "${2:-}" != -m ] || [ "${3:-}" != uvicorn ]; then exec "$@" fi mkdir -p "$PCB_SESSION_ROOT" "$PCB_CODEX_HOME" "$HOME" "$XDG_RUNTIME_DIR" chmod 700 "$XDG_RUNTIME_DIR" [ -w "$PCB_SESSION_ROOT" ] || fail 'Runtime volume is not writable by UID 10001.' if [ ! -e "$PCB_SESSION_ROOT/resources" ] && [ ! -L "$PCB_SESSION_ROOT/resources" ]; then ln -s /opt/pcb-resources "$PCB_SESSION_ROOT/resources" fi [ -r "$PCB_SESSION_ROOT/resources/references/_index.md" ] || fail 'Missing EDA API references.' [ -r "$PCB_FREEROUTING_JAR" ] || fail 'Missing Freerouting JAR.' command -v "$PCB_CODEX_BIN" >/dev/null 2>&1 || fail 'Codex CLI not found; check PCB_CODEX_BIN.' command -v "$PCB_EDA_BIN" >/dev/null 2>&1 || fail 'Mount the complete Linux EDA directory and check PCB_EDA_BIN.' command -v "$PCB_BLENDER_BIN" >/dev/null 2>&1 || fail 'Blender not found; check PCB_BLENDER_BIN.' # Validate credential presence without printing or writing its value. python -c 'from codex_pcb.rpc import process_config; process_config(False)' Xvfb "$DISPLAY" -screen 0 1920x1080x24 -nolisten tcp > /tmp/pcb-xvfb.log 2>&1 & xvfb_pid=$! app_pid= cleanup() { if [ -n "$app_pid" ]; then kill -TERM "$app_pid" 2>/dev/null || true wait "$app_pid" 2>/dev/null || true fi kill -TERM "$xvfb_pid" 2>/dev/null || true wait "$xvfb_pid" 2>/dev/null || true } trap cleanup EXIT trap 'exit 143' TERM trap 'exit 130' INT ready=0 for attempt in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$xvfb_pid" 2>/dev/null || fail 'Xvfb exited; inspect /tmp/pcb-xvfb.log.' if xdpyinfo -display "$DISPLAY" >/dev/null 2>&1; then ready=1 break fi sleep 1 done [ "$ready" = 1 ] || fail 'Virtual display did not become ready.' "$@" & app_pid=$! set +e wait "$app_pid" status=$? set -e app_pid= exit "$status"