#!/bin/bash # # Purpose : Compile and install applications from compile.conf source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib.sh" CONF_FILE="${HAMPACK_COMPILE_CONF:-$HAMPACK_DIR/compile.conf}" APPLICATIONS_DIR="$HOME/.local/share/applications" FAILED=() # Check if an update is needed. Never asks: this runs unattended under `update`. 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, building 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 } # 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" [ -n "$desktop" ] || return 0 if [ ! -f "$desktop" ]; then echo " Warning: desktop file $desktop not found, skipping." return 0 fi 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" version="$2" tags tag tags=$(git ls-remote --tags --refs "$git_url" | sed 's|.*refs/tags/||') || return 1 [ -z "$tags" ] && return 0 for tag in "$version" "v$version"; do if grep -qxF "$tag" <<< "$tags"; then echo "$tag" return 0 fi done return 1 } # Fetch source via git or an archive download into SRC_DIR fetch_source() { 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 --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 ! 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 --quiet --branch "$tag" "$git_url" "$src_dir" || return 1 else echo " $git_url has no tags; cloning the default branch..." git clone --quiet "$git_url" "$src_dir" || return 1 fi elif [ -n "$wget_url" ]; then local filename="${wget_url##*/}" local filepath="$HAMPACK_TMP/$filename" 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 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 ;; *) echo " Error: unknown file type for $filename" return 1 ;; esac rm -f "$filepath" else echo " Error: no git= or wget= URL in compile.conf" return 1 fi } # 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 steps="$1" src_dir="$2" step step_list cd "$src_dir" || return 1 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" < /dev/null; then echo " Error: step failed: $step" return 1 fi done } # Process the app in CONF (called by parse_conf for each section) process_app() { 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 ===" 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 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 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 install_desktop "$desktop" } # --- Main --- if [ ! -f "$CONF_FILE" ]; then echo "Error: $CONF_FILE not found." exit 1 fi make_work_dir || exit 1 echo "Starting HamPack source builds..." 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