From 0b471c9771cbd0a513985085c5e92e1001a77e40 Mon Sep 17 00:00:00 2001 From: David Young Date: Sun, 8 Nov 2026 09:55:13 -0700 Subject: [PATCH] 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 --- .githooks/pre-commit | 3 + .shellcheckrc | 2 + Makefile | 20 ++ README.md | 53 +++++- binaries.conf | 21 ++- compile.conf | 27 ++- hampackcheck | 142 ++++---------- hampackremove | 356 +++++++++++++++++++++--------------- install-binaries.sh | 235 ++++++------------------ install-compiled.sh | 339 ++++++++-------------------------- install-flatpaks.sh | 30 ++- install-windows-apps.sh | 303 ++++++++---------------------- install.sh | 12 +- lib.sh | 129 +++++++++++++ packages.conf | 10 + patch-potacat.sh | 40 ++-- sudo.sh | 31 ---- systemd/graywolf.service | 5 +- tests/hampackremove.bats | 93 ++++++++++ tests/helpers.bash | 108 +++++++++++ tests/install-binaries.bats | 88 +++++++++ tests/install-compiled.bats | 127 +++++++++++++ tests/installers.bats | 85 +++++++++ tests/lib.bats | 105 +++++++++++ tests/update.bats | 98 ++++++++++ update.sh | 78 ++++++-- 26 files changed, 1525 insertions(+), 1015 deletions(-) create mode 100755 .githooks/pre-commit create mode 100644 .shellcheckrc create mode 100644 Makefile create mode 100644 lib.sh delete mode 100755 sudo.sh create mode 100644 tests/hampackremove.bats create mode 100644 tests/helpers.bash create mode 100644 tests/install-binaries.bats create mode 100644 tests/install-compiled.bats create mode 100644 tests/installers.bats create mode 100644 tests/lib.bats create mode 100644 tests/update.bats diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..1112327 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,3 @@ +#!/bin/bash +# Installed with `make hooks`. Skip once with `git commit --no-verify`. +exec make --no-print-directory check diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..7fb1322 --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,2 @@ +# Scripts source lib.sh and CoreDesktop's utils.sh through variables +disable=SC1090,SC1091 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..247c4c4 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +# HamPack development tasks +# make lint - shellcheck every script +# make test - run the bats tests in tests/ +# make hooks - run `make lint test` before each commit + +SCRIPTS := install.sh update.sh lib.sh install-binaries.sh install-compiled.sh \ + install-flatpaks.sh install-windows-apps.sh patch-potacat.sh hampackcheck hampackremove + +.PHONY: check lint test hooks + +check: lint test + +lint: + shellcheck -x $(SCRIPTS) + +test: + bats tests/ + +hooks: + git config core.hooksPath .githooks diff --git a/README.md b/README.md index e2f1377..a830db6 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,12 @@ A collection of Ham Radio utilities and applications built on top of [CoreDeskto ## Introduction -HamPack (and it's sister project HamPackServer) is a tool box for ham radio operators to install a vast array of applications and utilities useful to the operator under many different operating circumstances. +HamPack (and its sister project HamPackServer) is a tool box for ham radio operators to install a vast array of applications and utilities useful to the operator under many different operating circumstances. The main objectives of this project are to: -1. Remove the Windows hegemony from ham radio operating today. No one benefits from a reliance on Windows. It is a closed system with many proven security risks, it is anti-thetical to the "tinkering" ethos of ham radio, and it inevitably increases the costs of operating. -1. Collaborate with other ham radio operators and enthusiasts on software and platforms beneficials to all operators and enthusiasts. Embracing open source and Linux allow ham radio software to enter a new Cambrian ago of software flourishing. +1. Remove the Windows hegemony from ham radio operating today. No one benefits from a reliance on Windows. It is a closed system with many proven security risks, it is antithetical to the "tinkering" ethos of ham radio, and it inevitably increases the costs of operating. +1. Collaborate with other ham radio operators and enthusiasts on software and platforms beneficial to all operators and enthusiasts. Embracing open source and Linux allows ham radio software to enter a new Cambrian age of software flourishing. 1. Have some fun. We build antennas and radios. Why not extend the fun to our software? ## Installation @@ -20,19 +20,42 @@ sibling add https://gitea.young.computer/david/HamPack.git This clones HamPack into `~/.local/share/siblings/HamPack` and runs its `install.sh`. From then on, CoreDesktop's `update` command pulls HamPack and runs its `update.sh`, so HamPack stays current along with the rest of the system. Re-run the `sibling add` command above to repair an install. -To uninstall HamPack and the applications it installed, run `hampackremove`. +HamPack asks for your sudo password once when it installs; under `update`, CoreDesktop has already asked. Nothing is left in `/etc/sudoers.d`. + +To uninstall HamPack and the applications it installed, run `hampackremove`. It removes only the packages and Flatpaks HamPack installed itself, and of those, only the ones no other software needs. Packages you already had are left alone. HamPack started keeping this record in November 2026, so packages from earlier installs aren't removed; `hampackremove` lists them, with the command to remove any you don't want. If you installed HamPack before it became a sibling project, run the `sibling add` command above once. It moves HamPack to the new location and removes the old update hooks. -## What Get's Installed +## What Gets Installed -This is the long list of stuff that gets installed... +Applications are pinned to tested versions, which we try to keep up with. Pinned versions are in `binaries.conf` and `compile.conf`, and `./hampackcheck` reports newer upstream releases. -Note: we try to keep up with the latest versions. +**From Arch and the AUR** (`packages.conf`): fldigi, flrig, Hamlib, CHIRP, GridTracker 2, chattervox, Pat (Winlink), QGIS, SPLAT!, VOACAP, GPSBabel, Wine and winetricks, plus the libraries and build tools the applications below need. + +**Flatpaks from Flathub** (`packages.conf`): WSJT-X and QLog. + +**Built from source** (`compile.conf`): + +- Dire Wolf: software TNC and APRS digipeater/iGate +- QtTermTCP: packet radio terminal +- Kiwix Desktop and kiwix-tools: offline Wikipedia and other ZIM archives +- Dump1090: ADS-B decoder +- YAAC: APRS client +- POTACAT: POTA spot aggregator with CAT control and logging +- JS8Call (JS8Call-improved): keyboard-to-keyboard HF messaging +- Graywolf: APRS transceiver and gateway, run as a systemd service + +**Pre-built downloads** (`binaries.conf`, each checked against its SHA-256): ardopcf (ARDOP modem), Paracon (packet terminal) and dx.py (DX cluster client). + +**Others:** not1mm (contest logger, installed with `uv`), and VARA HF, VARA FM, VARA Terminal and Winlink Express under Wine. The Windows installers need you to click through them, so they run only when one of these is missing and you're at the desktop. + +### Graywolf's web UI + +Graywolf's web UI is at http://localhost:8080 and, by default, can only be reached from the machine it runs on. To reach it from other machines, run `sudo systemctl edit graywolf` and override `ExecStart` with `-http 0.0.0.0:8080`. Changes made that way survive HamPack updates; edits to `/etc/systemd/system/graywolf.service` itself don't. ## Why CoreDesktop and/or Arch Linux? -There's no reason HamPack could run on a Debian, Ubuntu, Fedora or any man other distributions of Linux and all the libraries and utilities required for getting the ham radio software offered running. And let's hope that happens, in time. +There's no reason HamPack couldn't run on Debian, Ubuntu, Fedora or any of the many other distributions of Linux, with all the libraries and utilities required for getting the ham radio software offered running. And let's hope that happens, in time. But... @@ -42,8 +65,18 @@ Arch Linux is a wonderful community of hackers ensuring most *anything* will run But... -There's not reason why someone might want to collaborate on HamPack and "port" it to other flavors of Linux. That would be great! +There's no reason why someone couldn't collaborate on HamPack and "port" it to other flavors of Linux. That would be great! + +## Development + +HamPack is a set of Bash scripts. `install.sh` runs once, from `sibling add`; `update.sh` runs on every `update` and does the actual installing, so every step must work both on a fresh machine and on one that already has everything. Shared functions are in `lib.sh`. + +- `make lint` runs shellcheck on every script +- `make test` runs the bats tests in `tests/`, which stub out sudo, pacman, Flatpak and systemd, so they're safe to run +- `make hooks` runs both before each commit + +To add an application, add a section to `binaries.conf` or `compile.conf`; the comments at the top of each describe the fields. ## How to Participate... -You can help to develop HamPack by submitting "issues" to the repo. If you'd like to contribute code and/or pull requests, contact me on [email](mailto:david@young.computer) or [matrix](@david:matrix.young.computer). +You can help to develop HamPack by submitting "issues" to the repo. If you'd like to contribute code and/or pull requests, contact me on [email](mailto:david@young.computer) or [Matrix](https://matrix.to/#/@david:matrix.young.computer). diff --git a/binaries.conf b/binaries.conf index f47479e..4e4dcfb 100644 --- a/binaries.conf +++ b/binaries.conf @@ -1,19 +1,25 @@ # binaries.conf - Pre-built binaries to download and install # # Fields: -# version= - version string for update checking -# url= - direct download URL for the binary -# install= - destination path (e.g. $HOME/.local/bin/appname) +# version= - version string for update checking (required). Changing it +# downloads the binary again on the next update +# url= - direct download URL for the binary (required) +# sha256= - SHA-256 of the download. A download that doesn't match isn't +# installed. Update it along with url= and version= +# install= - destination path, e.g. $HOME/.local/bin/appname (required) # exec= - command for the .desktop Exec= field (defaults to install path if omitted) # gui=true - set if a .desktop file should be generated. No .desktop file is created if false # comment= - description for the .desktop file # icon= - icon theme name (e.g. wsjtx) or path to icon file (e.g. $HAMPACK_DIR/desktop/icons/wsjtx.png) # categories= - desktop categories (default: HamRadio;) # +# $HOME, $HAMPACK_DIR and $USER are expanded in every field. +# # Example: # [wsjtx] # version=2.7.0 # url=https://example.com/wsjtx-2.7.0-x86_64.AppImage +# sha256=0123456789abcdef... # install=$HOME/.local/bin/wsjtx # gui=true # comment=Weak signal digital modes for amateur radio @@ -24,12 +30,14 @@ [ardop] version=1.0.4.1.3 url=https://github.com/pflarue/ardop/releases/download/1.0.4.1.3/ardopcf_amd64_Linux_64 +sha256=caf256ca1138ea992323bf8c6729a06aaaacb1b62181d0a7b2abba3dffaa9ad8 install=$HOME/.local/bin/ardopcf_amd64_Linux_64 gui=false [paracon] version=1.3.0 url=https://github.com/mfncooper/paracon/releases/download/v1.3.0/paracon_1.3.0.pyz +sha256=44679cedff613c0c52c1691cf0c047796345ac54890c6a8325afb4d31885999b install=$HOME/.local/bin/paracon.pyz gui=true comment=Packet radio terminal @@ -37,3 +45,10 @@ exec=python $HOME/.local/bin/paracon.pyz icon=$HAMPACK_DIR/desktop/icons/paracon.png categories=HamRadio; +# dx.py has no releases, so it's pinned to a commit; version= is that commit +[dx.py] +version=8c742c5f5d37c0a4af26e9b3f9d4be27164c108f +url=https://raw.githubusercontent.com/HB9VQQ/dx.py/8c742c5f5d37c0a4af26e9b3f9d4be27164c108f/dx.py +sha256=98510e407f64e5ebcf7754ef29265a163756c585e7b3f89c803a43bc8834b68e +install=$HOME/.local/bin/dx.py +gui=false diff --git a/compile.conf b/compile.conf index cbcd4f4..d7f07c7 100644 --- a/compile.conf +++ b/compile.conf @@ -1,15 +1,22 @@ # compile.conf - Applications to build from source and install # # Fields: -# version= - version string for update checking +# version= - version string for update checking (required). Changing it +# rebuilds the app on the next update # git= - git repository URL to clone (mutually exclusive with wget). Builds the # tag named after the version (1.2.3 or v1.2.3), or the default branch # if the repo has no tags # ref= - commit to build instead of the version's tag, for repos whose tags are wrong # wget= - URL of a zip/tar archive to download and extract (mutually exclusive with git) -# install= - path to the installed binary, used to detect if already installed -# steps= - comma-separated build commands, run in order from the source directory -# $HOME is expanded; use sudo for steps that require elevated privileges +# install= - path to the installed binary, used to detect if already installed (required) +# steps= - comma-separated build commands, run in order from a private copy of +# the source (required). A step that fails stops the build, and the +# app is tried again on the next update. Steps run unattended, so +# they mustn't ask questions. $HOME, $USER and $HAMPACK_DIR are set. +# For an app that installs many files, install to a staging +# directory and finish with `install_staged DIR` (see lib.sh): it +# copies the files into place without leaving anything owned by +# root in your home, and records them for hampackremove # gui=true - set if a .desktop file should be installed # desktop= - path to the .desktop file to install (required when gui=true) @@ -17,7 +24,7 @@ version=1.8.1 git=https://github.com/wb2osz/direwolf.git install=$HOME/.local/bin/direwolf -steps=mkdir build, cd build, cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/.local, make -j4, sudo make install, sudo udevadm control --reload-rules, mkdir -p $HOME/.local/state/HamPack/conf.d, cp -n $HAMPACK_DIR/conf.d/direwolf.conf $HOME/.local/state/HamPack/conf.d/ +steps=mkdir build, cd build, cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/.local, make -j4, make install DESTDIR=$PWD/stage, install_staged stage, sudo rm -f /etc/udev/rules.d/99-direwolf-cmedia.rules, sudo udevadm control --reload-rules, mkdir -p $HAMPACK_STATE/conf.d, cp -n $HAMPACK_DIR/conf.d/direwolf.conf $HAMPACK_STATE/conf.d/ [QtTermTCP] version=0.81 @@ -25,6 +32,7 @@ git=https://github.com/g8bpq/QtTermTCP.git # Upstream tags are one release behind: tag 0.81 is the 0.0.0.79 commit ref=00ca15573c4653882265d6bfc884fca619556c30 install=$HOME/.local/bin/QtTermTCP +desktop=$HAMPACK_DIR/desktop/qttermtcp.desktop gui=true steps=rm -f *.o .qmake.stash Makefile, qmake-qt5, make, mv QtTermTCP $HOME/.local/bin/ @@ -33,13 +41,13 @@ version=2.5.1 git=https://github.com/kiwix/kiwix-desktop.git install=$HOME/.local/bin/kiwix-desktop gui=true -steps=/usr/lib/qt6/bin/qmake PREFIX=$HOME/.local "QMAKE_CXXFLAGS+=-Wno-error=sfinae-incomplete", make, make install +steps=/usr/lib/qt6/bin/qmake PREFIX=$HOME/.local "QMAKE_CXXFLAGS+=-Wno-error=sfinae-incomplete", make, make install INSTALL_ROOT=$PWD/stage, install_staged stage [kiwix-tools] version=3.8.2 git=https://github.com/kiwix/kiwix-tools.git install=$HOME/.local/bin/kiwix-serve -steps=sudo chown -R $USER:$USER $HOME/.local/share/man, meson setup build --prefix=$HOME/.local, ninja -C build, ninja -C build install +steps=meson setup build --prefix=$HOME/.local, ninja -C build, DESTDIR=$PWD/stage ninja -C build install, install_staged stage [Dump1090] version=1.0.0 @@ -53,7 +61,7 @@ wget=https://www.ka2ddo.org/ka2ddo/YAAC.zip install=$HOME/.local/bin/yaac/YAAC.jar desktop=$HAMPACK_DIR/desktop/yaac.desktop gui=true -steps=rm -rf $HOME/.local/bin/yaac, mkdir -p $HOME/.local/bin/yaac, mv /tmp/hampack-build/yaac/* $HOME/.local/bin/yaac/ +steps=rm -rf $HOME/.local/bin/yaac, mkdir -p $HOME/.local/bin/yaac, mv ./* $HOME/.local/bin/yaac/ [potacat] version=1.11.7 @@ -72,8 +80,9 @@ desktop=$HAMPACK_DIR/desktop/js8call.desktop gui=true steps=mkdir build, cd build, cmake -D CMAKE_INSTALL_PREFIX=/opt/JS8Call-improved .., cmake --build . -- -j 4, mv JS8Call $HOME/.local/bin/JS8Call +# update.sh installs and enables graywolf's systemd service once it's built [graywolf] version=0.14.14 git=https://github.com/chrissnell/graywolf.git install=$HOME/.local/bin/graywolf -steps=getent group dialout || sudo groupadd dialout, make all, install -m 755 bin/graywolf $HOME/.local/bin/graywolf, install -m 755 bin/graywolf-modem $HOME/.local/bin/graywolf-modem, mkdir -p $HOME/.local/share/graywolf, sed "s|HAMPACK_USER|$USER|g; s|HAMPACK_HOME|$HOME|g" $HAMPACK_DIR/systemd/graywolf.service > /tmp/graywolf.service, sudo install -m 644 /tmp/graywolf.service /etc/systemd/system/graywolf.service, sudo systemctl daemon-reload, sudo systemctl enable graywolf +steps=make all, install -m 755 bin/graywolf $HOME/.local/bin/graywolf, install -m 755 bin/graywolf-modem $HOME/.local/bin/graywolf-modem diff --git a/hampackcheck b/hampackcheck index 9398207..95df264 100755 --- a/hampackcheck +++ b/hampackcheck @@ -1,34 +1,24 @@ #!/bin/bash # # Purpose: Check for available version updates in binaries.conf and compile.conf +# (a tool for HamPack maintainers; it changes nothing) + +source "$(dirname "$(realpath "$0")")/lib.sh" -SCRIPT_DIR=$(dirname "$(realpath "$0")") -HAMPACK_DIR="${HAMPACK_DIR:-$SCRIPT_DIR}" BINARIES_CONF="${HAMPACK_BINARIES_CONF:-$HAMPACK_DIR/binaries.conf}" COMPILE_CONF="${HAMPACK_COMPILE_CONF:-$HAMPACK_DIR/compile.conf}" -# Extract "owner/repo" from a GitHub URL, or empty string if not GitHub -github_repo_from_url() { - local url="$1" - if [[ "$url" =~ github\.com/([^/]+)/([^/]+) ]]; then - echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}" - fi -} - # Fetch latest release tag via GitHub API, stripping leading "v" github_latest() { - local repo="$1" - curl -sf "https://api.github.com/repos/$repo/releases/latest" \ - | grep '"tag_name"' \ - | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/' \ + curl -sf "https://api.github.com/repos/$1/releases/latest" \ + | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' \ | sed 's/^v//' } # Latest release tag in a git repo, stripping leading "v". Only plain version # tags count (1.2.3 or v1.2.3), not pre-releases like 1.8-beta1. git_latest_tag() { - local url="$1" - git ls-remote --tags --refs "$url" 2>/dev/null \ + git ls-remote --tags --refs "$1" 2> /dev/null \ | sed 's|.*refs/tags/||' \ | grep -E '^v?[0-9]+(\.[0-9]+)+$' \ | sed 's/^v//' \ @@ -36,106 +26,51 @@ git_latest_tag() { | tail -n1 } +# Latest commit on a GitHub repo's default branch +github_head() { + git ls-remote "https://github.com/$1" HEAD 2> /dev/null | cut -f1 +} + print_result() { - local app="$1" - local pinned="$2" - local latest="$3" - local manual_url="$4" - local source="${5:-GitHub API}" + local app="$1" pinned="$2" latest="$3" manual_url="$4" source="${5:-GitHub API}" if [ -n "$manual_url" ]; then - printf " %-20s pinned=%-15s (manual check: %s)\n" "$app" "$pinned" "${manual_url%/*}" + printf " %-20s pinned=%-15s (manual check: %s)\n" "$app" "${pinned:0:15}" "${manual_url%/*}" elif [ -z "$latest" ]; then - printf " %-20s pinned=%-15s (%s unavailable)\n" "$app" "$pinned" "$source" + printf " %-20s pinned=%-15s (%s unavailable)\n" "$app" "${pinned:0:15}" "$source" elif [ "$pinned" = "$latest" ]; then - printf " %-20s up to date (%s)\n" "$app" "$pinned" + printf " %-20s up to date (%s)\n" "$app" "${pinned:0:15}" else - printf " %-20s pinned=%-15s latest=%-15s ** UPDATE AVAILABLE **\n" "$app" "$pinned" "$latest" + printf " %-20s pinned=%-15s latest=%-15s ** UPDATE AVAILABLE **\n" "$app" "${pinned:0:15}" "${latest:0:15}" fi } -check_app() { - local app="$1" - local version="$2" - local url="$3" # git= or url= field (may be GitHub) - local fallback="$4" # wget= or non-GitHub url for manual check +# parse_conf callback for binaries.conf +check_binary() { + local app="${CONF[_name]}" version="${CONF[version]}" url="${CONF[url]}" + [ -n "$version" ] || return 0 + [ -n "${CONF[sha256]}" ] || echo " $app has no sha256=, so its download isn't verified" - [ -z "$app" ] || [ -z "$version" ] && return - - local gh_repo - gh_repo=$(github_repo_from_url "$url") - - if [ -n "$gh_repo" ]; then - print_result "$app" "$version" "$(github_latest "$gh_repo")" "" + if [[ "$url" =~ ^https://raw\.githubusercontent\.com/([^/]+/[^/]+)/ ]]; then + print_result "$app" "$version" "$(github_head "${BASH_REMATCH[1]}")" "" "git" + elif [[ "$url" =~ github\.com/([^/]+)/([^/]+) ]]; then + print_result "$app" "$version" "$(github_latest "${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}")" "" else - print_result "$app" "$version" "" "${fallback:-$url}" - fi -} - -check_binaries() { - echo "=== binaries.conf ===" - local app="" version="" url="" - - while IFS= read -r line || [ -n "$line" ]; do - [[ -z "$line" || "$line" == \#* ]] && continue - - if [[ "$line" =~ ^\[(.+)\]$ ]]; then - check_app "$app" "$version" "$url" "" - app="${BASH_REMATCH[1]}" version="" url="" - continue - fi - - local key="${line%%=*}" value="${line#*=}" - case "$key" in - version) version="$value" ;; - url) url="$value" ;; - esac - done < "$BINARIES_CONF" - - check_app "$app" "$version" "$url" "" -} - -# compile.conf builds git= apps from the tag matching their version, so compare -# against the repo's tags rather than GitHub releases -check_compiled_app() { - local app="$1" - local version="$2" - local git_url="$3" - local wget_url="$4" - - [ -z "$app" ] || [ -z "$version" ] && return - - if [ -n "$git_url" ]; then - print_result "$app" "$version" "$(git_latest_tag "$git_url")" "" "version tags" - else - check_app "$app" "$version" "" "$wget_url" + print_result "$app" "$version" "" "$url" fi } +# parse_conf callback for compile.conf. git= apps are built from the tag +# matching their version, so compare against the repo's tags, not releases. check_compiled() { - echo "" - echo "=== compile.conf ===" - local app="" name="" version="" git_url="" wget_url="" + local app="${CONF[name]:-${CONF[_name]}}" version="${CONF[version]}" + [ -n "$version" ] || return 0 - while IFS= read -r line || [ -n "$line" ]; do - [[ -z "$line" || "$line" == \#* ]] && continue - - if [[ "$line" =~ ^\[(.+)\]$ ]]; then - check_compiled_app "${name:-$app}" "$version" "$git_url" "$wget_url" - app="${BASH_REMATCH[1]}" name="" version="" git_url="" wget_url="" - continue - fi - - local key="${line%%=*}" value="${line#*=}" - case "$key" in - name) name="$value" ;; - version) version="$value" ;; - git) git_url="$value" ;; - wget) wget_url="$value" ;; - esac - done < "$COMPILE_CONF" - - check_compiled_app "${name:-$app}" "$version" "$git_url" "$wget_url" + if [ -n "${CONF[git]}" ]; then + print_result "$app" "$version" "$(git_latest_tag "${CONF[git]}")" "" "version tags" + else + print_result "$app" "$version" "" "${CONF[wget]}" + fi } # --- Main --- @@ -148,7 +83,8 @@ for conf in "$BINARIES_CONF" "$COMPILE_CONF"; do done echo "Checking HamPack application versions..." -check_binaries -check_compiled +echo "=== binaries.conf ===" +parse_conf "$BINARIES_CONF" check_binary echo "" -echo "Done." +echo "=== compile.conf ===" +parse_conf "$COMPILE_CONF" check_compiled diff --git a/hampackremove b/hampackremove index 91fde59..e3fe02b 100755 --- a/hampackremove +++ b/hampackremove @@ -1,4 +1,4 @@ -#!/bin/bash -i +#!/bin/bash # # Purpose : HamPack uninstaller @@ -20,174 +20,242 @@ print_logo() { EOF } +COREDESKTOP="$HOME/.local/share/coredesktop" +APPLICATIONS_DIR="$HOME/.local/share/applications" +BIN="$HOME/.local/bin" + +# Print the path of the HamPack clone that `sibling add` made +find_hampack_dir() { + local dir + for dir in "$HOME"/.local/share/siblings/*/; do + [ -d "$dir/.git" ] || continue + if [[ "$(git -C "$dir" remote get-url origin 2> /dev/null)" =~ /[Hh]am[Pp]ack(\.git)?/?$ ]]; then + echo "${dir%/}" + return 0 + fi + done + return 1 +} + +# Remove a file, with sudo if it's outside your home +remove_file() { + if [[ "$1" == "$HOME"/* ]]; then + rm -f "$1" + else + sudo rm -f "$1" + fi +} + +# Remove the files install_staged recorded for each app it installed +remove_manifests() { + local manifest file + for manifest in "$HAMPACK_STATE"/manifests/*; do + [ -f "$manifest" ] || continue + while IFS= read -r file; do + [ -n "$file" ] && remove_file "$file" + done < "$manifest" + done +} + +# parse_conf callbacks: remove each app's binary and desktop file +remove_binary_app() { + rm -f "$(expand_vars "${CONF[install]}")" + [ "${CONF[gui]}" = "true" ] && rm -f "$APPLICATIONS_DIR/${CONF[_name]}.desktop" + return 0 +} +remove_compiled_app() { + rm -f "$(expand_vars "${CONF[install]}")" + [ -n "${CONF[desktop]}" ] && rm -f "$APPLICATIONS_DIR/$(basename "${CONF[desktop]}")" + return 0 +} + +# Files from builds made before HamPack recorded what builds install +remove_unrecorded_build_files() { + local name + # direwolf + for name in direwolf decode_aprs text2tt tt2text ll2utm utm2ll aclients log2gpx gen_packets \ + atest ttcalc kissutil tnctest appserver cm108 dwespeak.sh telem-balloon.pl telem-bits.pl \ + telem-data.pl telem-data91.pl telem-eqns.pl telem-parm.pl telem-seq.sh telem-unit.pl telem-volts.py; do + rm -f "$BIN/$name" + done + rm -f "$BIN"/direwolf-* + for name in direwolf decode_aprs kissutil aclients atest gen_packets ll2utm log2gpx text2tt tt2text utm2ll; do + rm -f "$HOME/.local/share/man/man1/$name.1" + done + rm -rf "$HOME/.local/share/direwolf" "$HOME/.local/share/doc/direwolf" + rm -f "$APPLICATIONS_DIR/direwolf.desktop" "$HOME/.local/share/pixmaps/direwolf_icon.png" + sudo rm -f /etc/udev/rules.d/99-direwolf-cmedia.rules + # kiwix-desktop and kiwix-tools + rm -f "$BIN"/kiwix-desktop "$BIN"/kiwix-serve "$BIN"/kiwix-manage "$BIN"/kiwix-search "$BIN"/kiwix-lib + rm -f "$HOME"/.local/share/man/man1/kiwix-*.1 "$HOME"/.local/share/man/*/man1/kiwix-*.1 + rm -f "$APPLICATIONS_DIR/org.kiwix.desktop.desktop" "$HOME/.local/share/metainfo/org.kiwix.desktop.appdata.xml" + rm -f "$HOME"/.local/share/icons/hicolor/*/apps/kiwix-desktop.* "$HOME"/.local/share/icons/hicolor/*/mimetypes/org.kiwix.desktop.* + rm -f "$HOME/.local/share/mime/packages/org.kiwix.desktop-mime.xml" + # graywolf, yaac, paracon + rm -f "$BIN/graywolf-modem" + rm -rf "$HOME/.local/share/graywolf" "$BIN/yaac" + rm -f "$BIN"/paracon_*.pyz + return 0 +} + +remove_graywolf_service() { + [ -f /etc/systemd/system/graywolf.service ] || return 0 + sudo systemctl disable --now graywolf + sudo rm -f /etc/systemd/system/graywolf.service + sudo systemctl daemon-reload +} + +remove_wine_apps() { + local prefix="${WINEPREFIX:-$HOME/.wine}" + [ -d "$prefix" ] || return 0 + + rm -rf "$prefix/drive_c/VARA" "$prefix/drive_c/VARA FM" "$prefix/drive_c/RMS Express" + rm -f "$HAMPACK_STATE/wine-deps-installed" + + if [ -f "$HAMPACK_STATE/wine-prefix-created" ]; then + read -rp "HamPack created the Wine prefix $prefix. Remove it, with anything else installed in it? [y/N] " answer + if [[ "$answer" =~ ^[yY] ]]; then + rm -rf "$prefix" + return 0 + fi + fi + if command -v wine &> /dev/null; then + wine reg delete 'HKCU\Software\Wine\X11 Driver' /v Decorated /f &> /dev/null + fi + return 0 +} + +# Remove the packages HamPack installed. They're marked as dependencies and +# only those nothing else needs are removed, so removing HamPack can't break +# other software. Any kept now go once nothing needs them. +remove_recorded_packages() { + local recorded=() installed=() orphans=() pkg + + mapfile -t recorded < <(recorded_installs installed-packages) + for pkg in "${recorded[@]}"; do + [ "$(pacman -Qq "$pkg" 2> /dev/null)" = "$pkg" ] && installed+=("$pkg") + done + if [ ${#installed[@]} -eq 0 ]; then + echo "No packages to remove." + return 0 + fi + + sudo pacman -D --asdeps "${installed[@]}" > /dev/null + while :; do + mapfile -t orphans < <(comm -12 <(pacman -Qdtq | sort) <(printf '%s\n' "${installed[@]}" | sort)) + [ ${#orphans[@]} -eq 0 ] && break + echo "Removing packages: ${orphans[*]}" + sudo pacman -Rns --noconfirm "${orphans[@]}" || break + done + + local kept=() + for pkg in "${installed[@]}"; do + pacman -Qq "$pkg" &> /dev/null && kept+=("$pkg") + done + [ ${#kept[@]} -eq 0 ] || + echo "Kept because other software needs them: ${kept[*]}" +} + +remove_recorded_flatpaks() { + local recorded=() + mapfile -t recorded < <(recorded_installs installed-flatpaks) + [ ${#recorded[@]} -eq 0 ] || remove_flatpaks "${recorded[@]}" +} + +# Tell the user about packages and Flatpaks from HamPack's lists that are still +# installed but weren't recorded as installed by HamPack: they were there +# before HamPack, or installed before it kept records, so they're left alone +report_unrecorded() { + local pkg leftover=() leftover_flatpaks=() coredesktop_pkgs + + coredesktop_pkgs="$(sed -nE 's/^[[:space:]]+([a-z0-9@._+-]+).*/\1/p' "$COREDESKTOP/packages.conf" 2> /dev/null)" + for pkg in "${UTILITIES[@]}" "${APPLICATIONS[@]}"; do + [ "$(pacman -Qq "$pkg" 2> /dev/null)" = "$pkg" ] || continue + grep -qxF "$pkg" <<< "$coredesktop_pkgs" && continue + leftover+=("$pkg") + done + for pkg in "${FLATPAKS[@]}"; do + flatpak info "$pkg" &> /dev/null && leftover_flatpaks+=("$pkg") + done + + if [ ${#leftover[@]} -gt 0 ] || [ ${#leftover_flatpaks[@]} -gt 0 ]; then + echo "" + echo "These are on HamPack's list but were left in place, because HamPack has no" + echo "record of installing them (you had them already, or they were installed" + echo "before HamPack kept records). Remove any you don't need yourself:" + [ ${#leftover[@]} -eq 0 ] || echo " sudo pacman -Rns ${leftover[*]}" + [ ${#leftover_flatpaks[@]} -eq 0 ] || echo " sudo flatpak uninstall ${leftover_flatpaks[*]}" + fi +} + # --- Main --- clear print_logo -echo "This will remove all packages and files installed by HamPack." +if ! HAMPACK_DIR="$(find_hampack_dir)"; then + echo "Couldn't find HamPack in ~/.local/share/siblings, so there's nothing to remove." + echo "If it's installed elsewhere, run its hampackremove from there." + exit 1 +fi +export HAMPACK_DIR +source "$HAMPACK_DIR/lib.sh" +source "$COREDESKTOP/utils.sh" +source "$HAMPACK_DIR/packages.conf" + +echo "This will remove HamPack, the applications it installed, and the packages it" +echo "installed that nothing else needs." echo "" read -rp "Are you sure you want to continue? [y/N] " confirm [[ "$confirm" =~ ^[yY] ]] || { echo "Aborted."; exit 0; } echo "" -# Stop and remove graywolf systemd service +start_sudo_keepalive || exit 1 + echo "Removing graywolf systemd service..." -if systemctl is-active --quiet graywolf 2>/dev/null; then - sudo systemctl disable --now graywolf -fi -if [ -f /etc/systemd/system/graywolf.service ]; then - sudo rm -f /etc/systemd/system/graywolf.service - sudo systemctl daemon-reload -fi +remove_graywolf_service -# Remove compiled binaries -echo "Removing compiled binaries..." -rm -f ~/.local/bin/direwolf -rm -f ~/.local/bin/direwolf-* -rm -f ~/.local/bin/QtTermTCP -rm -f ~/.local/bin/kiwix-desktop -rm -f ~/.local/bin/kiwix-lib -rm -f ~/.local/bin/kiwix-serve -rm -f ~/.local/bin/kiwix-search -rm -f ~/.local/bin/kiwix-manage -rm -rf ~/.local/bin/yaac -rm -f ~/.local/bin/POTACAT.AppImage -rm -f ~/.local/bin/JS8Call -rm -f ~/.local/bin/dump1090 -rm -f ~/.local/bin/graywolf -rm -f ~/.local/bin/graywolf-modem -rm -rf ~/.local/share/graywolf -rm -f ~/.local/bin/ardopcf_amd64_Linux_64 -rm -f ~/.local/bin/paracon.pyz ~/.local/bin/paracon_*.pyz +echo "Removing installed applications..." +remove_manifests +parse_conf "$HAMPACK_DIR/binaries.conf" remove_binary_app +parse_conf "$HAMPACK_DIR/compile.conf" remove_compiled_app +remove_unrecorded_build_files +sudo udevadm control --reload-rules -# Remove HamPack scripts and binaries -echo "Removing HamPack scripts and binaries..." -rm -f ~/.local/bin/hampackrefresh -rm -f ~/.local/bin/hampackupdate -rm -f ~/.local/bin/hampackcheck -rm -f ~/.local/bin/hampackremove -rm -f ~/.local/bin/hampack -rm -f ~/.local/bin/dx.py +echo "Removing HamPack scripts..." +rm -f "$BIN"/hampackrefresh "$BIN"/hampackupdate "$BIN"/hampackcheck "$BIN"/hampack -# Uninstall not1mm echo "Uninstalling not1mm..." -if command -v uv &>/dev/null; then - uv tool uninstall not1mm 2>/dev/null || true +if command -v uv &> /dev/null; then + uv tool uninstall not1mm 2> /dev/null fi +rm -f "$APPLICATIONS_DIR/k6gte-not1mm.desktop" "$HOME"/.local/share/icons/hicolor/*/apps/k6gte-not1mm.png -# Remove Flatpak applications -echo "Removing Flatpak applications..." -FLATPAKS=( - "io.sourceforge.wsjt.wsjtx" - "io.github.foldynl.QLog" -) -for pak in "${FLATPAKS[@]}"; do - if flatpak list 2>/dev/null | grep -qi "$pak"; then - flatpak uninstall --noninteractive "$pak" - fi -done - -# Remove system packages -echo "Removing system packages..." -UTILITIES=( - alsa-utils - aria2 - at-spi2-core - base-devel - boost - boost-libs - cmake - docopt - fldigi - flrig - gpds - gpsbabel - gtk4 - hamlib - jdk-openjdk - libappindicator-gtk3 - libkiwix - libtorrent-rasterbar - libnotify - libsecret - libxcrypt-compat - libxtst - libzim - meson - ninja - nss - pat-bin - python-yattag - qgis - qt6-base - qt6-tools - qt6-webengine - splat - util-linux-libs - voacapl - wine - winetricks - xdg-utils - zim-tools - zimwriterfs -) - -APPLICATIONS=( - chattervox-bin - chirp-next - gridtracker2 -) - -PACKAGES_TO_REMOVE=() -for pkg in "${UTILITIES[@]}" "${APPLICATIONS[@]}"; do - if pacman -Qi "$pkg" &>/dev/null; then - PACKAGES_TO_REMOVE+=("$pkg") - fi -done - -if [ ${#PACKAGES_TO_REMOVE[@]} -gt 0 ]; then - echo "Removing packages: ${PACKAGES_TO_REMOVE[*]}" - sudo pacman -Rns --noconfirm "${PACKAGES_TO_REMOVE[@]}" 2>/dev/null || \ - echo "Warning: some packages could not be removed (may be required by other software)." -else - echo "No system packages to remove." -fi - -# Remove Wine applications +# Before packages, while Wine is still installed echo "Removing Wine applications..." -rm -rf "$HOME/.wine/drive_c/VARA" -rm -rf "$HOME/.wine/drive_c/VARA FM" -rm -rf "$HOME/.wine/drive_c/VARA Terminal" -rm -rf "$HOME/.wine/drive_c/RMS Express" +remove_wine_apps + +echo "Removing Flatpak applications..." +remove_recorded_flatpaks + +echo "Removing system packages..." +remove_recorded_packages -# Remove desktop files echo "Removing desktop files..." -rm -f ~/.local/share/applications/js8call.desktop -rm -f ~/.local/share/applications/yaac.desktop -rm -f ~/.local/share/applications/potacat.desktop -rm -f ~/.local/share/applications/qttermtcp.desktop -rm -f ~/.local/share/applications/paracon.desktop -rm -f ~/.local/share/applications/hampack.desktop -rm -f ~/.local/share/applications/k6gte-not1mm.desktop -rm -f ~/.local/share/icons/hicolor/*/apps/k6gte-not1mm.png -rm -f ~/.local/share/icons/hicolor/*/apps/computer.young.HamPack.png +rm -f "$APPLICATIONS_DIR/hampack.desktop" "$HOME"/.local/share/icons/hicolor/*/apps/computer.young.HamPack.png +update-desktop-database "$APPLICATIONS_DIR" 2> /dev/null +update-mime-database "$HOME/.local/share/mime" &> /dev/null -update-desktop-database "$HOME/.local/share/applications/" 2>/dev/null || true +report_unrecorded -# Remove HamPack state and data +echo "" echo "Removing HamPack state and data..." -rm -rf ~/.local/state/HamPack -rm -rf ~/.local/share/HamPack -rm -f ~/.local/share/coredesktop/updates.d/[0-9][0-9][0-9]-hampack-* +rm -rf "$HAMPACK_STATE" "$HOME/.local/share/HamPack" +rm -f "$COREDESKTOP"/updates.d/[0-9][0-9][0-9]-hampack-* +rm -f "$BIN/hampackremove" # Remove the sibling clone, so CoreDesktop's `update` stops updating HamPack -for dir in ~/.local/share/siblings/*/; do - [ -d "$dir/.git" ] || continue - if [[ "$(git -C "$dir" remote get-url origin 2>/dev/null)" =~ /[Hh]am[Pp]ack(\.git)?/?$ ]]; then - rm -rf "$dir" - fi -done +rm -rf "$HAMPACK_DIR" echo "" echo "HamPack has been removed." diff --git a/install-binaries.sh b/install-binaries.sh index 2681a0f..e9d0fd6 100755 --- a/install-binaries.sh +++ b/install-binaries.sh @@ -2,125 +2,72 @@ # # Purpose: Download and install pre-built binaries from binaries.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_BINARIES_CONF:-$HAMPACK_DIR/binaries.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" - - if [ -f "$VERSION_FILE" ]; then - local stored - stored=$(grep "^$app=" "$VERSION_FILE" | cut -d'=' -f2) - if [ -n "$stored" ]; then - echo "$stored" - return - 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 -} - # Check if an update is needed needs_update() { - local app="$1" - local install_path="$2" - local latest="$3" + local app="$1" install_path="$2" latest="$3" installed - if [ ! -f "$install_path" ]; then + if [ ! -e "$install_path" ]; then echo " $app is not installed." return 0 fi - local installed - installed=$(get_installed_version "$app") - - if [ "$installed" = "unknown" ]; then + installed="$(get_recorded_version "$app")" + if [ -z "$installed" ]; then echo " Installed version of $app is unknown, reinstalling 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 } -# Download binary and place it at the install path +# Download a binary, check it against its SHA-256 if one is given, and put it +# at the install path. A failed download or check leaves the old binary alone. download_binary() { - local app="$1" - local url="$2" - local install_path="$3" - - local filename="${url##*/}" - local tmp_file="$TMP_DIR/$filename" + local url="$1" install_path="$2" sha256="$3" + local tmp_file="$HAMPACK_TMP/download" echo " Downloading $url..." - if ! wget -q "$url" -O "$tmp_file"; then + if ! curl -fsSL "$url" -o "$tmp_file"; then echo " Error: download of $url failed." - rm -f "$tmp_file" return 1 fi - mkdir -p "$(dirname "$install_path")" - mv "$tmp_file" "$install_path" - chmod +x "$install_path" - echo " Installed to $install_path" + if [ -n "$sha256" ]; then + if ! echo "$sha256 $tmp_file" | sha256sum -c --status; then + echo " Error: $url doesn't match its sha256 in binaries.conf." + echo " Expected $sha256" + echo " Got $(sha256sum "$tmp_file" | cut -d' ' -f1)" + return 1 + fi + else + echo " Warning: no sha256 in binaries.conf, so the download isn't verified." + fi + + mkdir -p "$(dirname "$install_path")" && + chmod +x "$tmp_file" && + mv -f "$tmp_file" "$install_path" && + echo " Installed to $install_path" } -# Generate and install a .desktop file +# Write the app's .desktop file. Done on every run, not just after a download, +# so paths in it (like icons in $HAMPACK_DIR) stay right if HamPack moves. install_desktop() { - local app="$1" - local install_path="$2" - local exec_cmd="$3" - local comment="$4" - local icon="$5" - local categories="$6" + local app="$1" exec_cmd="$2" comment="$3" icon="$4" categories="$5" - [ -z "$categories" ] && categories="HamRadio;" - [ -z "$exec_cmd" ] && exec_cmd="$install_path" - - local desktop_dir="$HOME/.local/share/applications" - mkdir -p "$desktop_dir" - - local desktop_file="$desktop_dir/$app.desktop" - - echo " Generating desktop file $desktop_file..." - cat > "$desktop_file" < "$APPLICATIONS_DIR/$app.desktop" << EOF [Desktop Entry] Type=Application Name=$app @@ -129,102 +76,39 @@ Comment=$comment Icon=$icon Categories=$categories EOF - - chmod +x "$desktop_file" - echo " Desktop file installed." } -# Process a single app +# Process the app in CONF (called by parse_conf for each section) process_app() { - local app="$1" - local url="$2" - local install_path="$3" - local version="$4" - local gui="$5" - local exec_cmd="$6" - local comment="$7" - local icon="$8" - local categories="$9" + local app="${CONF[_name]}" + local url install_path version="${CONF[version]}" sha256="${CONF[sha256]}" + url="$(expand_vars "${CONF[url]}")" + install_path="$(expand_vars "${CONF[install]}")" echo "" echo "=== $app ===" - if [ -z "$url" ]; then - echo " Error: no url specified for $app, skipping." + if [ -z "$url" ] || [ -z "$install_path" ] || [ -z "$version" ]; then + echo " Error: $app needs url=, install= and version= in binaries.conf, skipping." FAILED+=("$app") return fi - if [ -z "$install_path" ]; then - echo " Error: no install path specified for $app, skipping." - FAILED+=("$app") - return - fi - - if [ -f "$install_path" ]; then - if [ -z "$version" ]; then - echo " $app is already installed, skipping." + if needs_update "$app" "$install_path" "$version"; then + if ! download_binary "$url" "$install_path" "$sha256"; then + FAILED+=("$app") return fi - if ! needs_update "$app" "$install_path" "$version"; then - return - fi - else - echo " $app is not installed." - fi - - if ! download_binary "$app" "$url" "$install_path"; then - FAILED+=("$app") - return - fi - - if [ "$gui" = "true" ]; then - install_desktop "$app" "$install_path" "$exec_cmd" "$comment" "$icon" "$categories" - fi - - if [ -n "$version" ]; then save_installed_version "$app" "$version" + echo " $app $version installed." fi - echo " $app installed successfully." -} - -# Parse binaries.conf and process each app -process_conf() { - local app="" url="" install_path="" version="" gui="" exec_cmd="" comment="" icon="" categories="" - - while IFS= read -r line || [ -n "$line" ]; do - [[ -z "$line" || "$line" == \#* ]] && continue - - if [[ "$line" =~ ^\[(.+)\]$ ]]; then - if [ -n "$app" ]; then - process_app "$app" "$url" "$install_path" "$version" "$gui" "$exec_cmd" "$comment" "$icon" "$categories" - fi - app="${BASH_REMATCH[1]}" - url="" install_path="" version="" gui="" exec_cmd="" comment="" icon="" categories="" - continue - fi - - local key="${line%%=*}" - local value="${line#*=}" - value=$(expand_vars "$value") - - case "$key" in - url) url="$value" ;; - install) install_path="$value" ;; - version) version="$value" ;; - gui) gui="$value" ;; - exec) exec_cmd="$value" ;; - comment) comment="$value" ;; - icon) icon="$value" ;; - categories) categories="$value" ;; - esac - - done < "$CONF_FILE" - - # Process the last app in the file - if [ -n "$app" ]; then - process_app "$app" "$url" "$install_path" "$version" "$gui" "$exec_cmd" "$comment" "$icon" "$categories" + if [ "${CONF[gui]}" = "true" ]; then + install_desktop "$app" \ + "$(expand_vars "${CONF[exec]:-$install_path}")" \ + "$(expand_vars "${CONF[comment]}")" \ + "$(expand_vars "${CONF[icon]}")" \ + "${CONF[categories]:-HamRadio;}" fi } @@ -235,24 +119,17 @@ if [ ! -f "$CONF_FILE" ]; then exit 1 fi -mkdir -p "$TMP_DIR" -mkdir -p "$HOME/.local/state/HamPack" +make_work_dir || exit 1 mkdir -p "$HOME/.local/bin" echo "Starting HamPack binary installs..." -process_conf +parse_conf "$CONF_FILE" process_app -echo "" -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." diff --git a/install-compiled.sh b/install-compiled.sh index bd675b5..a5ada8f 100755 --- a/install-compiled.sh +++ b/install-compiled.sh @@ -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." \ No newline at end of file diff --git a/install-flatpaks.sh b/install-flatpaks.sh index 76f9a0a..7a34da9 100755 --- a/install-flatpaks.sh +++ b/install-flatpaks.sh @@ -1,13 +1,27 @@ -FLATPAKS=( -"io.sourceforge.wsjt.wsjtx" -"io.github.foldynl.QLog" -) +#!/bin/bash +# +# Purpose : Install the Flatpaks listed in packages.conf +HAMPACK_DIR="${HAMPACK_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}" +source "$HAMPACK_DIR/lib.sh" +source "$HOME/.local/share/coredesktop/utils.sh" +source "$HAMPACK_DIR/packages.conf" + +# Leave out those listed in ~/.config/coredesktop/skip-packages +mapfile -t FLATPAKS < <(without_skipped "${FLATPAKS[@]}") + +status=0 for pak in "${FLATPAKS[@]}"; do - if ! flatpak list | grep -i "$pak" &> /dev/null; then - echo "Installing Flatpak: $pak" - flatpak install --noninteractive "$pak" - else + if flatpak info "$pak" &> /dev/null; then echo "Flatpak already installed: $pak" + else + echo "Installing Flatpak: $pak" + if sudo flatpak install --system --noninteractive flathub "$pak"; then + record_installed installed-flatpaks "$pak" + else + echo "Error: could not install $pak." + status=1 + fi fi done +exit $status diff --git a/install-windows-apps.sh b/install-windows-apps.sh index 088e06a..18a549c 100755 --- a/install-windows-apps.sh +++ b/install-windows-apps.sh @@ -1,271 +1,128 @@ #!/bin/bash # -# Purpose : Download latest VARA HF, FM, Terminal and Winlink Express from winlink.org +# Purpose : Install VARA HF, VARA FM, VARA Terminal and Winlink Express from +# winlink.org under Wine. Products already installed are left alone, +# so on most updates this downloads nothing and opens no windows. + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib.sh" BASE_URL="https://downloads.winlink.org" -DOWNLOAD_DIR="/tmp/windowsapps" +WINE_C="${WINEPREFIX:-$HOME/.wine}/drive_c" -mkdir -p "$DOWNLOAD_DIR" +# Each product: name|program that shows it's installed|downloads page|zip name pattern|installer pattern +PRODUCTS=( + "VARA HF|$WINE_C/VARA/VARA.exe|VARA%20Products|VARA%20HF|VARA*.exe" + "VARA FM|$WINE_C/VARA FM/VARAFM.exe|VARA%20Products|VARA%20FM|VARA*.exe" + "VARA Terminal|$WINE_C/VARA/VARA Terminal.exe|VARA%20Products|VARA%20Terminal|VARA*.exe" + "Winlink Express|$WINE_C/RMS Express/RMS Express.exe|User%20Programs|Winlink_Express_install|Winlink_Express_install*.exe" +) -echo "Fetching VARA Products page..." -PAGE=$(curl -s "$BASE_URL/VARA%20Products/") +# Download a product's zip from its winlink.org page and extract it to DIR +download_product() { + local name="$1" page="$2" pattern="$3" dir="$4" + local listing href filename -if [ -z "$PAGE" ]; then - echo "Error: could not fetch VARA Products page." - exit 1 -fi - -# Download and extract a VARA product by pattern -download_vara() { - local pattern="$1" - - # Match against URL encoded pattern - local encoded_pattern=$(echo "$pattern" | sed 's| |%20|g') - ENCODED=$(echo "$PAGE" | grep -oP 'HREF="\K[^"]+' | grep -i "$encoded_pattern") - - if [ -z "$ENCODED" ]; then - echo "Error: could not find $pattern on page." + listing="$(curl -fsSL "$BASE_URL/$page/")" || { echo "Error: could not fetch $BASE_URL/$page/"; return 1; } + href="$(grep -oP 'HREF="\K[^"]+' <<< "$listing" | grep -i "$pattern" | grep -i '\.zip$' | head -n1)" + if [ -z "$href" ]; then + echo "Error: could not find $name on $BASE_URL/$page/" return 1 fi - # Decode %20 to spaces for local filename - FILENAME=$(echo "$ENCODED" | sed 's|.*/||; s|%20| |g') - - # Extract version number - VERSION=$(echo "$FILENAME" | grep -oP '\d+\.\d+[\.\d]*') - - if [ -z "$VERSION" ]; then - echo "Error: could not determine version from filename: $FILENAME" - return 1 - fi - - echo "Found $pattern version: $VERSION" - echo "Filename: $FILENAME" - - local DOWNLOAD_URL="$BASE_URL$ENCODED" - local FILEPATH="$DOWNLOAD_DIR/$FILENAME" - local EXTRACT_DIR="$DOWNLOAD_DIR/$(echo "$pattern" | sed 's| |_|g')" - - echo "Downloading $FILENAME..." - wget -q "$DOWNLOAD_URL" -O "$FILEPATH" - - if [ $? -ne 0 ]; then - echo "Error: download of $FILENAME failed." - return 1 - fi - - echo "Extracting to $EXTRACT_DIR..." - rm -rf "$EXTRACT_DIR" - mkdir -p "$EXTRACT_DIR" - unzip -q "$FILEPATH" -d "$EXTRACT_DIR" - - if [ $? -eq 0 ]; then - echo "Extracted $pattern version $VERSION to $EXTRACT_DIR" - rm -f "$FILEPATH" - else - echo "Error: extraction of $FILENAME failed." - return 1 - fi -} - -# Download and extract Winlink Express -download_winlink() { - local page - page=$(curl -s "https://downloads.winlink.org/User%20Programs/") - - if [ -z "$page" ]; then - echo "Error: could not fetch Winlink User Programs page." - return 1 - fi - - local encoded - encoded=$(echo "$page" | grep -oP 'HREF="\K[^"]+' | grep -i "Winlink_Express_install") - - if [ -z "$encoded" ]; then - echo "Error: could not find Winlink Express install zip on page." - return 1 - fi - - local filename - filename=$(echo "$encoded" | sed 's|.*/||; s|%20| |g') - - local version - version=$(echo "$filename" | grep -oP '\d+[-\.\d]+' | head -n1) - - echo "Found Winlink Express version: $version" - echo "Filename: $filename" - - local download_url="https://downloads.winlink.org$encoded" - local filepath="$DOWNLOAD_DIR/$filename" - local extract_dir="$DOWNLOAD_DIR/Winlink_Express" - + filename="$(sed 's|.*/||; s|%20| |g' <<< "$href")" echo "Downloading $filename..." - wget -q "$download_url" -O "$filepath" + curl -fsSL "$BASE_URL$href" -o "$HAMPACK_TMP/download.zip" || { echo "Error: download of $filename failed."; return 1; } - if [ $? -ne 0 ]; then - echo "Error: download of $filename failed." - return 1 - fi - - echo "Extracting to $extract_dir..." - rm -rf "$extract_dir" - mkdir -p "$extract_dir" - unzip -q "$filepath" -d "$extract_dir" - - if [ $? -eq 0 ]; then - echo "Extracted Winlink Express version $version to $extract_dir" - rm -f "$filepath" - else - echo "Error: extraction of $filename failed." - return 1 - fi + rm -rf "$dir" + mkdir -p "$dir" + unzip -q "$HAMPACK_TMP/download.zip" -d "$dir" || { echo "Error: extraction of $filename failed."; return 1; } + rm -f "$HAMPACK_TMP/download.zip" } # Wait for a Wine window to appear and raise it to the front raise_wine_window() { - for i in $(seq 1 20); do + local _ + for _ in $(seq 1 20); do sleep 1 - xdotool search --class Wine windowfocus windowraise 2>/dev/null && return 0 + xdotool search --class Wine windowfocus windowraise 2> /dev/null && return 0 done } -# Install a VARA product using Wine -install_vara() { - local pattern="$1" - local wine_dir="$2" - local extract_dir="$DOWNLOAD_DIR/$(echo "$pattern" | sed 's| |_|g')" - - # Check if already installed - if [ -d "$wine_dir" ]; then - echo " $pattern is already installed, skipping." - return 0 - fi - - if [ ! -d "$extract_dir" ]; then - echo "Error: directory $extract_dir not found, skipping." - return 1 - fi - - # Find the VARA exe file in the extracted directory - local exe - exe=$(find "$extract_dir" -maxdepth 1 -iname "VARA*.exe" | head -n1) +# Run a product's installer and wait for the user to finish it +run_installer() { + local name="$1" dir="$2" glob="$3" exe + exe="$(find "$dir" -maxdepth 1 -iname "$glob" | head -n1)" if [ -z "$exe" ]; then - echo "Error: no VARA*.exe found in $extract_dir, skipping." + echo "Error: no $glob found for $name." return 1 fi - local exe_name=$(basename "$exe") echo "" - echo "=== Installing $exe_name ===" + echo "=== Installing $name ===" echo "Please complete the installation in the Wine window." - echo "The script will continue once the installer is closed." + echo "This will continue once the installer is closed." echo "" wine "$exe" & local wine_pid=$! raise_wine_window & wait "$wine_pid" - - echo "$exe_name installation complete." } -# Install Winlink Express using Wine -install_winlink() { - local wine_dir="$HOME/.wine/drive_c/RMS Express" - local extract_dir="$DOWNLOAD_DIR/Winlink_Express" - - # Check if already installed - if [ -d "$wine_dir" ]; then - echo " Winlink Express is already installed, skipping." - return 0 +# Create the Wine prefix and install the runtimes VARA and Winlink need +prepare_wine() { + if [ ! -d "${WINEPREFIX:-$HOME/.wine}" ]; then + echo "Initializing Wine..." + wineboot --init || return 1 + # hampackremove offers to remove a prefix HamPack made + touch "$HAMPACK_STATE/wine-prefix-created" fi - if [ ! -d "$extract_dir" ]; then - echo "Error: directory $extract_dir not found, skipping." - return 1 + if [ ! -f "$HAMPACK_STATE/wine-deps-installed" ]; then + echo "Installing Wine dependencies..." + winetricks -q vb6run pdh_nt4 sound=alsa || { echo "Error: winetricks failed."; return 1; } + touch "$HAMPACK_STATE/wine-deps-installed" fi - local exe - exe=$(find "$extract_dir" -maxdepth 1 -iname "Winlink_Express_install*.exe" | head -n1) - - if [ -z "$exe" ]; then - echo "Error: no Winlink Express exe found in $extract_dir, skipping." - return 1 - fi - - local exe_name=$(basename "$exe") - echo "" - echo "=== Installing $exe_name ===" - echo "Please complete the installation in the Wine window." - echo "The script will continue once the installer is closed." - echo "" - - wine "$exe" & - local wine_pid=$! - raise_wine_window & - wait "$wine_pid" - - echo "$exe_name installation complete." + # No window decorations: VARA and Winlink draw their own + wine reg add 'HKCU\Software\Wine\X11 Driver' /v Decorated /t REG_SZ /d N /f > /dev/null 2>&1 } -# Download all products -download_vara "VARA HF" -download_vara "VARA FM" -download_vara "VARA Terminal" -download_winlink +# --- Main --- -# Initialize Wine if not already done -if [ ! -d "$HOME/.wine" ]; then - echo "Initializing Wine..." - wineboot --init - echo "Wine initialized." -fi - -# Update Wine registry to disable window decorations -USER_REG="$HOME/.wine/user.reg" - -if [ ! -f "$USER_REG" ]; then - echo "Warning: $USER_REG not found, skipping registry update." -else - echo "Updating Wine registry..." - if grep -q '"Decorated"="Y"' "$USER_REG"; then - sed -i 's/"Decorated"="Y"/"Decorated"="N"/' "$USER_REG" - elif grep -q '"Decorated"' "$USER_REG"; then - sed -i 's/"Decorated"=.*/"Decorated"="N"/' "$USER_REG" +missing=() +for product in "${PRODUCTS[@]}"; do + IFS='|' read -r name check _ <<< "$product" + if [ -e "$check" ]; then + echo "$name is already installed." else - echo '"Decorated"="N"' >> "$USER_REG" + missing+=("$product") fi - echo "Set Decorated=N in $USER_REG" +done + +[ ${#missing[@]} -eq 0 ] && exit 0 + +# The installers are Windows GUI programs; without a desktop there's nothing to show them on +if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then + echo "Not installing Windows applications without a desktop session; they'll be installed on a later update." + exit 0 fi -echo "" -echo "All products downloaded and extracted to $DOWNLOAD_DIR" +mkdir -p "$HAMPACK_STATE" +make_work_dir || exit 1 +prepare_wine || exit 1 -# Install Wine dependencies -echo "" -echo "Installing Wine dependencies..." -winetricks -q vb6run pdh_nt4 sound=alsa +status=0 +for product in "${missing[@]}"; do + IFS='|' read -r name check page pattern glob <<< "$product" + dir="$HAMPACK_TMP/${name// /_}" + if download_product "$name" "$page" "$pattern" "$dir" && run_installer "$name" "$dir" "$glob" && [ -e "$check" ]; then + echo "$name installed." + else + echo "$name was not installed; it will be tried again on the next update." + status=1 + fi +done -if [ $? -ne 0 ]; then - echo "Error: winetricks failed, aborting." - exit 1 -fi - -echo "Wine dependencies installed successfully." - -# Install all products -echo "" -echo "Starting installations..." - -install_vara "VARA HF" "$HOME/.wine/drive_c/VARA" -install_vara "VARA FM" "$HOME/.wine/drive_c/VARA FM" -install_vara "VARA Terminal" "$HOME/.wine/drive_c/VARA" -install_winlink - -echo "" -echo "Updating desktop database..." -update-desktop-database "$HOME/.local/share/applications/" - -echo "" -echo "All installations complete." \ No newline at end of file +exit $status diff --git a/install.sh b/install.sh index 7fb8c36..c3320ad 100755 --- a/install.sh +++ b/install.sh @@ -46,11 +46,11 @@ if [ -d "$LEGACY_DIR/.git" ] && [ "$LEGACY_DIR" != "$SELF" ]; then fi rm -f "$COREDESKTOP"/updates.d/[0-9][0-9][0-9]-hampack-* -echo "Setting up sudo..." -# Remove the temporary sudoers file however this script exits -trap 'sudo rm -f /etc/sudoers.d/hampack' EXIT -bash "$SELF/sudo.sh" - -bash "$SELF/update.sh" +# update.sh asks for the sudo password once and keeps it fresh while it runs +if ! bash "$SELF/update.sh"; then + echo "HamPack is installed, but some parts failed (listed above)." + echo "They'll be tried again on the next update, or run: bash $SELF/update.sh" + exit 1 +fi echo "HamPack is installed. You may want to restart existing applications when convenient." diff --git a/lib.sh b/lib.sh new file mode 100644 index 0000000..c535424 --- /dev/null +++ b/lib.sh @@ -0,0 +1,129 @@ +#!/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 +} diff --git a/packages.conf b/packages.conf index bbc2154..887a515 100644 --- a/packages.conf +++ b/packages.conf @@ -1,3 +1,6 @@ +# Packages and Flatpaks HamPack installs. update.sh installs them, and records +# which ones weren't there before so hampackremove removes only those. + # Ham utilities UTILITIES=( alsa-utils @@ -15,6 +18,7 @@ UTILITIES=( gtk4 hamlib jdk-openjdk + jq # patch-potacat.sh libappindicator-gtk3 libkiwix libtorrent-rasterbar @@ -37,6 +41,7 @@ UTILITIES=( qt6-webengine splat util-linux-libs + uv # installs not1mm voacapl wine winetricks @@ -53,3 +58,8 @@ APPLICATIONS=( gridtracker2 ) +# Flatpaks, installed system-wide from Flathub +FLATPAKS=( + io.sourceforge.wsjt.wsjtx + io.github.foldynl.QLog +) diff --git a/patch-potacat.sh b/patch-potacat.sh index 735f275..bf8f834 100755 --- a/patch-potacat.sh +++ b/patch-potacat.sh @@ -1,6 +1,7 @@ #!/bin/bash # -# Purpose : Remove deb target from electron-builder package.json +# Purpose : Remove the deb and rpm targets from POTACAT's electron-builder +# package.json, so `npm run dist:linux` builds only the AppImage PACKAGE_JSON="${1:-package.json}" @@ -9,25 +10,18 @@ if [ ! -f "$PACKAGE_JSON" ]; then exit 1 fi -python3 -c " -import json, sys -with open('$PACKAGE_JSON', 'r') as f: - data = json.load(f) -targets = data.get('build', {}).get('linux', {}).get('target', []) -if 'deb' in targets: - targets.remove('deb') - data['build']['linux']['target'] = targets - with open('$PACKAGE_JSON', 'w') as f: - json.dump(data, f, indent=2) - print('Removed deb target from $PACKAGE_JSON.') -else: - print('No deb target found, nothing to do.') -if 'rpm' in targets: - targets.remove('rpm') - data['build']['linux']['target'] = targets - with open('$PACKAGE_JSON', 'w') as f: - json.dump(data, f, indent=2) - print('Removed rpm target from $PACKAGE_JSON.') -else: - print('No rpm target found, nothing to do.') -" \ No newline at end of file +# Targets are either names ("deb") or objects ({"target": "deb", ...}) +tmp="$(mktemp "$PACKAGE_JSON.XXXXXX")" || exit 1 +if jq ' + if .build.linux.target then + .build.linux.target |= map(select( + (if type == "object" then .target else . end) as $name + | $name != "deb" and $name != "rpm")) + else . end' "$PACKAGE_JSON" > "$tmp"; then + mv -f "$tmp" "$PACKAGE_JSON" + echo "Linux targets: $(jq -c '.build.linux.target' "$PACKAGE_JSON")" +else + rm -f "$tmp" + echo "Error: could not patch $PACKAGE_JSON." + exit 1 +fi diff --git a/sudo.sh b/sudo.sh deleted file mode 100755 index 3155285..0000000 --- a/sudo.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -# -# Purpose : Set up passwordless sudo for HamPack installer - -SUDOERS_FILE="/etc/sudoers.d/hampack" -USERNAME=$(whoami) - -echo "Setting up passwordless sudo for HamPack..." -echo "Please enter your sudo password once to continue:" -sudo -v - -if [ $? -ne 0 ]; then - echo "Error: incorrect password or sudo not available." - exit 1 -fi - -sudo tee "$SUDOERS_FILE" > /dev/null << EOF -$USERNAME ALL=(ALL) NOPASSWD: /usr/bin/pacman, /usr/bin/cp, /usr/bin/udevadm, /usr/bin/systemctl, /usr/sbin/reboot, /usr/sbin/usermod, /usr/bin/make, /usr/bin/chown, /usr/sbin/groupadd, /usr/bin/install, /usr/bin/rm -EOF - -# Verify the file is valid -sudo visudo -c -f "$SUDOERS_FILE" - -if [ $? -eq 0 ]; then - echo "Sudoers file created successfully for $USERNAME." - echo "Passwordless sudo is now configured for the HamPack installer." -else - echo "Error: sudoers file is invalid, removing..." - sudo rm -f "$SUDOERS_FILE" - exit 1 -fi \ No newline at end of file diff --git a/systemd/graywolf.service b/systemd/graywolf.service index 7f29a0f..a59465c 100644 --- a/systemd/graywolf.service +++ b/systemd/graywolf.service @@ -4,10 +4,13 @@ Documentation=https://github.com/chrissnell/graywolf After=network-online.target Wants=network-online.target +# The web UI listens only on this machine (http://localhost:8080). To reach it +# from other machines, override ExecStart with `sudo systemctl edit graywolf` +# and use -http 0.0.0.0:8080; HamPack reinstalls this file on updates. [Service] Type=simple User=HAMPACK_USER -ExecStart=HAMPACK_HOME/.local/bin/graywolf -config HAMPACK_HOME/.local/share/graywolf/graywolf.db -history-db HAMPACK_HOME/.local/share/graywolf/graywolf-history.db -tile-cache-dir HAMPACK_HOME/.local/share/graywolf/tiles -modem HAMPACK_HOME/.local/bin/graywolf-modem -http 0.0.0.0:8080 +ExecStart=HAMPACK_HOME/.local/bin/graywolf -config HAMPACK_HOME/.local/share/graywolf/graywolf.db -history-db HAMPACK_HOME/.local/share/graywolf/graywolf-history.db -tile-cache-dir HAMPACK_HOME/.local/share/graywolf/tiles -modem HAMPACK_HOME/.local/bin/graywolf-modem -http 127.0.0.1:8080 Restart=on-failure RestartSec=5 diff --git a/tests/hampackremove.bats b/tests/hampackremove.bats new file mode 100644 index 0000000..e77b193 --- /dev/null +++ b/tests/hampackremove.bats @@ -0,0 +1,93 @@ +#!/usr/bin/env bats + +load helpers + +setup() { + setup_fake_home + fake_coredesktop + stub uv 'exit 0' + stub clear 'exit 0' + # hampackremove finds HamPack by the sibling clone's origin, and deletes it + SIBLING="$HOME/.local/share/siblings/HamPack" + mkdir -p "$SIBLING" + cp "$REPO"/{lib.sh,packages.conf,binaries.conf,compile.conf,hampackremove} "$SIBLING"/ + git init -q "$SIBLING" + git -C "$SIBLING" remote add origin https://gitea.example/david/HamPack.git +} + +remove() { + run bash "$REPO/hampackremove" <<< "${1:-y}" +} + +@test "removes only the packages HamPack recorded, and keeps ones others need" { + mark_installed hamlib fldigi wine cmake + mkdir -p "$HAMPACK_STATE" + printf 'fldigi\nhamlib\n' > "$HAMPACK_STATE/installed-packages" + echo hamlib > "$FAKE_PACMAN_DB/.required" # something else needs hamlib + + remove + [ "$status" -eq 0 ] + [ ! -e "$FAKE_PACMAN_DB/fldigi" ] + [ -e "$FAKE_PACMAN_DB/hamlib" ] + [ -e "$FAKE_PACMAN_DB/wine" ] + [ -e "$FAKE_PACMAN_DB/cmake" ] + [[ "$output" == *"Kept because other software needs them: hamlib"* ]] + [[ "$output" == *"sudo pacman -Rns"*"wine"* ]] +} + +@test "removes recorded Flatpaks only" { + touch "$FAKE_FLATPAKS/io.github.foldynl.QLog" "$FAKE_FLATPAKS/io.sourceforge.wsjt.wsjtx" + mkdir -p "$HAMPACK_STATE" + echo io.github.foldynl.QLog > "$HAMPACK_STATE/installed-flatpaks" + remove + [ ! -e "$FAKE_FLATPAKS/io.github.foldynl.QLog" ] + [ -e "$FAKE_FLATPAKS/io.sourceforge.wsjt.wsjtx" ] +} + +@test "removes files from manifests, conf install paths and older builds" { + mkdir -p "$HAMPACK_STATE/manifests" "$HOME/.local/share/applications" "$HOME/.local/share/doc/direwolf" + touch "$HOME/.local/bin/staged-file" "$HOME/.local/bin/paracon.pyz" "$HOME/.local/bin/QtTermTCP" \ + "$HOME/.local/bin/decode_aprs" "$HOME/.local/share/applications/qttermtcp.desktop" + printf '%s\n/etc/udev/rules.d/99-x.rules\n' "$HOME/.local/bin/staged-file" > "$HAMPACK_STATE/manifests/x" + + remove + [ "$status" -eq 0 ] + [ ! -e "$HOME/.local/bin/staged-file" ] + grep -qx "sudo rm -f /etc/udev/rules.d/99-x.rules" "$STUB_LOG" + [ ! -e "$HOME/.local/bin/paracon.pyz" ] + [ ! -e "$HOME/.local/bin/QtTermTCP" ] + [ ! -e "$HOME/.local/share/applications/qttermtcp.desktop" ] + [ ! -e "$HOME/.local/bin/decode_aprs" ] + [ ! -e "$HOME/.local/share/doc/direwolf" ] +} + +@test "removes Wine apps but keeps a Wine prefix HamPack didn't create" { + mkdir -p "$HOME/.wine/drive_c/VARA" "$HOME/.wine/drive_c/Other" + remove + [ ! -e "$HOME/.wine/drive_c/VARA" ] + [ -d "$HOME/.wine/drive_c/Other" ] +} + +@test "offers to remove a Wine prefix HamPack created" { + mkdir -p "$HOME/.wine/drive_c" "$HAMPACK_STATE" + touch "$HAMPACK_STATE/wine-prefix-created" + run bash "$REPO/hampackremove" <<< $'y\ny' + [ ! -e "$HOME/.wine" ] +} + +@test "removes graywolf's service, its state and the sibling clone" { + mkdir -p "$HAMPACK_STATE" + remove + [ "$status" -eq 0 ] + [ ! -e "$HAMPACK_STATE" ] + [ ! -e "$SIBLING" ] +} + +@test "does nothing unless confirmed" { + mark_installed fldigi + mkdir -p "$HAMPACK_STATE" + echo fldigi > "$HAMPACK_STATE/installed-packages" + remove n + [ -e "$FAKE_PACMAN_DB/fldigi" ] + [ -d "$SIBLING" ] +} diff --git a/tests/helpers.bash b/tests/helpers.bash new file mode 100644 index 0000000..ebfe322 --- /dev/null +++ b/tests/helpers.bash @@ -0,0 +1,108 @@ +# Shared setup for HamPack's bats tests. Each test gets a fake $HOME, and +# commands that would touch the system (sudo, pacman, flatpak, systemctl...) are +# replaced by stubs that log their arguments to $STUB_LOG. + +REPO="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." && pwd)" + +setup_fake_home() { + export HOME="$BATS_TEST_TMPDIR/home" + export USER="${USER:-tester}" + export TMPDIR="$BATS_TEST_TMPDIR/tmp" + export HAMPACK_DIR="$REPO" + export HAMPACK_STATE="$HOME/.local/state/HamPack" + export STUB_DIR="$BATS_TEST_TMPDIR/stubs" + export STUB_LOG="$BATS_TEST_TMPDIR/stub.log" + # Fake pacman database: one file per installed package; a file containing + # "dep" means it was installed as a dependency + export FAKE_PACMAN_DB="$BATS_TEST_TMPDIR/pacman-db" + export FAKE_FLATPAKS="$BATS_TEST_TMPDIR/flatpaks" + unset DISPLAY WAYLAND_DISPLAY WINEPREFIX + mkdir -p "$HOME/.local/bin" "$TMPDIR" "$STUB_DIR" "$FAKE_PACMAN_DB" "$FAKE_FLATPAKS" + : > "$STUB_LOG" + PATH="$STUB_DIR:$PATH" + + # Commands that just log and succeed + local cmd + for cmd in systemctl udevadm groupadd yay update-desktop-database update-mime-database \ + xdg-icon-resource xdg-desktop-menu wine wineboot winetricks xdotool; do + stub "$cmd" 'exit 0' + done + + # sudo runs stubbed commands and only logs the rest, so tests never change the system + stub sudo ' + case "$1" in -v|-n) exit 0 ;; esac + if [ -x "$STUB_DIR/$1" ]; then "$@"; else exit 0; fi' + + stub pacman ' + case "$1" in + -Qi|-Q|-Qq) + shift; status=0 + for p in "$@"; do + if [ -f "$FAKE_PACMAN_DB/$p" ]; then [ "$1" = -Qi ] || echo "$p"; else status=1; fi + done + exit $status ;; + -Qg) exit 1 ;; + -Qdq) + shift; for p in "$@"; do grep -qx dep "$FAKE_PACMAN_DB/$p" 2>/dev/null && echo "$p"; done; exit 0 ;; + -Qdtq) + for f in "$FAKE_PACMAN_DB"/*; do + [ -f "$f" ] || continue + p="$(basename "$f")" + grep -qx dep "$f" && ! grep -qx "$p" "$FAKE_PACMAN_DB"/.required 2>/dev/null && echo "$p" + done; exit 0 ;; + -D) + mode="$2"; shift 2 + for p in "$@"; do + if [ "$mode" = --asdeps ]; then echo dep > "$FAKE_PACMAN_DB/$p"; else : > "$FAKE_PACMAN_DB/$p"; fi + done ;; + -Rns) + shift; for p in "$@"; do [ "$p" = --noconfirm ] || rm -f "$FAKE_PACMAN_DB/$p"; done ;; + esac + exit 0' + + stub flatpak ' + case "$1" in + info) [ -f "$FAKE_FLATPAKS/$2" ] ;; + install) for p in "$@"; do :; done; touch "$FAKE_FLATPAKS/$p" ;; + uninstall) for p in "$@"; do rm -f "$FAKE_FLATPAKS/$p"; done ;; + esac' +} + +# Create a stub command: stub NAME BODY. Every call is logged as "NAME ARGS". +stub() { + printf '#!/bin/bash\necho "%s $*" >> "$STUB_LOG"\n%s\n' "$1" "$2" > "$STUB_DIR/$1" + chmod +x "$STUB_DIR/$1" +} + +# A minimal CoreDesktop: the utils.sh functions HamPack uses, backed by the fake pacman +fake_coredesktop() { + local cd="$HOME/.local/share/coredesktop" + mkdir -p "$cd" + printf 'SYSTEM_UTILS=(\n coredesktop-pkg\n)\n' > "$cd/packages.conf" + cat > "$cd/utils.sh" << 'EOF' +is_installed() { [ -f "$FAKE_PACMAN_DB/$1" ]; } +without_skipped() { printf '%s\n' "$@"; } +install_packages() { + local p + for p in "$@"; do + [ -f "$FAKE_PACMAN_DB/$p" ] || { echo "yay -S $p" >> "$STUB_LOG"; : > "$FAKE_PACMAN_DB/$p"; } + done +} +enable_services() { echo "enable_services $*" >> "$STUB_LOG"; } +install_script() { cp "$1" "$2" && chmod 755 "$2"; } +remove_flatpaks() { local p; for p in "$@"; do echo "remove_flatpaks $p" >> "$STUB_LOG"; rm -f "$FAKE_FLATPAKS/$p"; done; } +EOF +} + +mark_installed() { + local p + for p in "$@"; do : > "$FAKE_PACMAN_DB/$p"; done +} + +# All packages and Flatpaks in packages.conf +all_hampack_packages() { + (source "$REPO/packages.conf"; printf '%s\n' "${UTILITIES[@]}" "${APPLICATIONS[@]}") +} +all_hampack_flatpaks() { + (source "$REPO/packages.conf"; printf '%s\n' "${FLATPAKS[@]}") +} diff --git a/tests/install-binaries.bats b/tests/install-binaries.bats new file mode 100644 index 0000000..68216ce --- /dev/null +++ b/tests/install-binaries.bats @@ -0,0 +1,88 @@ +#!/usr/bin/env bats + +load helpers + +setup() { + setup_fake_home + SRC="$BATS_TEST_TMPDIR/src" + mkdir -p "$SRC" + echo "v1" > "$SRC/tool" + SHA="$(sha256sum "$SRC/tool" | cut -d' ' -f1)" + export HAMPACK_BINARIES_CONF="$BATS_TEST_TMPDIR/binaries.conf" +} + +write_conf() { + cat > "$HAMPACK_BINARIES_CONF" << EOF +[tool] +version=$1 +url=file://$SRC/tool +sha256=$2 +install=\$HOME/.local/bin/tool +gui=true +comment=A test tool +icon=\$HAMPACK_DIR/desktop/icons/paracon.png +EOF +} + +@test "first install downloads, verifies, records the version and writes the desktop file" { + write_conf 1.0 "$SHA" + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 0 ] + [ -x "$HOME/.local/bin/tool" ] + [ "$(cat "$HAMPACK_STATE/.installed_versions")" = "tool=1.0" ] + grep -qx "Categories=HamRadio;" "$HOME/.local/share/applications/tool.desktop" + grep -qx "Icon=$REPO/desktop/icons/paracon.png" "$HOME/.local/share/applications/tool.desktop" +} + +@test "a download that doesn't match its sha256 isn't installed" { + write_conf 1.0 0000000000000000000000000000000000000000000000000000000000000000 + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 1 ] + [[ "$output" == *"doesn't match its sha256"* ]] + [ ! -e "$HOME/.local/bin/tool" ] + [ -z "$(grep tool= "$HAMPACK_STATE/.installed_versions" 2> /dev/null)" ] +} + +@test "a failed update keeps the installed binary" { + write_conf 1.0 "$SHA" + bash "$REPO/install-binaries.sh" + write_conf 2.0 0000000000000000000000000000000000000000000000000000000000000000 + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 1 ] + [ "$(cat "$HOME/.local/bin/tool")" = "v1" ] + grep -qx "tool=1.0" "$HAMPACK_STATE/.installed_versions" +} + +@test "update with nothing new downloads nothing but refreshes the desktop file" { + write_conf 1.0 "$SHA" + bash "$REPO/install-binaries.sh" + rm "$SRC/tool" "$HOME/.local/share/applications/tool.desktop" + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"up to date"* ]] + [ -f "$HOME/.local/share/applications/tool.desktop" ] +} + +@test "update with a new version downloads it" { + write_conf 1.0 "$SHA" + bash "$REPO/install-binaries.sh" + echo "v2" > "$SRC/tool" + write_conf 2.0 "$(sha256sum "$SRC/tool" | cut -d' ' -f1)" + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 0 ] + [ "$(cat "$HOME/.local/bin/tool")" = "v2" ] + grep -qx "tool=2.0" "$HAMPACK_STATE/.installed_versions" +} + +@test "an installed binary with no recorded version is reinstalled" { + write_conf 1.0 "$SHA" + echo stale > "$HOME/.local/bin/tool" + run bash "$REPO/install-binaries.sh" + [ "$status" -eq 0 ] + [ "$(cat "$HOME/.local/bin/tool")" = "v1" ] +} + +@test "binaries.conf entries all have a sha256" { + run grep -c '^sha256=' "$REPO/binaries.conf" + [ "$output" -eq "$(grep -c '^\[' "$REPO/binaries.conf")" ] +} diff --git a/tests/install-compiled.bats b/tests/install-compiled.bats new file mode 100644 index 0000000..04efe79 --- /dev/null +++ b/tests/install-compiled.bats @@ -0,0 +1,127 @@ +#!/usr/bin/env bats + +load helpers + +setup() { + setup_fake_home + export HAMPACK_COMPILE_CONF="$BATS_TEST_TMPDIR/compile.conf" + REPO_URL="file://$BATS_TEST_TMPDIR/upstream" + git init -q "$BATS_TEST_TMPDIR/upstream" + git -C "$BATS_TEST_TMPDIR/upstream" -c user.email=t@t -c user.name=t commit -q --allow-empty -m init +} + +@test "first install builds each app and records its version" { + cat > "$HAMPACK_COMPILE_CONF" << EOF +[app] +version=1.0 +git=$REPO_URL +install=\$HOME/.local/bin/app +gui=true +desktop=\$HAMPACK_DIR/desktop/yaac.desktop +steps=mkdir sub, cd sub, touch \$HOME/.local/bin/app +EOF + run bash "$REPO/install-compiled.sh" + [ "$status" -eq 0 ] + [ -f "$HOME/.local/bin/app" ] + grep -qx "app=1.0" "$HAMPACK_STATE/.installed_versions" + grep -q "Icon=$REPO/desktop/icons/yaaclogo.png" "$HOME/.local/share/applications/yaac.desktop" +} + +@test "a failing step stops that build, records nothing, and later apps still build" { + cat > "$HAMPACK_COMPILE_CONF" << EOF +[bad] +version=1.0 +git=$REPO_URL +install=\$HOME/.local/bin/bad +steps=true, false, touch \$HOME/.local/bin/bad + +[good] +version=2.0 +git=$REPO_URL +install=\$HOME/.local/bin/good +steps=touch \$HOME/.local/bin/good +EOF + run bash "$REPO/install-compiled.sh" + [ "$status" -eq 1 ] + [[ "$output" == *"step failed: false"* ]] + [[ "$output" == *"Failed to install: bad"* ]] + [ ! -e "$HOME/.local/bin/bad" ] + [ -f "$HOME/.local/bin/good" ] + ! grep -q "^bad=" "$HAMPACK_STATE/.installed_versions" +} + +@test "update skips up-to-date apps without fetching, but refreshes desktop files" { + cat > "$HAMPACK_COMPILE_CONF" << EOF +[app] +version=1.0 +git=file:///nonexistent +install=\$HOME/.local/bin/app +desktop=\$HAMPACK_DIR/desktop/yaac.desktop +steps=false +EOF + touch "$HOME/.local/bin/app" + mkdir -p "$HAMPACK_STATE" + echo "app=1.0" > "$HAMPACK_STATE/.installed_versions" + run bash "$REPO/install-compiled.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"up to date"* ]] + [ -f "$HOME/.local/share/applications/yaac.desktop" ] +} + +@test "update rebuilds when the version changes or isn't recorded, without asking" { + cat > "$HAMPACK_COMPILE_CONF" << EOF +[changed] +version=2.0 +git=$REPO_URL +install=\$HOME/.local/bin/changed +steps=echo built > \$HOME/.local/bin/changed + +[unrecorded] +version=1.0 +git=$REPO_URL +install=\$HOME/.local/bin/unrecorded +steps=echo built > \$HOME/.local/bin/unrecorded +EOF + touch "$HOME/.local/bin/changed" "$HOME/.local/bin/unrecorded" + mkdir -p "$HAMPACK_STATE" + echo "changed=1.0" > "$HAMPACK_STATE/.installed_versions" + run bash "$REPO/install-compiled.sh" < /dev/null + [ "$status" -eq 0 ] + [ "$(cat "$HOME/.local/bin/changed")" = built ] + [ "$(cat "$HOME/.local/bin/unrecorded")" = built ] + grep -qx "changed=2.0" "$HAMPACK_STATE/.installed_versions" +} + +@test "a step that reads stdin doesn't eat the rest of compile.conf" { + cat > "$HAMPACK_COMPILE_CONF" << EOF +[first] +version=1.0 +git=$REPO_URL +install=\$HOME/.local/bin/first +steps=cat, touch \$HOME/.local/bin/first + +[second] +version=1.0 +git=$REPO_URL +install=\$HOME/.local/bin/second +steps=touch \$HOME/.local/bin/second +EOF + run bash "$REPO/install-compiled.sh" + [ "$status" -eq 0 ] + [ -f "$HOME/.local/bin/second" ] +} + +@test "an app missing version= fails instead of prompting" { + printf '[app]\ngit=%s\ninstall=$HOME/x\nsteps=true\n' "$REPO_URL" > "$HAMPACK_COMPILE_CONF" + run bash "$REPO/install-compiled.sh" < /dev/null + [ "$status" -eq 1 ] + [[ "$output" == *"needs version="* ]] +} + +@test "the build directory is private and removed afterwards" { + printf '[app]\nversion=1\ngit=%s\ninstall=$HOME/.local/bin/app\nsteps=pwd > $HOME/dir, touch $HOME/.local/bin/app\n' "$REPO_URL" > "$HAMPACK_COMPILE_CONF" + run bash "$REPO/install-compiled.sh" + [ "$status" -eq 0 ] + [[ "$(cat "$HOME/dir")" == "$TMPDIR"/hampack.* ]] + [ ! -e "$(cat "$HOME/dir")" ] +} diff --git a/tests/installers.bats b/tests/installers.bats new file mode 100644 index 0000000..db7cf03 --- /dev/null +++ b/tests/installers.bats @@ -0,0 +1,85 @@ +#!/usr/bin/env bats +# +# install-windows-apps.sh, install-flatpaks.sh and patch-potacat.sh + +load helpers + +setup() { + setup_fake_home + fake_coredesktop + stub curl 'exit 1' +} + +install_all_windows_apps() { + local c="$HOME/.wine/drive_c" + mkdir -p "$c/VARA" "$c/VARA FM" "$c/RMS Express" + touch "$c/VARA/VARA.exe" "$c/VARA/VARA Terminal.exe" "$c/VARA FM/VARAFM.exe" "$c/RMS Express/RMS Express.exe" +} + +@test "windows apps: when all are installed, nothing is downloaded or run" { + install_all_windows_apps + export DISPLAY=:0 + run bash "$REPO/install-windows-apps.sh" + [ "$status" -eq 0 ] + ! grep -qE '^(curl|winetricks|wine) ' "$STUB_LOG" +} + +@test "windows apps: VARA Terminal is installed even when VARA HF is" { + install_all_windows_apps + rm "$HOME/.wine/drive_c/VARA/VARA Terminal.exe" + export DISPLAY=:0 + run bash "$REPO/install-windows-apps.sh" + [[ "$output" == *"VARA HF is already installed"* ]] + [[ "$output" == *"VARA Terminal was not installed"* ]] + grep -q "^curl .*VARA%20Products" "$STUB_LOG" +} + +@test "windows apps: without a desktop session, nothing is downloaded and the update isn't failed" { + run bash "$REPO/install-windows-apps.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"without a desktop session"* ]] + ! grep -q '^curl ' "$STUB_LOG" +} + +@test "windows apps: winetricks runs only once" { + export DISPLAY=:0 + mkdir -p "$HOME/.wine" + bash "$REPO/install-windows-apps.sh" || true + bash "$REPO/install-windows-apps.sh" || true + [ "$(grep -c '^winetricks ' "$STUB_LOG")" -eq 1 ] +} + +@test "flatpaks: installs missing ones from flathub and records only those" { + local first second + first="$(all_hampack_flatpaks | sed -n 1p)" + second="$(all_hampack_flatpaks | sed -n 2p)" + touch "$FAKE_FLATPAKS/$first" + run bash "$REPO/install-flatpaks.sh" + [ "$status" -eq 0 ] + grep -qx "flatpak install --system --noninteractive flathub $second" "$STUB_LOG" + [ "$(cat "$HAMPACK_STATE/installed-flatpaks")" = "$second" ] +} + +@test "flatpaks: update with everything installed installs nothing" { + local pak + for pak in $(all_hampack_flatpaks); do touch "$FAKE_FLATPAKS/$pak"; done + run bash "$REPO/install-flatpaks.sh" + [ "$status" -eq 0 ] + ! grep -q '^flatpak install' "$STUB_LOG" +} + +@test "patch-potacat removes deb and rpm targets in both forms" { + local pkg="$BATS_TEST_TMPDIR/package.json" + echo '{"build":{"linux":{"target":["AppImage","deb",{"target":"rpm"}],"category":"Utility"}}}' > "$pkg" + run bash "$REPO/patch-potacat.sh" "$pkg" + [ "$status" -eq 0 ] + [ "$(jq -c '.build.linux' "$pkg")" = '{"target":["AppImage"],"category":"Utility"}' ] +} + +@test "patch-potacat leaves a package.json without targets alone" { + local pkg="$BATS_TEST_TMPDIR/package.json" + echo '{"name":"x"}' > "$pkg" + run bash "$REPO/patch-potacat.sh" "$pkg" + [ "$status" -eq 0 ] + [ "$(jq -c . "$pkg")" = '{"name":"x"}' ] +} diff --git a/tests/lib.bats b/tests/lib.bats new file mode 100644 index 0000000..7ca79a0 --- /dev/null +++ b/tests/lib.bats @@ -0,0 +1,105 @@ +#!/usr/bin/env bats + +load helpers + +setup() { + setup_fake_home + source "$REPO/lib.sh" +} + +@test "expand_vars replaces only HOME, HAMPACK_DIR and USER" { + run expand_vars 'python $HOME/x.pyz; $HAMPACK_DIR/icon.png $USER' + [ "$output" = "python $HOME/x.pyz; $REPO/icon.png $USER" ] +} + +@test "expand_vars keeps semicolons, quotes and spacing" { + run expand_vars "HamRadio;Utility;" + [ "$output" = "HamRadio;Utility;" ] + run expand_vars "it's (two spaces) & \$OTHER" + [ "$output" = "it's (two spaces) & \$OTHER" ] +} + +collect() { SEEN+=("${CONF[_name]}:${CONF[version]}:${CONF[url]}"); } + +@test "parse_conf calls back once per section, including the last, skipping comments" { + cat > "$BATS_TEST_TMPDIR/t.conf" << 'EOF' +# a comment +[one] +version=1 +url=https://a/b?c=d=e + + # an indented comment +[two] +version=2 +EOF + SEEN=() + parse_conf "$BATS_TEST_TMPDIR/t.conf" collect + [ "${#SEEN[@]}" -eq 2 ] + [ "${SEEN[0]}" = "one:1:https://a/b?c=d=e" ] + [ "${SEEN[1]}" = "two:2:" ] +} + +uses_regex() { [[ "x" =~ (x) ]]; SEEN+=("${CONF[_name]}"); } + +@test "parse_conf section names survive callbacks that use =~" { + printf '[a]\n[b]\n[c]\n' > "$BATS_TEST_TMPDIR/t.conf" + SEEN=() + parse_conf "$BATS_TEST_TMPDIR/t.conf" uses_regex + [ "${SEEN[*]}" = "a b c" ] +} + +swallow_stdin() { cat > /dev/null; SEEN+=("${CONF[_name]}"); } + +@test "parse_conf callbacks can't swallow the rest of the file through stdin" { + printf '[a]\nx=1\n[b]\nx=2\n[c]\nx=3\n' > "$BATS_TEST_TMPDIR/t.conf" + SEEN=() + parse_conf "$BATS_TEST_TMPDIR/t.conf" swallow_stdin < /dev/null + [ "${SEEN[*]}" = "a b c" ] +} + +@test "versions are saved, replaced and forgotten by exact name" { + save_installed_version dx.py abc + save_installed_version dxApy other + save_installed_version dx.py def + [ "$(get_recorded_version dx.py)" = "def" ] + [ "$(get_recorded_version dxApy)" = "other" ] + [ "$(grep -c '^dx\.py=' "$VERSION_FILE")" -eq 1 ] + forget_version dx.py + [ -z "$(get_recorded_version dx.py)" ] + [ "$(get_recorded_version dxApy)" = "other" ] +} + +@test "get_recorded_version prints nothing without a version file" { + run get_recorded_version anything + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "record_installed keeps a sorted list without duplicates" { + record_installed installed-packages b a + record_installed installed-packages a c + record_installed installed-packages + [ "$(recorded_installs installed-packages | tr '\n' ' ')" = "a b c " ] +} + +@test "install_staged copies home files as the user, uses sudo outside home, and writes a manifest" { + local stage="$BATS_TEST_TMPDIR/stage" + mkdir -p "$stage$HOME/.local/bin" "$stage/etc/udev/rules.d" + echo new > "$stage$HOME/.local/bin/tool" + echo rule > "$stage/etc/udev/rules.d/99-test.rules" + echo old > "$HOME/.local/bin/tool" + + HAMPACK_APP=testapp install_staged "$stage" + + [ "$(cat "$HOME/.local/bin/tool")" = "new" ] + grep -qx "sudo cp -P --remove-destination $stage/etc/udev/rules.d/99-test.rules /etc/udev/rules.d/99-test.rules" "$STUB_LOG" + grep -qx "$HOME/.local/bin/tool" "$HAMPACK_STATE/manifests/testapp" + grep -qx "/etc/udev/rules.d/99-test.rules" "$HAMPACK_STATE/manifests/testapp" +} + +@test "install_staged needs HAMPACK_APP" { + mkdir -p "$BATS_TEST_TMPDIR/stage" + unset HAMPACK_APP + run install_staged "$BATS_TEST_TMPDIR/stage" + [ "$status" -ne 0 ] +} diff --git a/tests/update.bats b/tests/update.bats new file mode 100644 index 0000000..86ca2e2 --- /dev/null +++ b/tests/update.bats @@ -0,0 +1,98 @@ +#!/usr/bin/env bats +# +# update.sh as run on first install (by install.sh) and on later updates +# (by CoreDesktop's `update`) + +load helpers + +setup() { + setup_fake_home + fake_coredesktop + # No apps to download or build; those scripts have their own tests + export HAMPACK_BINARIES_CONF="$BATS_TEST_TMPDIR/empty.conf" + export HAMPACK_COMPILE_CONF="$BATS_TEST_TMPDIR/empty.conf" + : > "$HAMPACK_BINARIES_CONF" + local data="$BATS_TEST_TMPDIR/uv-tools/not1mm/lib/python3.13/site-packages/not1mm/data" + mkdir -p "$data" + touch "$data/k6gte-not1mm.desktop" + export UV_TOOL_DIR="$BATS_TEST_TMPDIR/uv-tools" + stub uv ' + case "$1 $2" in + "tool list") [ -f "$HOME/not1mm" ] && echo "not1mm v1.0" ;; + "tool install") touch "$HOME/not1mm" ;; + "tool dir") echo "$UV_TOOL_DIR" ;; + esac + exit 0' +} + +@test "first install records only the packages and Flatpaks it installed" { + mark_installed cmake wine + run bash "$REPO/update.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"HamPack updated."* ]] + # Everything on the list is installed now + local pkg + for pkg in $(all_hampack_packages); do [ -f "$FAKE_PACMAN_DB/$pkg" ]; done + # but what was there before isn't recorded as HamPack's + ! grep -qx cmake "$HAMPACK_STATE/installed-packages" + ! grep -qx wine "$HAMPACK_STATE/installed-packages" + grep -qx hamlib "$HAMPACK_STATE/installed-packages" + [ "$(wc -l < "$HAMPACK_STATE/installed-flatpaks")" -eq "$(all_hampack_flatpaks | wc -l)" ] + grep -qx "uv tool install not1mm" "$STUB_LOG" + [ -x "$HOME/.local/bin/hampackremove" ] +} + +@test "a later update installs and records nothing new" { + bash "$REPO/update.sh" + cp "$HAMPACK_STATE/installed-packages" "$BATS_TEST_TMPDIR/before" + : > "$STUB_LOG" + run bash "$REPO/update.sh" + [ "$status" -eq 0 ] + ! grep -q '^yay -S' "$STUB_LOG" + ! grep -q '^flatpak install' "$STUB_LOG" + grep -qx "uv tool upgrade not1mm" "$STUB_LOG" + cmp "$HAMPACK_STATE/installed-packages" "$BATS_TEST_TMPDIR/before" +} + +@test "an update after install from before records: existing packages aren't claimed" { + local pkg + for pkg in $(all_hampack_packages); do mark_installed "$pkg"; done + run bash "$REPO/update.sh" + [ "$status" -eq 0 ] + [ ! -s "$HAMPACK_STATE/installed-packages" ] +} + +@test "update removes stale state from older HamPack versions" { + mkdir -p "$HAMPACK_STATE" + printf 'Kiwix-Desktop=2.75.1\nkiwix-desktop=2.5.1\nKiwix-Tools=3.8.2\n' > "$HAMPACK_STATE/.installed_versions" + touch "$HOME/.local/bin/hampackupdate" + run bash "$REPO/update.sh" + [ "$(cat "$HAMPACK_STATE/.installed_versions")" = "kiwix-desktop=2.5.1" ] + [ ! -e "$HOME/.local/bin/hampackupdate" ] + grep -qx "sudo rm -f /etc/sudoers.d/hampack" "$STUB_LOG" +} + +@test "graywolf's service is installed and enabled once graywolf is built" { + run bash "$REPO/update.sh" + ! grep -q "graywolf.service" "$STUB_LOG" + + touch "$HOME/.local/bin/graywolf" + chmod +x "$HOME/.local/bin/graywolf" + : > "$STUB_LOG" + run bash "$REPO/update.sh" + [ "$status" -eq 0 ] + grep -qx "sudo tee /etc/systemd/system/graywolf.service" "$STUB_LOG" + grep -qx "enable_services graywolf" "$STUB_LOG" +} + +@test "the graywolf web UI listens only on this machine" { + grep -q -- "-http 127.0.0.1:8080" "$REPO/systemd/graywolf.service" +} + +@test "a failing step is reported and the rest still run" { + stub flatpak 'exit 1' + run bash "$REPO/update.sh" + [ "$status" -eq 1 ] + [[ "$output" == *"errors in: Installing Flatpak applications"* ]] + grep -qx "uv tool install not1mm" "$STUB_LOG" +} diff --git a/update.sh b/update.sh index f119396..0975645 100644 --- a/update.sh +++ b/update.sh @@ -8,6 +8,7 @@ source "$COREDESKTOP/utils.sh" # Config files and build steps refer to files in this repo through $HAMPACK_DIR export HAMPACK_DIR="$SELF" +source "$SELF/lib.sh" FAILED=() @@ -36,6 +37,21 @@ remove_stale_files() { rm -f "$HOME"/.local/bin/paracon_*.pyz # Hooks from before HamPack was a sibling; 71-siblings.sh now runs this script rm -f "$COREDESKTOP"/updates.d/[0-9][0-9][0-9]-hampack-* + # Versions recorded under app names HamPack no longer uses + forget_version Kiwix-Desktop + forget_version Kiwix-Tools + # Build directories from before builds used a private temporary directory + rm -rf /tmp/hampack-build /tmp/windowsapps 2> /dev/null + # The passwordless sudoers file the old installer used, if a killed install left it behind + sudo rm -f /etc/sudoers.d/hampack + # Builds used to run `sudo make install` into ~/.local, leaving files there owned by root + local dirs=() dir + for dir in bin lib include share/applications share/doc share/icons share/man share/metainfo share/direwolf; do + [ -d "$HOME/.local/$dir" ] && dirs+=("$HOME/.local/$dir") + done + [ ${#dirs[@]} -eq 0 ] || + find "${dirs[@]}" -xdev -user root -print0 2> /dev/null | xargs -0 -r sudo chown -h "$USER:" + return 0 } install_tools() { @@ -43,24 +59,37 @@ install_tools() { install_script "$SELF/hampackremove" "$HOME/.local/bin/hampackremove" } +# Install the packages in packages.conf, and record the ones that weren't +# already installed, so hampackremove removes those and nothing the user had before install_hampack_packages() { source "$SELF/packages.conf" - install_packages "${UTILITIES[@]}" && install_packages "${APPLICATIONS[@]}" + local pkg missing=() added=() status=0 + + for pkg in "${UTILITIES[@]}" "${APPLICATIONS[@]}"; do + is_installed "$pkg" || missing+=("$pkg") + done + + install_packages "${UTILITIES[@]}" && install_packages "${APPLICATIONS[@]}" || status=1 + + for pkg in "${missing[@]}"; do + is_installed "$pkg" && added+=("$pkg") + done + record_installed installed-packages "${added[@]}" + return $status } install_not1mm() { - if command -v uv &> /dev/null && uv tool list 2> /dev/null | grep -q '^not1mm '; then + if uv tool list 2> /dev/null | grep -q '^not1mm '; then uv tool upgrade not1mm else - curl -LsSf uvx.sh/not1mm/install.sh | sh + uv tool install not1mm fi } # not1mm installs its own launcher only on first run, so install it up front install_not1mm_desktop() { - local uv data - uv=$(command -v uv || echo "$HOME/.local/bin/uv") - data=$(echo "$("$uv" tool dir)"/not1mm/lib/python3*/site-packages/not1mm/data) + local data + data=$(echo "$(uv tool dir)"/not1mm/lib/python3*/site-packages/not1mm/data) [ -f "$data/k6gte-not1mm.desktop" ] || return 1 for size in 32 64 128; do xdg-icon-resource install --size "$size" --context apps --mode user \ @@ -69,33 +98,48 @@ install_not1mm_desktop() { xdg-desktop-menu install --mode user "$data/k6gte-not1mm.desktop" } -install_dxpy() { - wget -q https://raw.githubusercontent.com/HB9VQQ/dx.py/main/dx.py -O "$HOME/.local/bin/dx.py" && - chmod +x "$HOME/.local/bin/dx.py" -} +# Install graywolf's systemd service once graywolf is built, and reinstall it +# when systemd/graywolf.service changes. To change how graywolf runs (e.g. to +# reach its web UI from other machines), use `sudo systemctl edit graywolf`: +# a drop-in survives updates, while edits to the unit file itself don't. +install_graywolf_service() { + [ -x "$HOME/.local/bin/graywolf" ] || return 0 -install_desktop_files() { - mkdir -p "$HOME/.local/share/applications" - sed "s|\$HAMPACK_DIR|$SELF|g; s|\$HOME|$HOME|g" "$SELF/desktop/js8call.desktop" \ - > "$HOME/.local/share/applications/js8call.desktop" && - update-desktop-database "$HOME/.local/share/applications/" + local unit=/etc/systemd/system/graywolf.service rendered + rendered="$(sed "s|HAMPACK_USER|$USER|g; s|HAMPACK_HOME|$HOME|g" "$SELF/systemd/graywolf.service")" + mkdir -p "$HOME/.local/share/graywolf" + getent group dialout > /dev/null || sudo groupadd dialout || return 1 + + if [ "$(cat "$unit" 2> /dev/null)" != "$rendered" ]; then + echo "Installing $unit..." + printf '%s\n' "$rendered" | sudo tee "$unit" > /dev/null || return 1 + sudo systemctl daemon-reload || return 1 + sudo systemctl try-restart graywolf + fi + enable_services graywolf } # --- Main --- echo "Updating HamPack..." +# Builds and package installs need sudo; ask now rather than partway through. +# Under CoreDesktop's `update`, sudo already has the password and doesn't ask. +if ! start_sudo_keepalive; then + echo "HamPack needs sudo to install packages and applications." + exit 1 +fi + step "Removing stale HamPack files" remove_stale_files step "Installing HamPack tools" install_tools step "Installing HamPack packages" install_hampack_packages step "Installing Flatpak applications" bash "$SELF/install-flatpaks.sh" step "Installing pre-built binaries" bash "$SELF/install-binaries.sh" step "Installing compiled applications" bash "$SELF/install-compiled.sh" +step "Installing graywolf service" install_graywolf_service step "Installing Windows applications" bash "$SELF/install-windows-apps.sh" step "Installing not1mm" install_not1mm step "Installing not1mm desktop file" install_not1mm_desktop -step "Installing dx.py" install_dxpy -step "Installing desktop files" install_desktop_files if (( ${#FAILED[@]} > 0 )); then echo "HamPack updated, with errors in: ${FAILED[*]}"