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>
This commit is contained in:
David Young
2026-11-08 09:55:13 -07:00
co-authored by Claude Opus 5.5
parent ebda32d567
commit 0b471c9771
26 changed files with 1525 additions and 1015 deletions
+81 -258
View File
@@ -2,157 +2,60 @@
#
# Purpose : Compile and install applications from compile.conf
HAMPACK_DIR="${HAMPACK_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
export HAMPACK_DIR
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib.sh"
CONF_FILE="${HAMPACK_COMPILE_CONF:-$HAMPACK_DIR/compile.conf}"
TMP_DIR="/tmp/hampack-build"
VERSION_FILE="$HOME/.local/state/HamPack/.installed_versions"
APPLICATIONS_DIR="$HOME/.local/share/applications"
FAILED=()
# 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"
}
# Get the installed version of an app
get_installed_version() {
local app="$1"
local gui="$2"
# Check version file first — it's authoritative for compiled apps
if [ -f "$VERSION_FILE" ]; then
local stored
stored=$(grep "^$app=" "$VERSION_FILE" | cut -d'=' -f2)
if [ -n "$stored" ]; then
echo "$stored"
return
fi
fi
# Fall back to --version for non-GUI apps not yet in the version file
if [ "$gui" != "true" ]; then
if command -v "$app" &> /dev/null; then
local ver
ver=$("$app" --version 2>&1 | grep -oP '\d+\.\d+[\.\d]*' | head -n1)
if [ -n "$ver" ]; then
echo "$ver"
return
fi
fi
fi
echo "unknown"
}
# Save installed version to version file
save_installed_version() {
local app="$1"
local version="$2"
touch "$VERSION_FILE"
if grep -q "^$app=" "$VERSION_FILE"; then
sed -i "s/^$app=.*/$app=$version/" "$VERSION_FILE"
else
echo "$app=$version" >> "$VERSION_FILE"
fi
}
# Ask the user whether to proceed when version can't be determined
ask_user_proceed() {
local app="$1"
local latest="$2"
echo "Warning: could not determine installed version of $app."
echo "compile.conf specifies version $latest."
read -rp "Compile and install $app anyway? [y/N] " response
[[ "$response" =~ ^[yY] ]]
}
# Check if an update is needed
# Check if an update is needed. Never asks: this runs unattended under `update`.
needs_update() {
local app="$1"
local install_path="$2"
local latest="$3"
local gui="$4"
local app="$1" install_path="$2" latest="$3" installed
# Not installed at all
if ! command -v "$app" &> /dev/null && [ ! -f "$install_path" ]; then
if [ ! -e "$install_path" ]; then
echo " $app is not installed."
return 0
fi
local installed
installed=$(get_installed_version "$app" "$gui")
if [ "$installed" = "unknown" ]; then
if ask_user_proceed "$app" "$latest"; then
return 0
else
return 1
fi
installed="$(get_recorded_version "$app")"
if [ -z "$installed" ]; then
echo " Installed version of $app is unknown, building version $latest."
return 0
fi
echo " Installed version : $installed"
echo " Latest version : $latest"
if [ "$installed" = "$latest" ]; then
echo " $app is up to date, skipping."
echo " $app $installed is up to date."
return 1
fi
echo " Update available for $app."
echo " Updating $app from $installed to $latest."
return 0
}
# Generate desktop file with absolute paths
generate_desktop() {
local desktop="$1"
local tmp_desktop="/tmp/$(basename "$desktop")"
sed "s|\$HAMPACK_DIR|$HAMPACK_DIR|g; s|\$HOME|$HOME|g" "$desktop" > "$tmp_desktop"
echo "$tmp_desktop"
}
# Install desktop file if specified
# Install the app's .desktop file with $HAMPACK_DIR and $HOME filled in. Done on
# every run, not just after a build, so its paths stay right if HamPack moves.
install_desktop() {
local desktop="$1"
if [ -z "$desktop" ]; then
return
fi
[ -n "$desktop" ] || return 0
if [ ! -f "$desktop" ]; then
echo " Warning: desktop file $desktop not found, skipping."
return
return 0
fi
echo " Installing desktop file..."
mkdir -p "$HOME/.local/share/applications"
local resolved
resolved=$(generate_desktop "$desktop")
cp "$resolved" "$HOME/.local/share/applications/$(basename "$desktop")"
chmod +x "$HOME/.local/share/applications/$(basename "$desktop")"
rm -f "$resolved"
echo " Desktop file installed."
mkdir -p "$APPLICATIONS_DIR"
sed "s|\$HAMPACK_DIR|$HAMPACK_DIR|g; s|\$HOME|$HOME|g" "$desktop" \
> "$APPLICATIONS_DIR/$(basename "$desktop")"
}
# Find the tag for a version, named either "1.2.3" or "v1.2.3". Prints nothing
# if the repo has no tags at all; fails if it has tags but none for the version.
find_version_tag() {
local git_url="$1"
local version="$2"
local tags
local git_url="$1" version="$2" tags tag
tags=$(git ls-remote --tags --refs "$git_url" | sed 's|.*refs/tags/||') || return 1
[ -z "$tags" ] && return 0
local tag
for tag in "$version" "v$version"; do
if grep -qxF "$tag" <<< "$tags"; then
echo "$tag"
@@ -162,202 +65,128 @@ find_version_tag() {
return 1
}
# Fetch source via git or wget
# Fetch source via git or an archive download into SRC_DIR
fetch_source() {
local app="$1"
local git_url="$2"
local wget_url="$3"
local version="$4"
local ref="$5"
local src_dir="$TMP_DIR/$app"
local git_url="$1" wget_url="$2" version="$3" ref="$4" src_dir="$5"
rm -rf "$src_dir"
mkdir -p "$src_dir"
if [ -n "$git_url" ] && [ -n "$ref" ]; then
echo " Cloning $git_url at $ref..."
git clone "$git_url" "$src_dir" || return 1
git -C "$src_dir" -c advice.detachedHead=false checkout "$ref" || return 1
git clone --quiet "$git_url" "$src_dir" || return 1
git -C "$src_dir" -c advice.detachedHead=false checkout --quiet "$ref" || return 1
elif [ -n "$git_url" ]; then
local tag=""
if [ -n "$version" ]; then
if ! tag=$(find_version_tag "$git_url" "$version"); then
echo " Error: no tag $version or v$version in $git_url"
return 1
fi
local tag
if ! tag=$(find_version_tag "$git_url" "$version"); then
echo " Error: no tag $version or v$version in $git_url"
return 1
fi
if [ -n "$tag" ]; then
echo " Cloning $git_url at $tag..."
git -c advice.detachedHead=false clone --branch "$tag" "$git_url" "$src_dir" || return 1
git -c advice.detachedHead=false clone --quiet --branch "$tag" "$git_url" "$src_dir" || return 1
else
echo " $git_url has no tags; cloning the default branch..."
git clone "$git_url" "$src_dir" || return 1
git clone --quiet "$git_url" "$src_dir" || return 1
fi
elif [ -n "$wget_url" ]; then
echo " Downloading $wget_url..."
local filename="${wget_url##*/}"
local filepath="$TMP_DIR/$filename"
local filepath="$HAMPACK_TMP/$filename"
wget -q "$wget_url" -O "$filepath" || return 1
echo " Downloading $wget_url..."
curl -fsSL "$wget_url" -o "$filepath" || return 1
echo " Extracting $filename..."
case "$filename" in
*.zip)
unzip -q "$filepath" -d "$src_dir" || return 1
# If the zip extracted into a single subdirectory, flatten it
local contents
contents=$(ls -1 "$src_dir")
local count
count=$(echo "$contents" | wc -l)
if [ "$count" -eq 1 ] && [ -d "$src_dir/$contents" ]; then
echo " Descending into $contents..."
local inner="$src_dir/$contents"
mv "$inner"/* "$src_dir/"
rmdir "$inner"
local entries=("$src_dir"/*)
if [ ${#entries[@]} -eq 1 ] && [ -d "${entries[0]}" ]; then
echo " Descending into $(basename "${entries[0]}")..."
mv "${entries[0]}" "$src_dir.inner" && rmdir "$src_dir" && mv "$src_dir.inner" "$src_dir" || return 1
fi
;;
*.tar.gz|*.tgz)
tar -xzf "$filepath" -C "$src_dir" --strip-components=1 || return 1
;;
*.tar.bz2)
tar -xjf "$filepath" -C "$src_dir" --strip-components=1 || return 1
;;
*.tar.xz)
tar -xJf "$filepath" -C "$src_dir" --strip-components=1 || return 1
;;
*.tar.gz|*.tgz) tar -xzf "$filepath" -C "$src_dir" --strip-components=1 || return 1 ;;
*.tar.bz2) tar -xjf "$filepath" -C "$src_dir" --strip-components=1 || return 1 ;;
*.tar.xz) tar -xJf "$filepath" -C "$src_dir" --strip-components=1 || return 1 ;;
*)
echo " Error: unknown file type for $filename"
return 1
;;
esac
rm -f "$filepath"
else
echo " Error: no git or wget URL specified for $app"
echo " Error: no git= or wget= URL in compile.conf"
return 1
fi
}
# cd into source directory and run steps
# Run the comma-separated build steps from SRC_DIR, stopping at the first that
# fails. Steps run in this shell, so a `cd` in one carries on to the next.
run_steps() {
local app="$1"
local steps="$2"
local src_dir="$TMP_DIR/$app"
local steps="$1" src_dir="$2" step step_list
if [ ! -d "$src_dir" ]; then
echo " Error: source directory $src_dir not found."
return 1
fi
echo " Changing into $src_dir..."
cd "$src_dir" || return 1
echo " Building $app..."
IFS=',' read -ra step_list <<< "$steps"
for step in "${step_list[@]}"; do
step="${step#"${step%%[![:space:]]*}"}"
step="${step%"${step##*[![:space:]]}"}"
echo " Running: $step"
if ! eval "$step"; then
if ! eval "$step" < /dev/null; then
echo " Error: step failed: $step"
return 1
fi
done
}
# Process a single app
# Process the app in CONF (called by parse_conf for each section)
process_app() {
local app="$1"
local git_url="$2"
local wget_url="$3"
local install_path="$4"
local steps="$5"
local version="$6"
local desktop="$7"
local gui="$8"
local ref="$9"
local app="${CONF[_name]}" version="${CONF[version]}"
local install_path desktop
install_path="$(expand_vars "${CONF[install]}")"
desktop="$(expand_vars "${CONF[desktop]}")"
echo ""
echo "=== $app ==="
# Check if already installed
if command -v "$app" &> /dev/null || [ -f "$install_path" ]; then
if [ -z "$version" ]; then
echo " $app is already installed, skipping."
return
fi
fi
if [ -z "$version" ]; then
echo " Warning: no version specified in compile.conf for $app."
read -rp " Compile and install $app anyway? [y/N] " response
if [[ ! "$response" =~ ^[yY] ]]; then
echo " Skipping $app."
return
fi
elif ! needs_update "$app" "$install_path" "$version" "$gui"; then
return
fi
if ! fetch_source "$app" "$git_url" "$wget_url" "$version" "$ref"; then
echo " Error: could not fetch $app source, skipping."
if [ -z "$version" ] || [ -z "$install_path" ] || [ -z "${CONF[steps]}" ]; then
echo " Error: $app needs version=, install= and steps= in compile.conf, skipping."
FAILED+=("$app")
return
fi
# Steps run in this shell (they may `cd`), so return to a safe directory afterwards
if ! run_steps "$app" "$steps"; then
if needs_update "$app" "$install_path" "$version"; then
local src_dir="$HAMPACK_TMP/$app"
if ! fetch_source "$(expand_vars "${CONF[git]}")" "$(expand_vars "${CONF[wget]}")" \
"$version" "${CONF[ref]}" "$src_dir"; then
echo " Error: could not fetch $app source, skipping."
FAILED+=("$app")
return
fi
echo " Building $app..."
local status=0
HAMPACK_APP="$app" run_steps "${CONF[steps]}" "$src_dir" || status=1
cd "$HOME" || true
echo " Error: build of $app failed; version not recorded, will retry next update."
FAILED+=("$app")
return
rm -rf "$src_dir"
if (( status != 0 )); then
echo " Error: build of $app failed; it will be tried again on the next update."
FAILED+=("$app")
return
fi
save_installed_version "$app" "$version"
echo " $app $version installed."
fi
cd "$HOME" || true
install_desktop "$desktop"
save_installed_version "$app" "$version"
echo " $app $version installed successfully."
}
# Parse compile.conf and process each app
process_conf() {
local app="" git_url="" wget_url="" install_path="" steps="" version="" desktop="" gui="" ref=""
while IFS= read -r line || [ -n "$line" ]; do
[[ -z "$line" || "$line" == \#* ]] && continue
if [[ "$line" =~ ^\[(.+)\]$ ]]; then
if [ -n "$app" ]; then
process_app "$app" "$git_url" "$wget_url" "$install_path" "$steps" "$version" "$desktop" "$gui" "$ref"
fi
app="${BASH_REMATCH[1]}"
git_url="" wget_url="" install_path="" steps="" version="" desktop="" gui="" ref=""
continue
fi
local key="${line%%=*}"
local value="${line#*=}"
case "$key" in
git) git_url="$(expand_vars "$value")" ;;
ref) ref="$value" ;;
wget) wget_url="$(expand_vars "$value")" ;;
install) install_path="$(expand_vars "$value")" ;;
steps) steps="$value" ;;
version) version="$value" ;;
desktop) desktop="$(expand_vars "$value")" ;;
gui) gui="$value" ;;
esac
done < "$CONF_FILE"
# Process the last app in the file
if [ -n "$app" ]; then
process_app "$app" "$git_url" "$wget_url" "$install_path" "$steps" "$version" "$desktop" "$gui" "$ref"
fi
}
# --- Main ---
@@ -367,22 +196,16 @@ if [ ! -f "$CONF_FILE" ]; then
exit 1
fi
mkdir -p "$TMP_DIR"
mkdir -p "$HOME/.local/state/HamPack"
make_work_dir || exit 1
echo "Starting HamPack source builds..."
process_conf
parse_conf "$CONF_FILE" process_app
echo "Updating desktop database..."
update-desktop-database "$HOME/.local/share/applications/"
echo ""
echo "Cleaning up..."
rm -rf "$TMP_DIR"
mkdir -p "$APPLICATIONS_DIR"
update-desktop-database "$APPLICATIONS_DIR" 2> /dev/null
if (( ${#FAILED[@]} > 0 )); then
echo ""
echo "Failed to install: ${FAILED[*]}"
exit 1
fi
echo "All done."