#!/bin/bash # # Purpose: Download and install pre-built binaries from binaries.conf source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib.sh" CONF_FILE="${HAMPACK_BINARIES_CONF:-$HAMPACK_DIR/binaries.conf}" APPLICATIONS_DIR="$HOME/.local/share/applications" FAILED=() # Check if an update is needed needs_update() { local app="$1" install_path="$2" latest="$3" installed if [ ! -e "$install_path" ]; then echo " $app is not installed." return 0 fi installed="$(get_recorded_version "$app")" if [ -z "$installed" ]; then echo " Installed version of $app is unknown, reinstalling version $latest." return 0 fi if [ "$installed" = "$latest" ]; then echo " $app $installed is up to date." return 1 fi echo " Updating $app from $installed to $latest." return 0 } # 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 url="$1" install_path="$2" sha256="$3" local tmp_file="$HAMPACK_TMP/download" echo " Downloading $url..." if ! curl -fsSL "$url" -o "$tmp_file"; then echo " Error: download of $url failed." return 1 fi 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" } # 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" exec_cmd="$2" comment="$3" icon="$4" categories="$5" mkdir -p "$APPLICATIONS_DIR" cat > "$APPLICATIONS_DIR/$app.desktop" << EOF [Desktop Entry] Type=Application Name=$app Exec=$exec_cmd Comment=$comment Icon=$icon Categories=$categories EOF } # Process the app in CONF (called by parse_conf for each section) process_app() { 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" ] || [ -z "$install_path" ] || [ -z "$version" ]; then echo " Error: $app needs url=, install= and version= in binaries.conf, skipping." FAILED+=("$app") return fi if needs_update "$app" "$install_path" "$version"; then if ! download_binary "$url" "$install_path" "$sha256"; then FAILED+=("$app") return fi save_installed_version "$app" "$version" echo " $app $version installed." fi 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 } # --- Main --- if [ ! -f "$CONF_FILE" ]; then echo "Error: $CONF_FILE not found." exit 1 fi make_work_dir || exit 1 mkdir -p "$HOME/.local/bin" echo "Starting HamPack binary installs..." parse_conf "$CONF_FILE" process_app 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