#!/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 }