Files
svxlink-talkgroups/update_talkgrups.sh
T
2026-11-14 12:30:29 +00:00

50 lines
1.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Aktualisiert die Talkgroup-Namen eines SvxLink-PHP-Dashboards.
set -Eeuo pipefail
SOURCE_URL='https://git.unixweb.net/joachim/svxlink-talkgroups/raw/branch/main/talkgroups.php'
TARGET="${1:-/var/www/html/include/tgdb.php}"
if (( EUID != 0 )); then
echo 'Fehler: Bitte mit sudo ausführen (Schreibrechte und Backup erforderlich).' >&2
exit 1
fi
for cmd in curl php mktemp cmp cp date dirname chmod chown mv; do
command -v "$cmd" >/dev/null || { echo "Fehlendes Programm: $cmd" >&2; exit 1; }
done
if [[ ! -f "$TARGET" ]]; then
echo "Zieldatei nicht gefunden: $TARGET" >&2
exit 1
fi
TARGET_DIR="$(dirname -- "$TARGET")"
TMP="$(mktemp "$TARGET_DIR/.tgdb-update.XXXXXXXX.php")"
trap 'rm -f -- "$TMP"' EXIT
curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \
--connect-timeout 10 --max-time 40 --retry 2 "$SOURCE_URL" -o "$TMP"
php -l "$TMP" >/dev/null
# Plausibilitätsprüfung: Datei muss das erwartete Array mit relevanten IDs bereitstellen.
php -r '
include $argv[1];
if (!isset($tgdb_array) || !is_array($tgdb_array) || count($tgdb_array) < 50
|| !isset($tgdb_array["0"], $tgdb_array["2628"])) {
fwrite(STDERR, "Unerwartete/zu kurze Talkgroup-Datenbank\n"); exit(1);
}
' "$TMP"
if cmp -s -- "$TMP" "$TARGET"; then
echo 'Bereits aktuell – keine Änderungen.'
exit 0
fi
BACKUP="${TARGET}.bak.$(date +%Y%m%d-%H%M%S)"
cp -p -- "$TARGET" "$BACKUP"
# Existierende Zugriffsrechte und Besitzer beibehalten; atomarer Austausch auf gleichem Dateisystem.
chmod --reference="$TARGET" "$TMP"
chown --reference="$TARGET" "$TMP"
mv -f -- "$TMP" "$TARGET"
echo "Aktualisiert: $TARGET"
echo "Backup: $BACKUP"