-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.sh
More file actions
executable file
·358 lines (320 loc) · 13.5 KB
/
play.sh
File metadata and controls
executable file
·358 lines (320 loc) · 13.5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
#
# play.sh — Self-healing, fault-tolerant, reflective launch script
# for Game Server Admin (GSA)
#
# This script is homoiconic: it reads and reasons about its own source.
# It can introspect its own capabilities, self-repair broken state,
# and recover from partial failures at every stage.
#
# CAPABILITIES (parsed reflectively from this source):
# @cap:sync — Verify repo is synchronised with remote
# @cap:build — Ensure latest build is present and current
# @cap:run — Launch the game/application
# @cap:heal — Self-heal broken state (missing tools, dirty worktrees)
# @cap:reflect — Introspect own source for capabilities and metadata
# @cap:platform — Auto-detect OS, arch, and available toolchains
# @cap:fault — Fault-tolerant with retry and graceful degradation
set -euo pipefail
# ─── Metaiconic core: the script can read itself ─────────────────────────────
readonly SELF="$(readlink -f "${BASH_SOURCE[0]}")"
readonly SELF_DIR="$(dirname "$SELF")"
readonly SELF_NAME="$(basename "$SELF")"
readonly SELF_SHA="$(sha256sum "$SELF" 2>/dev/null | cut -d' ' -f1 || echo "unknown")"
# ─── Reflective capability registry (parsed from @cap: tags above) ───────────
declare -A CAPS
_reflect_caps() {
while IFS= read -r line; do
if [[ "$line" =~ @cap:([a-z]+) ]]; then
local cap="${BASH_REMATCH[1]}"
local desc="${line#*— }"
CAPS["$cap"]="$desc"
fi
done < "$SELF"
}
# ─── Platform detection ──────────────────────────────────────────────────────
declare PLAT_OS="" PLAT_ARCH="" PLAT_SHELL="" PLAT_DISPLAY=""
_detect_platform() {
case "$(uname -s)" in
Linux*) PLAT_OS="linux" ;;
Darwin*) PLAT_OS="macos" ;;
CYGWIN*|MINGW*|MSYS*) PLAT_OS="windows" ;;
FreeBSD*) PLAT_OS="freebsd" ;;
*) PLAT_OS="unknown" ;;
esac
case "$(uname -m)" in
x86_64|amd64) PLAT_ARCH="x86_64" ;;
aarch64|arm64) PLAT_ARCH="aarch64" ;;
armv7l) PLAT_ARCH="armv7" ;;
riscv64) PLAT_ARCH="riscv64" ;;
*) PLAT_ARCH="$(uname -m)" ;;
esac
PLAT_SHELL="$(basename "${SHELL:-/bin/sh}")"
if [[ -n "${WAYLAND_DISPLAY:-}" ]]; then
PLAT_DISPLAY="wayland"
elif [[ -n "${DISPLAY:-}" ]]; then
PLAT_DISPLAY="x11"
elif [[ "$PLAT_OS" == "macos" ]]; then
PLAT_DISPLAY="quartz"
else
PLAT_DISPLAY="headless"
fi
}
# ─── Colour + output ─────────────────────────────────────────────────────────
_supports_colour() {
[[ -t 1 ]] && [[ "${TERM:-dumb}" != "dumb" ]]
}
if _supports_colour; then
readonly C_GREEN='\033[1;32m' C_RED='\033[1;31m' C_YELLOW='\033[1;33m'
readonly C_CYAN='\033[1;36m' C_BOLD='\033[1m' C_DIM='\033[2m'
readonly C_RESET='\033[0m'
else
readonly C_GREEN='' C_RED='' C_YELLOW='' C_CYAN='' C_BOLD='' C_DIM='' C_RESET=''
fi
_info() { printf "${C_CYAN}[info]${C_RESET} %s\n" "$*"; }
_ok() { printf "${C_GREEN}[ ok]${C_RESET} %s\n" "$*"; }
_warn() { printf "${C_YELLOW}[warn]${C_RESET} %s\n" "$*" >&2; }
_fail() { printf "${C_RED}[FAIL]${C_RESET} %s\n" "$*" >&2; }
_step() { printf "\n${C_BOLD}═══ %s ═══${C_RESET}\n" "$*"; }
# ─── Fault tolerance: retry with backoff ─────────────────────────────────────
_retry() {
local max_attempts="${1}"; shift
local delay="${1}"; shift
local attempt=1
while true; do
if "$@"; then
return 0
fi
if (( attempt >= max_attempts )); then
_fail "Failed after ${max_attempts} attempts: $*"
return 1
fi
_warn "Attempt ${attempt}/${max_attempts} failed, retrying in ${delay}s..."
sleep "$delay"
(( attempt++ ))
(( delay *= 2 ))
done
}
# ─── Self-healing: check and fix prerequisites ──────────────────────────────
_heal_tool() {
local tool="$1" install_hint="$2"
if command -v "$tool" &>/dev/null; then
_ok "$tool found: $(command -v "$tool")"
return 0
fi
_warn "$tool not found — attempting self-heal..."
case "$PLAT_OS" in
linux)
if command -v rpm-ostree &>/dev/null; then
_info "Fedora Atomic detected — install with: rpm-ostree install $install_hint"
elif command -v dnf &>/dev/null; then
_info "Try: sudo dnf install $install_hint"
elif command -v apt-get &>/dev/null; then
_info "Try: sudo apt-get install $install_hint"
elif command -v pacman &>/dev/null; then
_info "Try: sudo pacman -S $install_hint"
fi
;;
macos)
if command -v brew &>/dev/null; then
_info "Try: brew install $install_hint"
fi
;;
esac
# Check asdf as fallback
if command -v asdf &>/dev/null; then
_info "asdf available — check: asdf plugin list all | grep $tool"
fi
_fail "$tool is required but could not be auto-installed"
return 1
}
# ─── Git sync check ─────────────────────────────────────────────────────────
_check_sync() {
_step "SYNC CHECK"
cd "$SELF_DIR"
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
_fail "Not a git repository: $SELF_DIR"
return 1
fi
local branch
branch="$(git symbolic-ref --short HEAD 2>/dev/null || echo "detached")"
_info "Branch: $branch"
# Fetch with fault tolerance (network may be flaky)
if _retry 3 2 git fetch --quiet origin 2>/dev/null; then
_ok "Remote fetched successfully"
else
_warn "Could not reach remote — continuing with local state"
return 0
fi
local local_sha remote_sha base_sha
local_sha="$(git rev-parse HEAD)"
remote_sha="$(git rev-parse "origin/${branch}" 2>/dev/null || echo "none")"
if [[ "$remote_sha" == "none" ]]; then
_warn "No remote tracking branch origin/${branch}"
return 0
fi
if [[ "$local_sha" == "$remote_sha" ]]; then
_ok "Local and remote are in sync ($branch @ ${local_sha:0:8})"
return 0
fi
base_sha="$(git merge-base "$local_sha" "$remote_sha" 2>/dev/null || echo "none")"
if [[ "$base_sha" == "$local_sha" ]]; then
_warn "Local is behind remote — pulling..."
if git pull --ff-only origin "$branch" 2>/dev/null; then
_ok "Fast-forward pull succeeded"
else
_warn "Could not fast-forward — manual merge may be needed"
return 1
fi
elif [[ "$base_sha" == "$remote_sha" ]]; then
_warn "Local is ahead of remote (unpushed commits)"
_info "Run 'git push' when ready"
else
_warn "Local and remote have diverged — manual resolution needed"
return 1
fi
}
# ─── Build check ─────────────────────────────────────────────────────────────
_check_build() {
_step "BUILD CHECK"
cd "$SELF_DIR"
# GSA builds with: just build (which runs zig build under the hood)
# The CLI binary lives at src/interface/ffi/zig-out/bin/gsa
local binary="src/interface/ffi/zig-out/bin/gsa"
# Check if build artifact exists and is newer than source
local needs_build=false
if [[ ! -f "$binary" ]]; then
_warn "Build artifact not found: $binary"
needs_build=true
else
# Check if any source file is newer than the binary
local newest_src
newest_src="$(find src/ -name '*.zig' -o -name '*.idr' -o -name '*.eph' 2>/dev/null \
| head -100 \
| xargs -I{} stat -c '%Y {}' {} 2>/dev/null \
| sort -rn | head -1 | cut -d' ' -f2 || echo "")"
if [[ -n "$newest_src" ]] && [[ "$newest_src" -nt "$binary" ]]; then
_warn "Source newer than build — rebuild needed"
needs_build=true
else
_ok "Build is up to date"
fi
fi
if [[ "$needs_build" == "true" ]]; then
_info "Building GSA..."
# Self-heal: ensure build tools are available
_heal_tool "just" "just" || return 1
_heal_tool "zig" "zig" || return 1
if _retry 2 3 just build; then
_ok "Build succeeded"
else
_fail "Build failed — check errors above"
return 1
fi
fi
}
# ─── Run ──────────────────────────────────────────────────────────────────────
_run_game() {
_step "LAUNCH"
cd "$SELF_DIR"
_info "Platform: ${PLAT_OS}/${PLAT_ARCH} (${PLAT_DISPLAY}, ${PLAT_SHELL})"
_info "Starting Game Server Admin..."
# GSA primary mode: status check + probe interface
if command -v just &>/dev/null; then
just run status
else
local binary="src/interface/ffi/zig-out/bin/gsa"
if [[ -x "$binary" ]]; then
"$binary" status
else
_fail "No way to run GSA — need 'just' or built binary"
return 1
fi
fi
}
# ─── Reflection: introspect own capabilities ─────────────────────────────────
_show_reflection() {
_step "SELF-REFLECTION"
_info "Script: $SELF_NAME"
_info "SHA256: ${SELF_SHA:0:16}..."
_info "Size: $(wc -c < "$SELF") bytes, $(wc -l < "$SELF") lines"
_info "Platform: ${PLAT_OS}/${PLAT_ARCH} (${PLAT_DISPLAY})"
echo ""
_info "Registered capabilities:"
for cap in "${!CAPS[@]}"; do
printf " ${C_GREEN}@cap:%-10s${C_RESET} %s\n" "$cap" "${CAPS[$cap]}"
done
}
# ─── The smiley ──────────────────────────────────────────────────────────────
_smiley() {
printf "${C_GREEN}"
cat << 'SMILEY'
██████████████████████████
████ ████
██ ██
██ ██
██ ██
██ ████████ ████████ ██
██ ████████ ████████ ██
██ ████████ ████████ ██
██ ████ ████ ██
██ ██
██ ██
██ ██ ██ ██
██ ██ ██ ██
██ ████████████████ ██
██ ████████ ██
██ ██
████ ████
██████████████████████████
Game Server Admin — Ready!
SMILEY
printf "${C_RESET}"
}
# ─── Main ─────────────────────────────────────────────────────────────────────
main() {
_detect_platform
_reflect_caps
_smiley
case "${1:-run}" in
reflect|--reflect|-r)
_show_reflection
;;
sync|--sync)
_check_sync
;;
build|--build)
_check_build
;;
run|--run|"")
_check_sync || _warn "Sync check had issues — continuing anyway"
_check_build || { _fail "Build failed — cannot run"; exit 1; }
_run_game
;;
heal|--heal)
_step "SELF-HEAL"
_heal_tool "git" "git"
_heal_tool "just" "just"
_heal_tool "zig" "zig"
_ok "Heal check complete"
;;
help|--help|-h)
_show_reflection
echo ""
_info "Usage: ./$SELF_NAME [command]"
_info " run Full pipeline: sync → build → run (default)"
_info " sync Check repo is synced with remote"
_info " build Ensure latest build is present"
_info " heal Check and fix prerequisites"
_info " reflect Show script introspection data"
_info " help This message"
;;
*)
_fail "Unknown command: $1 (try --help)"
exit 1
;;
esac
}
main "$@"