Disarm on offboard every sync gate onboard arms, with the same helper, so the two are one operation in both directions

This commit is contained in:
Gmer4Lfe
2026-08-16 21:36:46 -04:00
parent 3ae6298656
commit 27989d066e
4 changed files with 103 additions and 46 deletions
+46 -2
View File
@@ -939,9 +939,53 @@ is_own_conf_file() {
[[ "${1,,}" == "${MY_ID,,}.conf" ]]
}
# ── Set a boolean toggle in master.conf, in place ─────────────────────────────────────────────
# Booleans only. The value pattern stops at whitespace, so a quoted value containing spaces would
# be truncated — every gate this sets is true/false and nothing else should use it.
#
# Preserves what the line already looks like: leading indent, the existing quoting style, and the
# column the trailing comment sits in. master.conf is hand-aligned and read by a person; an edit
# that reflows a line makes a diff that looks bigger than the change actually is.
#
# Prefer this over update_master_conf() for toggles. That one rewrites the entire line as
# " KEY=VALUE", which drops the trailing comment — run it over the four sync gates and
# master.conf loses the notes explaining what each tier does.
#
# Lives here rather than in partnership_onboard.sh because onboard arming a gate and offboard
# disarming the same gate must be the same operation in both directions.
# Usage: set_conf_bool "$key" "true"|"false" "$master_conf_path"
set_conf_bool() {
local key="$1" val="$2" file="$3" cur pad spaces quoted gap
if ! grep -qE "^[[:space:]]*${key}=" "$file"; then
warn " ${key} is not in master.conf — skipped (add it to the template first)"
return 1
fi
cur=$(grep -E "^[[:space:]]*${key}=" "$file" | head -1 | sed -E "s|^[[:space:]]*${key}=||; s|[[:space:]]*#.*$||")
quoted=""; [[ "$cur" == \"*\" ]] && quoted='"'
cur="${cur//\"/}"
[[ "$cur" == "$val" ]] && { echo " ${key} already ${val}"; return 0; }
# Rewrite the whole gap rather than padding on top of the existing one. Adding
# (len(old) - len(new)) spaces in front of the gap that is already there holds the column for
# one edit and drifts on the next: false→true pads +1, true→false pads 0 but keeps that space,
# so every arm/disarm cycle pushes the comment one column right. An onboard and offboard pair
# is exactly that cycle, and master.conf is hand-aligned.
pad=0
if grep -qE "^[[:space:]]*${key}=[^#]*#" "$file"; then
# Existing run of spaces between the value and the #
gap=$(grep -E "^[[:space:]]*${key}=" "$file" | head -1 \
| sed -E "s|^[[:space:]]*${key}=[^#[:space:]]*||; s|#.*$||")
pad=$(( ${#gap} + ${#cur} - ${#val} ))
(( pad < 1 )) && pad=1
fi
spaces=$(printf '%*s' "$pad" '')
sed -i -E "s|^([[:space:]]*)${key}=[^#[:space:]]*[[:space:]]*(#.*)?$|\1${key}=${quoted}${val}${quoted}${spaces}\2|" "$file"
echo " ${key}: ${cur}${val}"
}
# Sets key=value in a flat state file (setup.db style) — updates in place if the
# key exists, appends if not. No indentation handling — for master.conf use
# partnership_manager.sh's update_master_conf() instead.
# key exists, appends if not. No indentation handling — for master.conf toggles use
# set_conf_bool() above.
# Usage: set_state_var "$state_file" "$key" "$value"
set_state_var() {
local file="$1" key="$2" value="$3"