#!/bin/sh
# bostrat node boot: first-boot volume layout, then hand off to the CLI.
# Everything under $HOME sits on the Fly volume (see node.Dockerfile).
set -e
mkdir -p "$HOME/repos"
# Boxes commit as they work; give git an identity on first boot only — the
# operator can overwrite it (it persists in $HOME/.gitconfig on the volume).
git config --global user.name >/dev/null 2>&1 || git config --global user.name "bostrat node"
git config --global user.email >/dev/null 2>&1 || git config --global user.email "node@bostrat.ai"
# The agent drives boxes on a host tmux server (the Pi's globo-tmux.service
# equivalent): keep one alive with a detached keeper session.
SOCKET="${BOSTRAT_TMUX_SOCKET:-/tmp/tmux-0/default}"
mkdir -p "$(dirname "$SOCKET")"
tmux -S "$SOCKET" has-session 2>/dev/null || tmux -S "$SOCKET" new-session -d -s keeper "sleep infinity"
# Global-install layout (the GHCR image / npm -g) has bostrat-node on PATH;
# staged repo-context builds (`bostrat-node fly` wizard) run the tree copy.
if command -v bostrat-node >/dev/null 2>&1; then
  exec bostrat-node run
else
  exec node /app/agent/bin/bostrat-node.js run
fi
