#!/usr/bin/env bash set -euo pipefail LABEL="org.prole.knoe-db.kpf-dev" PLIST="$HOME/Library/LaunchAgents/$LABEL.plist" PROLE_HOME="$HOME/.prole" BIN_DIR="$PROLE_HOME/bin" RUN_DIR="$PROLE_HOME/run" PIDFILE="$RUN_DIR/kpf.pids" # Source environment if present if [ -f "$BIN_DIR/prole-env.sh" ]; then # shellcheck source=/dev/null . "$BIN_DIR/prole-env.sh" fi ensure_dirs() { mkdir -p "$BIN_DIR" "$RUN_DIR" } list_cmds() { if /usr/libexec/PlistBuddy -c "Print :ProleCommands" "$PLIST" >/dev/null 2>&1; then local i=0 while true; do if ! val=$(/usr/libexec/PlistBuddy -c "Print :ProleCommands:$i" "$PLIST" 2>/dev/null); then break fi echo "$val" i=$((i+1)) done fi } start() { ensure_dirs : > "$PIDFILE" # Iterate over commands and start each in background shell while IFS= read -r cmd; do [ -z "$cmd" ] && continue (sh -lc "$cmd") & echo $! >> "$PIDFILE" done < <(list_cmds) wait || true } stop() { if [ -f "$PIDFILE" ]; then while read -r pid; do [ -z "$pid" ] && continue kill "$pid" 2>/dev/null || true done < "$PIDFILE" rm -f "$PIDFILE" fi } status() { if [ ! -f "$PIDFILE" ]; then echo "not running" exit 3 fi local alive=0 total=0 while read -r pid; do [ -z "$pid" ] && continue total=$((total+1)) if kill -0 "$pid" 2>/dev/null; then alive=$((alive+1)); fi done < "$PIDFILE" echo "$alive/$total running" } case "${1:-}" in start) start ;; stop) stop ;; restart) stop; start ;; status) status ;; *) echo "Usage: $0 {start|stop|restart|status}" >&2; exit 2 ;; esac