Undo radio configure with radio unconfigure

Stop and remove the hampack-rigctld service and HamPack's flrig autostart,
and offer to put back each file radio configure backed up, from its oldest
backup. What's there now is backed up first, databases are copied through
SQLite, running programs are left alone, and --auto doesn't set a radio up
again until radio configure is run.

Backups made in the same second no longer overwrite each other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Young
2026-11-29 12:34:41 -07:00
co-authored by Claude Opus 5.5
parent 8096dcc97f
commit ad57b6b1a4
5 changed files with 295 additions and 7 deletions
+164 -3
View File
@@ -8,6 +8,7 @@
# radio probe PORT
# radio configure [--force] [--model N] [--flrig] [--no-cat --ptt HOW] [NUMBER]
# radio configure --auto
# radio unconfigure
# radio help
SELF="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
@@ -43,6 +44,9 @@ GRAYWOLF_DB="$HOME/.local/share/graywolf/graywolf.db"
FLRIG_DIR="$HOME/.flrig"
AUTOSTART_DIR="$CONFIG_HOME/autostart"
FLRIG_AUTOSTART="$AUTOSTART_DIR/hampack-flrig.desktop"
# Left by radio unconfigure, so radio configure --auto doesn't set a radio up
# again until you run radio configure
UNCONFIGURED="$HAMPACK_STATE/radio-unconfigured"
# flrig's baud rates; it saves the rate as its place in this list
FLRIG_BAUDS=(300 600 1200 2400 4800 9600 19200 38400 57600 115200 230400 460800)
DIREWOLF_CONF="$HAMPACK_STATE/conf.d/direwolf.conf"
@@ -89,6 +93,11 @@ Usage: radio [detect] [--probe]
radio recognises by its USB serial number, without asking
it, unless the radio set up already is plugged in. Stop it
with \`systemctl --user mask hampack-radio-plugged\`
unconfigure Undo radio configure: stop and remove the rigctld service
and flrig's autostart entry, and offer to put back each
program's settings from before radio configure changed
them. Radios aren't set up when they're plugged in again
until you next run radio configure
EOF
}
@@ -633,11 +642,21 @@ ini_set() {
' "$1" > "$tmp" && chmod --reference="$1" "$tmp" && mv -f "$tmp" "$1"
}
# Print a name for a backup of a file, FILE.bak-DATE-TIME, that isn't taken:
# two backups in a second get -2, -3...
backup_name() {
local name n=2
name="$1.bak-$(date +%Y%m%d-%H%M%S)"
local base="$name"
while [ -e "$name" ]; do name="$base-$((n++))"; done
echo "$name"
}
# Copy a file before changing it, and say so
back_up() {
[ -f "$1" ] || return 0
local copy
copy="$1.bak-$(date +%Y%m%d-%H%M%S)"
copy="$(backup_name "$1")"
cp -p "$1" "$copy" && BACKUPS+=("$copy")
}
@@ -1300,7 +1319,7 @@ configure_qlog() {
return
fi
local backup
backup="$QLOG_DB.bak-$(date +%Y%m%d-%H%M%S)"
backup="$(backup_name "$QLOG_DB")"
result="$(qlog_profile "$backup")"
case "$result" in
added)
@@ -1514,7 +1533,7 @@ configure_graywolf() {
fi
was_active=1
fi
backup="$GRAYWOLF_DB.bak-$(date +%Y%m%d-%H%M%S)"
backup="$(backup_name "$GRAYWOLF_DB")"
result="$(graywolf_setup "$backup" "$device" "$2")"
(( ! was_active )) || sudo systemctl start graywolf
read -r added changed kept <<< "$result"
@@ -1808,6 +1827,7 @@ configure() {
configure_potacat "$name"
configure_graywolf "$card" "$name"
rm -f "$UNCONFIGURED"
mkdir -p "$HAMPACK_STATE" &&
printf 'name=%s\nhamlib=%s\nport=%s\nbaud=%s\ncard=%s\ncontroller=%s\nptt=%s\n' "$name" "$model" "$port" "$baud" "$card" \
"$( (( USE_FLRIG )) && echo flrig || echo rigctld)" "${ptt:-cat}" > "$HAMPACK_STATE/radio"
@@ -1856,6 +1876,10 @@ notify() {
# Graywolf is left alone while it runs, since stopping it needs sudo
auto_configure() {
local i known=() name="" card_sys out status tries=0 skipped
if [ -f "$UNCONFIGURED" ]; then
echo "\`radio unconfigure\` was run, so radios aren't set up when they're plugged in until you run \`radio configure\`."
return 0
fi
# The radio's sound card may come up after its serial port
udevadm settle --timeout=10 > /dev/null 2>&1
find_radios
@@ -1901,10 +1925,147 @@ auto_configure() {
fi
}
# Copy an SQLite database through SQLite, which takes in what's still in its
# write-ahead log, and leaves no stale log to be applied to the copy:
# copy_db FROM TO
copy_db() {
python3 -I -c 'import sqlite3, sys
src, dst = sqlite3.connect(sys.argv[1]), sqlite3.connect(sys.argv[2])
src.backup(dst)
dst.close()
src.close()' "$1" "$2"
}
# The program a file radio configure changes belongs to, as NAME|PROCESS,
# with PROCESS "graywolf.service" for Graywolf's system service
program_of() {
case "$1" in
"$WSJTX_CONF") echo "WSJT-X|wsjtx" ;;
"$JS8CALL_CONF") echo "JS8Call|JS8Call" ;;
"$FLRIG_DIR"/*) echo "flrig|flrig" ;;
"$PAT_CONF") echo "Pat|pat" ;;
"$FLDIGI_CONF") echo "fldigi|fldigi" ;;
"$DIREWOLF_CONF") echo "Dire Wolf|direwolf" ;;
"$NOT1MM_CONF") echo "not1mm|not1mm" ;;
"$QLOG_DB") echo "QLog|qlog" ;;
"$POTACAT_DIR"/*) echo "POTACAT|potacat" ;;
"$GRAYWOLF_DB") echo "Graywolf|graywolf.service" ;;
esac
}
# Put a file back from a backup radio configure made, backing up what's there
# now first. Prints the copy of what was there: restore_backup FILE BACKUP
restore_backup() {
local copy
copy="$(backup_name "$1")"
if [[ "$1" == *.db ]]; then
if [ -f "$1" ]; then copy_db "$1" "$copy" || return 1; else copy=""; fi
copy_db "$2" "$1" || return 1
else
if [ -f "$1" ]; then cp -p "$1" "$copy" || return 1; else copy=""; fi
mkdir -p "$(dirname "$1")" && cp -p "$2" "$1" || return 1
fi
echo "$copy"
}
# radio unconfigure: undo radio configure. Stop and remove the rigctld
# service and flrig's autostart entry, then offer to put back each program's
# settings from the oldest backup radio configure made of them, which is
# from before it first changed them. Backups are found by their names, next
# to the files they're of
unconfigure() {
[ $# -eq 0 ] || { usage >&2; return 2; }
local did=0 b file files=() name process when copy answer was_active kept=()
local -A oldest=() newer=()
if [ -f "$RIGCTLD_UNIT" ]; then
systemctl --user disable --now hampack-rigctld 2> /dev/null
rm -f "$RIGCTLD_UNIT"
systemctl --user daemon-reload 2> /dev/null
report rigctld "stopped, and the hampack-rigctld service removed"
did=1
fi
if remove_flrig_autostart; then
report flrig "no longer starts when you log in"
did=1
fi
for b in "$WSJTX_CONF".bak-* "$JS8CALL_CONF".bak-* "$FLRIG_DIR"/*.prefs.bak-* "$PAT_CONF".bak-* \
"$FLDIGI_CONF".bak-* "$DIREWOLF_CONF".bak-* "$NOT1MM_CONF".bak-* "$QLOG_DB".bak-* \
"$POTACAT_DIR"/settings.json.bak-* "$POTACAT_DIR"/profiles/*/settings.json.bak-* "$GRAYWOLF_DB".bak-*; do
[[ -f "$b" && "$b" =~ \.bak-[0-9]{8}-[0-9]{6}(-[0-9]+)?$ ]] || continue
file="${b%.bak-*}"
# Globs sort, so the first backup of each file is the oldest (-2 sorts
# after the backup it came the same second as)
if [ -z "${oldest[$file]:-}" ]; then
oldest[$file]="$b"
files+=("$file")
else
newer[$file]=1
fi
done
if [ ${#files[@]} -gt 0 ]; then
did=1
echo
echo "radio configure backed up these settings before changing them. Putting them back loses anything you've changed since; what's there now is backed up first."
for file in "${files[@]}"; do
b="${oldest[$file]}"
IFS='|' read -r name process <<< "$(program_of "$file")"
when="${b##*.bak-}"
when="${when:0:4}-${when:4:2}-${when:6:2} at ${when:9:2}:${when:11:2}"
if [ "$process" != graywolf.service ] && running "$process"; then
report "$name" "left as it is, because it's running. Quit it and run \`radio unconfigure\` again"
kept+=("$name")
continue
fi
[ "$name" != QLog ] || echo " QLog's log holds your contacts: any you've logged since $when won't be in the log put back."
printf 'Put back %s, as it was on %s? [y/N] ' "${file/#$HOME/\~}" "$when"
answer=""
read -r answer || echo
if [[ ! "$answer" =~ ^[yY] ]]; then
kept+=("$name")
continue
fi
was_active=0
if [ "$process" = graywolf.service ] && systemctl is-active --quiet graywolf 2> /dev/null; then
if ! sudo systemctl stop graywolf; then
report "$name" "left as it is: couldn't stop it to put its settings back"
kept+=("$name")
continue
fi
was_active=1
fi
if copy="$(restore_backup "$file" "$b")"; then
report "$name" "put back${copy:+. What was there is in $copy}"
else
report "$name" "couldn't put back ${file/#$HOME/\~} from $b"
kept+=("$name")
fi
(( ! was_active )) || sudo systemctl start graywolf
done
[ -z "${newer[*]}" ] || echo "Later backups radio configure made are still there, next to each file."
fi
rm -f "$HAMPACK_STATE/radio"
mkdir -p "$HAMPACK_STATE" && touch "$UNCONFIGURED"
echo
if (( ! did )); then
echo "radio configure hasn't set anything up, so there's nothing to undo."
elif [ ${#kept[@]} -gt 0 ]; then
local IFS='|' list
list="${kept[*]}"
list="$(sort -u <<< "${list//|/$'\n'}" | paste -sd '|')"
echo "${list//|/, } may still use rigctld on $RIGCTLD_ADDR, which isn't running now: set their rig control yourself, or run \`radio configure\` to set it up again."
fi
echo "Radios aren't set up when they're plugged in until you run \`radio configure\` again."
}
case "${1:-detect}" in
detect) shift; detect "$@" ;;
probe) shift; probe "$@" ;;
configure) shift; configure "$@" ;;
unconfigure) shift; unconfigure "$@" ;;
help | -h | --help) usage ;;
--probe) detect --probe ;;
*) usage >&2; exit 2 ;;