#!/bin/sh set -eu repo_path="${1:-$(pwd)}" socket_host="${TOOLKIT_SOCKET_HOST:-0.0.0.0}" socket_port="${TOOLKIT_SOCKET_PORT:-5174}" socket_cors_origin="${TOOLKIT_SOCKET_CORS_ORIGIN:-http://127.0.0.1:5173,http://localhost:5173,http://bone-kingdom.tabitha.tealthrone}" pid_file="$repo_path/.toolkit-socket.pid" log_directory="$repo_path/logs" log_file="$log_directory/toolkit-socket.log" cd "$repo_path" if [ ! -f package.json ] || [ ! -f socket/server.mjs ]; then echo "[deploy:toolkit] $repo_path is not a Bone Kingdom repo checkout." exit 1 fi mkdir -p "$log_directory" \ "$repo_path/src/assets/images/uploads" \ "$repo_path/src/assets/music/uploads" if [ ! -d "$repo_path/node_modules/socket.io" ]; then echo "[deploy:toolkit] node_modules missing socket.io; installing workspace dependencies." npm install --no-audit --no-fund fi if [ -f "$pid_file" ]; then existing_pid="$(cat "$pid_file")" if [ -n "$existing_pid" ] && kill -0 "$existing_pid" 2>/dev/null; then echo "[deploy:toolkit] stopping existing socket process $existing_pid" kill -TERM "$existing_pid" sleep 1 fi fi echo "[deploy:toolkit] starting socket on $socket_host:$socket_port" TOOLKIT_SOCKET_HOST="$socket_host" \ TOOLKIT_SOCKET_PORT="$socket_port" \ TOOLKIT_SOCKET_CORS_ORIGIN="$socket_cors_origin" \ nohup node socket/server.mjs >"$log_file" 2>&1 & socket_pid="$!" printf '%s\n' "$socket_pid" > "$pid_file" sleep 1 if ! kill -0 "$socket_pid" 2>/dev/null; then echo "[deploy:toolkit] socket failed to start; recent log output follows." tail -n 80 "$log_file" || true exit 1 fi if command -v curl >/dev/null 2>&1; then curl -fsS "http://127.0.0.1:$socket_port/health" >/dev/null fi echo "[deploy:toolkit] socket running as $socket_pid"