Files
HamPack/lib.sh
T
David YoungandClaude Opus 5.5 0b471c9771 Harden install, update and uninstall; add shared lib and tests
Updates
- install-compiled.sh never prompts: unknown versions rebuild, a missing
  version= fails
- Detect VARA Terminal by "VARA Terminal.exe" in drive_c/VARA, not by
  the folder VARA HF also uses
- install-windows-apps.sh checks first: nothing is downloaded and
  winetricks doesn't run unless a product is missing, and installers
  only run with a desktop session
- Flatpaks install system-wide from flathub and honor skip-packages
- Rewrite desktop files on every run so icon paths stay right; add the
  missing QtTermTCP desktop file
- Build steps can't read the rest of the conf through stdin

Uninstall
- update.sh records the packages and Flatpaks HamPack itself installed;
  hampackremove removes only those that nothing else needs, and lists
  the rest
- Read package lists from packages.conf instead of copies, remove
  files from install manifests, conf install paths and older builds,
  and revert the Wine Decorated setting

Security
- Replace the NOPASSWD sudoers file with a sudo keepalive, and delete a
  leftover /etc/sudoers.d/hampack
- Verify binaries.conf downloads against sha256=; pin dx.py to a commit
- Graywolf's web UI listens on 127.0.0.1; update.sh manages its unit
- Install direwolf and kiwix through a staging directory with
  install_staged, so nothing in ~/.local ends up owned by root; hand
  back any root-owned files left by older builds
- Use private temp directories instead of fixed /tmp paths

Code quality
- Shared lib.sh: conf parser, variable expansion, version records,
  install tracking, staged installs, sudo keepalive
- bats tests covering first install and update paths, Makefile with
  lint/test/hooks, pre-commit hook
- patch-potacat.sh uses jq; drop stale kiwix version entries

README: fill in what gets installed, uninstall and graywolf notes,
development section, typo and Matrix link fixes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 09:55:13 -07:00

130 lines
4.7 KiB
Bash

#!/bin/bash
#
# Purpose : Functions shared by HamPack's install, update and remove scripts.
# Source it; it doesn't do anything on its own.
HAMPACK_DIR="${HAMPACK_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
export HAMPACK_DIR
HAMPACK_STATE="${HAMPACK_STATE:-$HOME/.local/state/HamPack}"
VERSION_FILE="$HAMPACK_STATE/.installed_versions"
# Expand $HOME, $HAMPACK_DIR and $USER in a config value. Unlike `eval echo`,
# this leaves everything else (semicolons, quotes, spacing) untouched.
expand_vars() {
local v="$1"
v="${v//\$HAMPACK_DIR/"$HAMPACK_DIR"}"
v="${v//\$HOME/"$HOME"}"
v="${v//\$USER/"$USER"}"
printf '%s\n' "$v"
}
# Read an INI-style conf file and call CALLBACK once for each [section]. The
# callback finds the section's name in CONF[_name] and its keys, unexpanded, in
# CONF[key]. The file is read on fd 3, so commands the callback runs (builds,
# installers) can't swallow the rest of the file by reading stdin.
# shellcheck disable=SC2034 # CONF is read by the callbacks
parse_conf() {
local file="$1" callback="$2" line name have=0
declare -gA CONF=()
while IFS= read -r -u 3 line || [ -n "$line" ]; do
[[ "$line" =~ ^[[:space:]]*(#|$) ]] && continue
if [[ "$line" =~ ^\[(.+)\]$ ]]; then
# Take the name before the callback runs: its own =~ would overwrite BASH_REMATCH
name="${BASH_REMATCH[1]}"
(( have )) && "$callback"
CONF=([_name]="$name")
have=1
continue
fi
(( have )) && CONF["${line%%=*}"]="${line#*=}"
done 3< "$file"
(( have )) && "$callback"
return 0
}
# Print the version recorded for an app, or nothing if none is recorded
get_recorded_version() {
[ -f "$VERSION_FILE" ] || return 0
awk -v app="$1" 'index($0, app "=") == 1 { print substr($0, length(app) + 2); exit }' "$VERSION_FILE"
}
# Record the installed version of an app, replacing any earlier one
save_installed_version() {
forget_version "$1" && echo "$1=$2" >> "$VERSION_FILE"
}
# Drop an app's recorded version
forget_version() {
local tmp
mkdir -p "$HAMPACK_STATE" && touch "$VERSION_FILE" || return 1
tmp="$(mktemp "$VERSION_FILE.XXXXXX")" || return 1
awk -v app="$1" 'index($0, app "=") != 1' "$VERSION_FILE" > "$tmp" && mv -f "$tmp" "$VERSION_FILE"
}
# Add names to one of HamPack's lists of things it installed (installed-packages,
# installed-flatpaks), so hampackremove removes only those and not anything the
# user had before HamPack
record_installed() {
local list="$HAMPACK_STATE/$1"
shift
[ $# -gt 0 ] || return 0
mkdir -p "$HAMPACK_STATE" || return 1
{ cat "$list" 2> /dev/null; printf '%s\n' "$@"; } | sort -u > "$list.tmp" && mv -f "$list.tmp" "$list"
}
# Print the names in one of those lists
recorded_installs() {
[ -f "$HAMPACK_STATE/$1" ] && cat "$HAMPACK_STATE/$1"
return 0
}
# Copy a staged install (made with `make install DESTDIR=STAGE` or similar) into
# place, and write the files to a manifest that hampackremove uses. Files under
# $HOME are copied as you, so nothing in your home ends up owned by root; only
# files outside it (udev rules and the like) are copied with sudo. Meant for
# compile.conf steps, which run with HAMPACK_APP set to the app being built.
install_staged() {
local stage manifest file dest
stage="$(cd "$1" && pwd)" || return 1
manifest="$HAMPACK_STATE/manifests/${HAMPACK_APP:?HAMPACK_APP is not set}"
mkdir -p "$(dirname "$manifest")" || return 1
: > "$manifest.new"
while IFS= read -r -d '' file; do
dest="${file#"$stage"}"
if [[ "$dest" == "$HOME"/* ]]; then
mkdir -p "$(dirname "$dest")" && cp -P --remove-destination "$file" "$dest"
else
sudo mkdir -p "$(dirname "$dest")" && sudo cp -P --remove-destination "$file" "$dest"
fi || { echo " Error: could not install $dest"; rm -f "$manifest.new"; return 1; }
echo "$dest" >> "$manifest.new"
done < <(find "$stage" \( -type f -o -type l \) -print0 | sort -z)
mv -f "$manifest.new" "$manifest"
}
# Make a private work directory in $HAMPACK_TMP, removed when the script exits
make_work_dir() {
HAMPACK_TMP="$(mktemp -d -t hampack.XXXXXX)" || return 1
export HAMPACK_TMP
# shellcheck disable=SC2064 # expand now, so the trap removes this directory
trap "rm -rf '$HAMPACK_TMP'" EXIT
}
# Ask for the sudo password once (if sudo doesn't already have it cached), then
# keep it fresh until this script exits, so long builds don't stop to ask again.
# Unlike a passwordless sudoers entry, nothing is left behind if the script is killed.
start_sudo_keepalive() {
sudo -v || return 1
local parent=$$
# Detached from stdout/stderr, so a pipe reading this script's output (a log,
# a test runner) sees it finish when the script does, not up to 60s later
while kill -0 "$parent" 2> /dev/null; do
sleep 60
sudo -n -v
done > /dev/null 2>&1 < /dev/null &
disown
}