-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart
More file actions
executable file
·145 lines (130 loc) · 4.62 KB
/
Copy pathstart
File metadata and controls
executable file
·145 lines (130 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# Start the CampaignGenerator server in the background with persistent logs.
# Wraps ./startup — accepts the same arguments.
#
# Usage (from anywhere):
# ~/CampaignGenerator/start \
# --campaign-dir ~/campaigns/out-of-the-abyss \
# --session-dir summaries/20260404
#
# Multiple instances: each gets its own port. If the requested port (default
# 5000) is busy, the next free port is used automatically. PID files are
# per-port (server-<port>.pid) so instances don't collide.
#
# --session-dir may be relative to --campaign-dir or an absolute path.
# Logs go to <session-dir>/logs/ (or CampaignGenerator/logs/ if no session).
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# ── Parse --campaign-dir, --session-dir, --port from args ──────────────────
CAMPAIGN_DIR_RAW=""
SESSION_DIR_RAW=""
PORT=5000
PREV=""
for ARG in "$@"; do
case "$PREV" in
--campaign-dir) CAMPAIGN_DIR_RAW="$ARG" ;;
--session-dir) SESSION_DIR_RAW="$ARG" ;;
--port) PORT="$ARG" ;;
esac
PREV="$ARG"
done
# ── Find a free port ───────────────────────────────────────────────────────
ORIGINAL_PORT=$PORT
while ss -tln 2>/dev/null | grep -q ":${PORT} "; do
PORT=$((PORT + 1))
done
if [ "$PORT" -ne "$ORIGINAL_PORT" ]; then
echo "Port $ORIGINAL_PORT is in use — using port $PORT instead"
fi
PID_FILE="$SCRIPT_DIR/server-${PORT}.pid"
# ── Check for already-running server on this port ──────────────────────────
if [ -f "$PID_FILE" ]; then
OLD_PID="$(cat "$PID_FILE")"
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "Server already running on port $PORT (PID $OLD_PID). Run $(dirname "$0")/stop --port $PORT first."
exit 1
else
rm -f "$PID_FILE"
fi
fi
# Resolve campaign dir to absolute
if [ -n "$CAMPAIGN_DIR_RAW" ]; then
CAMPAIGN_DIR_ABS="$(cd "$CAMPAIGN_DIR_RAW" 2>/dev/null && pwd)"
else
CAMPAIGN_DIR_ABS=""
fi
# Resolve session dir: try absolute first, then relative to campaign dir
SESSION_DIR_ABS=""
if [ -n "$SESSION_DIR_RAW" ]; then
if [ -d "$SESSION_DIR_RAW" ]; then
SESSION_DIR_ABS="$(cd "$SESSION_DIR_RAW" && pwd)"
elif [ -n "$CAMPAIGN_DIR_ABS" ] && [ -d "$CAMPAIGN_DIR_ABS/$SESSION_DIR_RAW" ]; then
SESSION_DIR_ABS="$(cd "$CAMPAIGN_DIR_ABS/$SESSION_DIR_RAW" && pwd)"
else
echo "Error: session-dir '$SESSION_DIR_RAW' not found (tried absolute and relative to campaign-dir)."
exit 1
fi
fi
# ── Rebuild args with resolved absolute paths and port ────────────────────────
NEW_ARGS=()
PORT_IN_ARGS=false
PREV=""
for ARG in "$@"; do
case "$PREV" in
--campaign-dir)
NEW_ARGS+=("${CAMPAIGN_DIR_ABS:-$ARG}")
PREV="$ARG"
continue
;;
--session-dir)
NEW_ARGS+=("${SESSION_DIR_ABS:-$ARG}")
PREV="$ARG"
continue
;;
--port)
NEW_ARGS+=("$PORT")
PORT_IN_ARGS=true
PREV="$ARG"
continue
;;
esac
NEW_ARGS+=("$ARG")
PREV="$ARG"
done
if ! $PORT_IN_ARGS; then
NEW_ARGS+=("--port" "$PORT")
fi
# ── Determine log directory ──────────────────────────────────────────────────
if [ -n "$SESSION_DIR_ABS" ]; then
LOG_BASE="$SESSION_DIR_ABS/logs"
elif [ -n "$CAMPAIGN_DIR_ABS" ]; then
LOG_BASE="$CAMPAIGN_DIR_ABS/logs"
else
LOG_BASE="$SCRIPT_DIR/logs"
fi
mkdir -p "$LOG_BASE"
LOG_FILE="$LOG_BASE/server-$(date +%Y%m%d-%H%M%S).log"
# ── Launch from campaign dir so ui_config.yaml lands in the right place ──────
LAUNCH_DIR="${CAMPAIGN_DIR_ABS:-$SCRIPT_DIR}"
echo "Starting Campaign Generator..."
[ -n "$CAMPAIGN_DIR_ABS" ] && echo " Campaign: $CAMPAIGN_DIR_ABS"
[ -n "$SESSION_DIR_ABS" ] && echo " Session: $SESSION_DIR_ABS"
echo " Log: $LOG_FILE"
echo ""
cd "$LAUNCH_DIR"
nohup "$SCRIPT_DIR/startup" "${NEW_ARGS[@]}" >> "$LOG_FILE" 2>&1 &
SERVER_PID=$!
echo "$SERVER_PID" > "$PID_FILE"
# Give the server a moment to start (or fail)
sleep 2
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
rm -f "$PID_FILE"
echo "Server failed to start. Last log output:"
echo ""
tail -20 "$LOG_FILE"
exit 1
fi
echo "Server started (PID $SERVER_PID) on port $PORT"
echo "Open http://localhost:$PORT in your browser"
echo ""
echo " tail -f $LOG_FILE"
echo " $(dirname "$0")/stop --port $PORT"