#!/usr/bin/env bash # Update only the four runtime files of the 0.9.1-3 RetroBrowser module. set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" [[ $EUID == 0 ]] || { echo 'Run: sudo ./install-retrobrowser-tokenfix.sh' >&2; exit 1; } www=/usr/local/share/mars-vme/www worker=/usr/local/libexec/mars-browser files=(websetup/index.html websetup/app.js browser/config.mjs browser/server.mjs) targets=("$www/index.html" "$www/app.js" "$worker/config.mjs" "$worker/server.mjs") for i in "${!files[@]}"; do [[ -f ${files[i]} && -f ${targets[i]} && ! -L ${targets[i]} ]] || { echo "Missing or unexpected file: ${files[i]} / ${targets[i]}" >&2; exit 1; } done for f in websetup/app.js browser/config.mjs browser/server.mjs; do /usr/bin/node --check "$f"; done # Restrict this update to precisely the source baseline or already updated files. for i in "${!files[@]}"; do actual=$(sha256sum "${targets[i]}" | cut -d' ' -f1) desired=$(sha256sum "${files[i]}" | cut -d' ' -f1) original=$(awk -v f="${files[i]}" '$2==f {print $1}' tokenfix-baseline.sha256) [[ -n $original && ( $actual == "$original" || $actual == "$desired" ) ]] || { echo "Unexpected installed version: ${targets[i]}. Review the source patch instead of overwriting local modifications." >&2; exit 1; } done backup=$(mktemp -d /var/lib/mars-vme-tokenfix.XXXXXXXX) chmod 0700 "$backup" for i in "${!files[@]}"; do install -D -m 0600 "${targets[i]}" "$backup/${files[i]}"; done # Stage all files before replacing any of them. Do not modify config.json or NetWare data. staged=() cleanup(){ for f in "${staged[@]}"; do rm -f -- "$f"; done; } trap cleanup EXIT for i in "${!files[@]}"; do f=$(mktemp "${targets[i]}.tokenfix.XXXXXXXX"); staged+=("$f"); install -m 0644 "${files[i]}" "$f" done for i in "${!files[@]}"; do if ! mv -f -- "${staged[i]}" "${targets[i]}"; then for j in "${!files[@]}"; do install -m 0644 "$backup/${files[j]}" "${targets[j]}"; done echo "Install failed; original files restored from $backup" >&2; exit 1 fi done if systemctl is-active --quiet mars_vme.service && [[ -d /run/mars-pcap ]]; then touch /run/mars-pcap/browser-engine-update echo 'RetroBrowser worker reload requested; NetWare not restarted. Active browser sessions may end.' else echo 'New files will be used when mars_vme starts. If needed, start the service normally.' fi printf 'Backup: %s\nReload the administration web page (Ctrl+F5).\n' "$backup"