-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanll-launcher.sh
More file actions
executable file
·728 lines (665 loc) · 27.5 KB
/
Copy pathpanll-launcher.sh
File metadata and controls
executable file
·728 lines (665 loc) · 27.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
#
# @launcher-deed begin
# ;; SPDX-License-Identifier: MPL-2.0
# (praxis-deed
# :schema-version "1.0.0"
# :canonical-name "panll-launcher"
# :beholding-chora #u5"estate/chora"
# (artefact :type "launcher" :version "0.2.0"
# :generator "launch-scaffolder")
# (app :name "panll" :display "PanLL"
# :url "http://localhost:8000/public/" :runtime-kind "server-url")
# (compliance :standard-version "0.4.0"
# :standards ("launcher-standard.adoc"
# "LM-LA-LIFECYCLE-STANDARD.adoc"
# "cross-platform-system-integration-modes"))
# (modes :accepted ("--start" "--stop" "--status" "--browser" "--web" "--auto" "--integ" "--disinteg" "--help" "--version"))
# (platforms :supported ("linux" "macos" "windows"))
# (lifecycle-phases :covered ("start" "stop" "status" "integ" "disinteg")
# :deferred ("install" "uninstall" "update" "backup" "restore" "migrate")))
# @launcher-deed end
#
# ============================================================================
# panll-launcher.sh — PanLL
# ============================================================================
# GENERATED by launch-scaffolder from panll.launcher.a2ml.
# Do NOT edit this file directly — your edits will be overwritten on the next
# `launch-scaffolder realign`. Put per-app tweaks in the [exceptions] block of
# the .launcher.a2ml config instead.
# ============================================================================
set -euo pipefail
# ----------------------------------------------------------------------------
# CONFIGURATION
# ----------------------------------------------------------------------------
APP_NAME='panll'
APP_DISPLAY='PanLL'
APP_DESC='PanLL — panels framework (TEA + workspace layer + Stapeln integration)'
APP_CATEGORIES='Development;Utility;'
APP_GENERIC_NAME='PanLL'
APP_VERSION='0.2.0'
BUILD_SHA_SHORT='unknown'
RUNTIME_KIND='server-url'
REPO_DIR='/var/mnt/eclipse/repos/panll'
ICON_SOURCE='/var/mnt/eclipse/repos/panll/assets/icon-256.png'
# Absolute path back to the per-app `<app>.launcher.a2ml` config that
# produced this script. Consumed by the --integ / --disinteg arms when
# the `launch-scaffolder` binary is on $PATH, so they can delegate to
# the Rust implementation instead of running the shell fallback.
CONFIG_FILE='/var/mnt/eclipse/repos/panll/panll.launcher.a2ml'
URL='http://localhost:8000/public/'
WAIT_SECONDS="15"
PID_FILE="${XDG_RUNTIME_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}}/launch-scaffolder/panll/server.pid"
LOG_FILE="${XDG_STATE_HOME:-$HOME/.local/state}/launch-scaffolder/panll/server.log"
# Both defaults live in per-app directories under per-user XDG state. Create
# the default leaves as 0700 before
# the first write: a predictable path inside a world-writable directory (the
# old /tmp default) let any local user pre-create or symlink the pid file and
# steer what this script later killed or removed (Hypatia 82/83, #48).
# Default locations use unique per-app directories before chmod. Explicit
# paths are never chmodded: a path such as /tmp must never have its parent
# permissions changed. Every resolved parent is checked before PID/log I/O.
ensure_state_dirs() {
local pid_dir log_dir
pid_dir="$(dirname "$PID_FILE")"
log_dir="$(dirname "$LOG_FILE")"
mkdir -p "$pid_dir" "$log_dir"
chmod 0700 "$pid_dir"
chmod 0700 "$log_dir"
check_private_state_dir "$pid_dir" || return 1
check_private_state_dir "$log_dir" || return 1
}
# Refuse shared or group-writable state locations. This also prevents a
# custom /tmp path from causing chmod on /tmp or exposing a predictable PID
# file to other local users.
check_private_state_dir() {
local dir="$1" mode digits numeric
[[ -O "$dir" ]] || { err "State directory is not owned by this user: $dir"; return 1; }
mode="$(stat -c '%a' "$dir" 2>/dev/null || stat -f '%Lp' "$dir" 2>/dev/null)" || {
err "Cannot inspect state-directory permissions: $dir"; return 1;
}
digits="${mode: -3}"
[[ "$digits" =~ ^[0-7]{3}$ ]] || { err "Cannot inspect state-directory permissions: $dir"; return 1; }
numeric=$((8#$digits))
if (( numeric & 022 )); then
err "State directory is group/world-writable; choose a private location: $dir"
return 1
fi
}
# Explicit argv from [runtime].command
START_COMMAND=('just' 'serve')
MODE="${1:---auto}"
FORCE="false"
[[ "${2:-}" == "--force" ]] && FORCE="true"
# ----------------------------------------------------------------------------
# LOGGING HELPERS
# ----------------------------------------------------------------------------
log() { echo -e "\033[0;32m[$APP_DISPLAY]\033[0m $1"; }
warn() { echo -e "\033[0;33m[$APP_DISPLAY]\033[0m $1" >&2; }
err() { echo -e "\033[0;31m[$APP_DISPLAY]\033[0m ERROR: $1" >&2; }
is_gui_context() {
[ ! -t 2 ] && { [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; }
}
# Report title ($1) and body ($2) to stderr, and show a desktop alert when
# a GUI context and a supported notification tool are available.
gui_error() {
local title="$1"
local body="$2"
err "$title"
# ${body//…} rather than `echo "$body" | sed 's/^/ /'` (shellcheck SC2001):
# the same two-space indent on every line, including empty ones, with no
# subprocess per call.
printf ' %s\n' "${body//$'\n'/$'\n '}" >&2
if is_gui_context; then
if command -v kdialog >/dev/null 2>&1; then kdialog --title "$APP_DISPLAY: $title" --error "$body" 2>/dev/null &
elif command -v zenity >/dev/null 2>&1; then zenity --error --title="$APP_DISPLAY: $title" --text="$body" --width=500 2>/dev/null &
elif command -v notify-send >/dev/null 2>&1; then notify-send --urgency=critical --icon=dialog-error "$APP_DISPLAY: $title" "$body" 2>/dev/null &
elif command -v xmessage >/dev/null 2>&1; then xmessage -center "$APP_DISPLAY: $title\n\n$body" 2>/dev/null &
fi
fi
}
# ----------------------------------------------------------------------------
# PLATFORM DETECTION
# ----------------------------------------------------------------------------
detect_platform() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "macos" ;;
CYGWIN*|MINGW*|MSYS*|Windows_NT) echo "windows" ;;
*) echo "unknown" ;;
esac
}
PLATFORM="$(detect_platform)"
case "$PLATFORM" in
linux)
APPS_DIR="$HOME/.local/share/applications"
ICON_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"
DESKTOP_SHORTCUT_DIR="$HOME/Desktop"
BIN_DIR="$HOME/.local/bin"
DESKTOP_FILE_TARGET="$APPS_DIR/${APP_NAME}.desktop"
DESKTOP_SHORTCUT_TARGET="$DESKTOP_SHORTCUT_DIR/${APP_NAME}.desktop"
ICON_TARGET="$ICON_DIR/${APP_NAME}.png"
ICON_MARKER_TARGET="$ICON_TARGET.launch-scaffolder-managed"
LAUNCHER_TARGET="$BIN_DIR/${APP_NAME}-launcher"
;;
macos)
APPS_DIR="$HOME/Applications"
DESKTOP_SHORTCUT_DIR="$HOME/Desktop"
BIN_DIR="$HOME/.local/bin"
DESKTOP_FILE_TARGET="$APPS_DIR/${APP_DISPLAY}.app"
DESKTOP_SHORTCUT_TARGET="$DESKTOP_SHORTCUT_DIR/${APP_DISPLAY}.command"
ICON_TARGET="$APPS_DIR/${APP_DISPLAY}.app/Contents/Resources/icon.png"
ICON_MARKER_TARGET="$ICON_TARGET.launch-scaffolder-managed"
LAUNCHER_TARGET="$BIN_DIR/${APP_NAME}-launcher"
;;
windows)
APPDATA_DIR="${APPDATA:-$HOME/AppData/Roaming}"
START_MENU_DIR="$APPDATA_DIR/Microsoft/Windows/Start Menu/Programs"
DESKTOP_SHORTCUT_DIR="$HOME/Desktop"
BIN_DIR="$HOME/.local/bin"
DESKTOP_FILE_TARGET="$START_MENU_DIR/${APP_DISPLAY}.lnk"
DESKTOP_SHORTCUT_TARGET="$DESKTOP_SHORTCUT_DIR/${APP_DISPLAY}.lnk"
ICON_TARGET="$BIN_DIR/${APP_NAME}.ico"
ICON_MARKER_TARGET="$ICON_TARGET.launch-scaffolder-managed"
LAUNCHER_TARGET="$BIN_DIR/${APP_NAME}-launcher.sh"
;;
*)
APPS_DIR=""; DESKTOP_SHORTCUT_DIR=""; BIN_DIR="$HOME/.local/bin"
DESKTOP_FILE_TARGET=""; DESKTOP_SHORTCUT_TARGET=""; ICON_TARGET=""; ICON_MARKER_TARGET=""
LAUNCHER_TARGET="$BIN_DIR/${APP_NAME}-launcher"
;;
esac
# ----------------------------------------------------------------------------
# PROCESS MANAGEMENT
# ----------------------------------------------------------------------------
# Succeed if the PID directory is absent or passes the ownership and
# permission checks; reject an existing unsafe directory.
pid_directory_is_safe() {
local pid_dir
pid_dir="$(dirname "$PID_FILE")"
[ -d "$pid_dir" ] || return 0
check_private_state_dir "$pid_dir"
}
# Print the PID from PID_FILE without a newline; fail if unreadable or if
# the value is not one to ten decimal digits representing a PID of at least 2.
read_pid() {
local pid
IFS= read -r pid < "$PID_FILE" || return 1
if [[ ! "$pid" =~ ^[0-9]{1,10}$ ]] || (( 10#$pid < 2 )); then
err "Invalid PID value in $PID_FILE"
return 1
fi
printf '%s' "$pid"
}
# Succeed when PID_FILE is in a safe directory, contains a valid PID, and
# kill -0 confirms that the process exists and is accessible to this user.
is_running() {
[ -f "$PID_FILE" ] || return 1
pid_directory_is_safe || return 1
local pid
pid="$(read_pid)" || return 1
kill -0 "$pid" 2>/dev/null
}
# Remove an existing PID file when is_running fails, logging a warning.
# Call only after the state directory has been validated.
clear_stale_pid() {
if [ -f "$PID_FILE" ] && ! is_running; then
warn "Removing stale or invalid PID file"
rm -f "$PID_FILE"
fi
}
# Poll URL with curl once per second for up to $1 seconds (default: 15).
# Return success on a successful request, or failure when the wait expires.
wait_for_url() {
local max_wait=${1:-15}
local waited=0
while [ "$waited" -lt "$max_wait" ]; do
if command -v curl >/dev/null 2>&1 && curl -fsS "$URL" >/dev/null 2>&1; then
return 0
fi
sleep 1
waited=$((waited + 1))
done
return 1
}
# Prepare private state directories and start START_COMMAND with nohup,
# recording its PID and log. Reuse a running instance; fail on early exit
# or URL readiness timeout (a timeout leaves the process running).
start_server() {
ensure_state_dirs || return 1
clear_stale_pid
if is_running; then
log "Already running (PID $(read_pid))"
return 0
fi
if [ -z "${START_COMMAND:-}" ] || { [ ! "${START_COMMAND:0:1}" = "(" ] && [ ! -x "${START_COMMAND%% *}" ] && ! command -v "${START_COMMAND%% *}" >/dev/null 2>&1; }; then
if [ -z "${START_COMMAND:-}" ]; then
gui_error "No startup command found" \
"$APP_DISPLAY is installed, but I don't know how to start it. Check the
[runtime].startup-command-search or [runtime].command in panll.launcher.a2ml."
return 1
fi
fi
log "Starting $APP_DISPLAY..."
cd "$REPO_DIR" 2>/dev/null || true
nohup "${START_COMMAND[@]}" >"$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
sleep 0.2
local started_pid
started_pid="$(read_pid)" || { rm -f "$PID_FILE"; return 1; }
if ! kill -0 "$started_pid" 2>/dev/null; then
gui_error "Process exited immediately" "Check $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
if ! wait_for_url "$WAIT_SECONDS"; then
gui_error "Server did not start" \
"$APP_DISPLAY did not become reachable at $URL within $WAIT_SECONDS seconds.
Check $LOG_FILE — and try: curl -v $URL"
if command -v feedback-o-tron >/dev/null 2>&1; then
feedback-o-tron --event "launcher:start_failed" \
--app "$APP_NAME" --url "$URL" --log "$LOG_FILE" \
--error "Timeout after $WAIT_SECONDS seconds" 2>/dev/null || true
fi
return 1
fi
log "Server started (PID $(read_pid)) — $URL"
return 0
}
# Send SIGTERM to the validated, accessible PID and remove PID_FILE.
# Do nothing if no running instance is found; do not wait for termination.
stop_server() {
if ! is_running; then
log "No running instance found"
return 0
fi
log "Stopping $APP_DISPLAY..."
pid_directory_is_safe || return 1
local pid
pid="$(read_pid)" || return 1
kill "$pid" 2>/dev/null || true
rm -f "$PID_FILE"
log "Stopped"
}
open_browser() {
if [ -z "$URL" ]; then
log "No URL configured for $APP_DISPLAY"
return 1
fi
if ! is_running; then
err "Server is not running — use --start or --auto first"
return 1
fi
log "Opening $URL..."
case "$PLATFORM" in
linux)
if command -v xdg-open >/dev/null 2>&1; then xdg-open "$URL" &
elif command -v firefox >/dev/null 2>&1; then firefox "$URL" &
elif command -v chromium >/dev/null 2>&1; then chromium "$URL" &
else warn "No browser found — open manually: $URL"; fi ;;
macos) open "$URL" 2>/dev/null || warn "Open manually: $URL" ;;
windows)
if command -v start >/dev/null 2>&1; then start "$URL"
elif command -v cygstart >/dev/null 2>&1; then cygstart "$URL"
else warn "Open manually: $URL"; fi ;;
*) warn "Unknown platform — open manually: $URL" ;;
esac
}
# ----------------------------------------------------------------------------
# SYSTEM INTEGRATION — --integ / --disinteg
# ----------------------------------------------------------------------------
# Succeed if path $1 exists, including when it is a dangling symlink.
path_exists() {
[ -e "$1" ] || [ -L "$1" ]
}
# Succeed if any configured integration target or icon ownership marker
# exists, including a dangling symlink.
already_integrated() {
path_exists "$DESKTOP_FILE_TARGET" || path_exists "$DESKTOP_SHORTCUT_TARGET" || \
path_exists "$ICON_TARGET" || path_exists "$ICON_MARKER_TARGET" || path_exists "$LAUNCHER_TARGET"
}
# Succeed only if at least one integration marker is found and all existing
# integration targets satisfy the launch-scaffolder ownership marker checks.
is_managed_install() {
local found_marker="false" target
local marker_targets=("$LAUNCHER_TARGET" "$DESKTOP_FILE_TARGET" "$DESKTOP_SHORTCUT_TARGET")
for target in "${marker_targets[@]}"; do
if ! path_exists "$target"; then
continue
fi
if [ "$target" = "$LAUNCHER_TARGET" ]; then
if [ ! -f "$target" ] || ! grep -Eq '^# GENERATED by launch-scaffolder from .+' "$target" 2>/dev/null; then
return 1
fi
else
if [ ! -f "$target" ] || ! grep -Fxq '# X-Launch-Scaffolder=launch-scaffolder' "$target" 2>/dev/null; then
return 1
fi
fi
found_marker="true"
done
if path_exists "$ICON_TARGET"; then
if [ ! -f "$ICON_MARKER_TARGET" ] || ! grep -Fxq 'launch-scaffolder managed icon' "$ICON_MARKER_TARGET" 2>/dev/null; then
return 1
fi
found_marker="true"
fi
if path_exists "$ICON_MARKER_TARGET"; then
if [ ! -f "$ICON_MARKER_TARGET" ] || ! grep -Fxq 'launch-scaffolder managed icon' "$ICON_MARKER_TARGET" 2>/dev/null; then
return 1
fi
found_marker="true"
fi
[ "$found_marker" = "true" ]
}
# Write the managed-icon marker beside ICON_MARKER_TARGET, set mode 0644,
# and rename it into place; clean up the temporary file on failure.
atomic_write_icon_marker() {
local temp
if ! temp="$(mktemp "${ICON_MARKER_TARGET}.tmp.XXXXXX")"; then
err "cannot create temporary icon ownership marker"
return 1
fi
if ! printf '%s\n' 'launch-scaffolder managed icon' >"$temp" || \
! chmod 0644 "$temp" || ! mv -f "$temp" "$ICON_MARKER_TARGET"; then
rm -f "$temp"
err "cannot atomically write icon ownership marker"
return 1
fi
}
# Copy source $1 to a temporary file beside target $2, apply mode $3, and
# rename it into place. Clean up the temporary file and fail on errors.
atomic_copy() {
local source="$1" class="pl-s">"$2" mode="$3" temp
if ! temp="$(mktemp "${target}.tmp.XXXXXX")"; then
err "cannot create temporary file beside $target"
return 1
fi
if ! cp "$source" "$temp" || ! chmod "$mode" "$temp" || ! mv -f "$temp" "$target"; then
rm -f "$temp"
err "cannot atomically install $target"
return 1
fi
}
# Print $1 with backslashes, newlines, carriage returns, and tabs escaped
# for a desktop-entry string value, without adding a trailing newline.
desktop_escape() {
local value="$1"
value="${value//\\/\\\\}"
value="${value//$'\n'/\\n}"
value="${value//$'\r'/\\r}"
value="${value//$'\t'/\\t}"
printf '%s' "$value"
}
# Print $1 as a quoted desktop-entry Exec argument, escaping backslashes,
# quotes, backticks, dollar signs, and percent field codes; append a newline.
desktop_exec_arg() {
local value="$1"
value="${value//\\/\\\\\\\\}"
value="${value//\"/\\\"}"
value="${value//\`/\\\`}"
value="${value//\$/\\\$}"
value="${value//%/%%}"
printf '"%s"\n' "$value"
}
# Print the path of keepopen.sh, the standard GUI → TUI → shell fallback
# helper, and fail if it is missing or not executable. Integration calls it
# before writing anything, so a missing helper cannot leave a partial install.
require_keepopen() {
local keepopen="/var/mnt/eclipse/repos/.desktop-tools/keepopen.sh"
if [ ! -f "$keepopen" ] || [ ! -x "$keepopen" ]; then
err "Required desktop helper is missing or not executable: $keepopen"
return 1
fi
printf '%s\n' "$keepopen"
}
# Atomically write a managed desktop entry to $1 with mode 0644, using
# keepopen.sh for GUI/TUI fallback and the custom icon or a generic icon.
write_linux_desktop_file() {
local class="pl-s">"$1" temp
local icon_name
if [ -f "$ICON_TARGET" ]; then
icon_name="$APP_NAME"
else
icon_name="package-x-generic"
fi
# keepopen.sh implements the standard fallback ladder: GUI → TUI →
# bash-at-repo-root. See launcher-standard.adoc §Fallback Ladder.
local keepopen
keepopen="$(require_keepopen)" || return 1
local gui_cmd tui_cmd quoted_launcher quoted_log
printf -v quoted_launcher '%q' "$LAUNCHER_TARGET"
printf -v quoted_log '%q' "$LOG_FILE"
# server-url: GUI = start server + open browser + tail log (so terminal
# stays open); TUI = start-only + follow log; Shell = repo root.
gui_cmd="$quoted_launcher --auto && tail -f $quoted_log"
tui_cmd="$quoted_launcher --start && tail -f $quoted_log"
if ! temp="$(mktemp "${target}.tmp.XXXXXX")"; then
err "cannot create temporary desktop file beside $target"
return 1
fi
if ! cat > "$temp" <<EOF
[Desktop Entry]
# X-Launch-Scaffolder=launch-scaffolder
Type=Application
Version=1.0
Name=$(desktop_escape "$APP_DISPLAY")
GenericName=$(desktop_escape "$APP_GENERIC_NAME")
Comment=$(desktop_escape "$APP_DESC")
Exec=$keepopen $(desktop_exec_arg "$APP_DISPLAY") $(desktop_exec_arg "$REPO_DIR") $(desktop_exec_arg "$gui_cmd") $(desktop_exec_arg "$tui_cmd") $(desktop_exec_arg "$LOG_FILE")
Icon=$icon_name
Terminal=true
Categories=$APP_CATEGORIES
StartupNotify=true
StartupWMClass=$APP_NAME
Actions=stop;status;
[Desktop Action stop]
Name=Stop
Exec=$(desktop_exec_arg "$LAUNCHER_TARGET") --stop
[Desktop Action status]
Name=Status
Exec=$(desktop_exec_arg "$LAUNCHER_TARGET") --status
EOF
then
rm -f "$temp"
err "cannot write desktop file $target"
return 1
fi
if ! chmod 0644 "$temp" || ! mv -f "$temp" "$target"; then
rm -f "$temp"
err "cannot atomically install desktop file $target"
return 1
fi
}
# Install the launcher, optional icon and marker, and Linux menu/desktop
# entries in user directories; refresh desktop metadata when tools exist.
do_integ_linux() {
# Check the desktop helper before any target is written (no partial install).
require_keepopen >/dev/null || return 1
mkdir -p "$APPS_DIR" "$ICON_DIR" "$BIN_DIR" "$DESKTOP_SHORTCUT_DIR"
# Declared and assigned separately (shellcheck SC2155). `local x="$(cmd)"`
# takes its exit status from `local`, so a failed `cd` was swallowed and the
# next line copied this script to $LAUNCHER_TARGET from a path assembled out
# of nothing — the one finding of the three with real failure-masking.
local script_dir
local script_path
if ! script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; then
err "cannot resolve this script's own directory"
return 1
fi
script_path="$script_dir/$(basename "${BASH_SOURCE[0]}")"
atomic_copy "$script_path" "$LAUNCHER_TARGET" 0755 || return 1
log " + launcher: $LAUNCHER_TARGET"
if [ -n "$ICON_SOURCE" ] && [ -f "$ICON_SOURCE" ]; then
atomic_copy "$ICON_SOURCE" "$ICON_TARGET" 0644 || return 1
atomic_write_icon_marker || return 1
log " + icon: $ICON_TARGET"
else
log " · no custom icon — using system fallback (package-x-generic)"
fi
write_linux_desktop_file "$DESKTOP_FILE_TARGET" || return 1
log " + menu: $DESKTOP_FILE_TARGET"
write_linux_desktop_file "$DESKTOP_SHORTCUT_TARGET" || return 1
log " + desktop: $DESKTOP_SHORTCUT_TARGET"
command -v update-desktop-database >/dev/null 2>&1 && \
update-desktop-database "$APPS_DIR" 2>/dev/null || true
if command -v gio >/dev/null 2>&1; then
gio set "$DESKTOP_FILE_TARGET" "metadata::trusted" true 2>/dev/null || true
gio set "$DESKTOP_SHORTCUT_TARGET" "metadata::trusted" true 2>/dev/null || true
fi
if [ -x "/var/mnt/eclipse/repos/.desktop-tools/verify-desktop-integrity.sh" ]; then
# if/else, not `cmd && log || log`: in that form a FAILING log on the
# success branch also fires the failure branch, so a run that worked
# reports both "generated" and "generation failed". The command's own
# status is what should choose the message.
if /var/mnt/eclipse/repos/.desktop-tools/verify-desktop-integrity.sh --generate 2>/dev/null; then
log " + integrity hashes generated"
else
log " · integrity hash generation failed (non-fatal)"
fi
fi
}
# Delegate installation to launch-scaffolder when its source config exists;
# otherwise use the Linux installer, refusing unmarked targets and prompting
# before reinstalling a managed installation unless FORCE is true.
do_integ() {
# Fast path: delegate to `launch-scaffolder provision` when it's on
# $PATH and the source config is still where it was at mint time.
# Keeps one authoritative implementation instead of drifting between
# this template and the Rust binary. Falls through to the shell
# implementation if either condition fails.
if command -v launch-scaffolder >/dev/null 2>&1 && [ -n "$CONFIG_FILE" ] && [ -f "$CONFIG_FILE" ]; then
local forward=(launch-scaffolder provision --integ "$CONFIG_FILE" --no-confirm)
[ "$FORCE" = "true" ] && forward+=(--force)
log "Delegating to launch-scaffolder provision..."
exec "${forward[@]}"
fi
if already_integrated; then
if ! is_managed_install; then
err "refusing to overwrite unmarked integration files for $APP_NAME"
return 1
fi
if [ "$FORCE" != "true" ]; then
warn "$APP_DISPLAY is already integrated."
read -rp "Reinstall? [y/N] " confirm
[[ ! "$confirm" =~ ^[Yy]$ ]] && { log "Nothing changed."; return 0; }
fi
fi
log "Integrating $APP_DISPLAY with the $PLATFORM desktop..."
case "$PLATFORM" in
linux) do_integ_linux || return 1 ;;
*) err "Only Linux integration is implemented in the generated template so far."; return 1 ;;
esac
log "✓ $APP_DISPLAY is now in your menu and on your Desktop."
}
# Validate state directories and stop a running server, then delegate removal
# to launch-scaffolder when available. Otherwise refuse unmarked targets,
# remove managed integration files and the PID file, and refresh the menu.
do_disinteg() {
ensure_state_dirs || return 1
# Fast path: delegate to `launch-scaffolder provision --disinteg`
# when available. Stop any running process first so the binary
# doesn't have to re-implement the process-management arm.
if is_running; then
log " • stopping running server"
stop_server
fi
if command -v launch-scaffolder >/dev/null 2>&1 && [ -n "$CONFIG_FILE" ] && [ -f "$CONFIG_FILE" ]; then
log "Delegating to launch-scaffolder provision..."
exec launch-scaffolder provision --disinteg "$CONFIG_FILE" --no-confirm
fi
if already_integrated && ! is_managed_install; then
err "refusing to remove unmarked integration files for $APP_NAME"
return 1
fi
log "Removing $APP_DISPLAY system integration..."
local removed_anything="false"
local targets=(
"$DESKTOP_FILE_TARGET"
"$DESKTOP_SHORTCUT_TARGET"
"$ICON_TARGET"
"$ICON_MARKER_TARGET"
"$LAUNCHER_TARGET"
)
for t in "${targets[@]}"; do
[ -z "$t" ] && continue
if [ -e "$t" ] || [ -L "$t" ]; then
rm -f "$t"
log " - removed $t"
removed_anything="true"
fi
done
[ "$PLATFORM" = "linux" ] && command -v update-desktop-database >/dev/null 2>&1 && \
update-desktop-database "$APPS_DIR" 2>/dev/null || true
rm -f "$PID_FILE"
if [ "$removed_anything" = "true" ]; then
log "✓ $APP_DISPLAY removed from your system."
else
log "Nothing to remove — $APP_DISPLAY was not integrated here."
fi
}
# Print usage, supported modes, and the detected platform/runtime to stdout.
show_help() {
cat <<EOF
$APP_DISPLAY launcher — $APP_DESC
Usage: $0 [MODE] [--force]
Runtime modes:
--auto Start server and open the browser (default)
--start Start the server only (no browser)
--stop Stop the running server
--status Show running status and URL
--browser Alias for --auto
System integration:
--integ Install desktop entry + shortcut + icon (idempotent; --force to reinstall)
--disinteg Remove everything --integ installed (idempotent)
Misc:
--help This text
--version Print the machine-readable launcher version
--browser Alias for --auto
--web Alias for --auto
Detected platform: $PLATFORM
Runtime kind: $RUNTIME_KIND
Repo: $REPO_DIR
EOF
}
# ----------------------------------------------------------------------------
# MAIN SWITCH
# ----------------------------------------------------------------------------
# Print PLATFORM and the uname machine architecture joined by a hyphen,
# without a trailing newline, for the version display.
platform_id() {
local arch
arch="$(uname -m)"
printf '%s-%s' "$PLATFORM" "$arch"
}
case "$MODE" in
--start) start_server ;;
--stop) stop_server ;;
--status)
if is_running; then
log "Running (PID $(cat "$PID_FILE"))${URL:+ — $URL}"
else
log "Not running${URL:+ — $URL}"
fi
;;
--browser|--web)
start_server && open_browser
;;
--version)
printf '%s %s (%s) [%s]\n' "$APP_NAME" "$APP_VERSION" "$BUILD_SHA_SHORT" "$(platform_id)"
;;
--auto)
start_server && open_browser
;;
--integ) do_integ ;;
--disinteg) do_disinteg ;;
--help|-h) show_help ;;
*)
err "Unknown mode: $MODE"
show_help
exit 2
;;
esac