#!/bin/bash # Helper functions for the Hibbian build scripts. This file expects callers to # define the shared configuration variables they need before sourcing it. log_step() { local message="==== $(date) $* ====" if [[ "${LOG_TO_STDOUT:-0}" == "1" ]]; then echo "$message" | tee -a "$LOGFILE" else echo "$message" >> "$LOGFILE" fi } ensure_git_repo_present() { local repo_url="$1" local repo_dir="$2" if [[ -d "$repo_dir/.git" ]]; then return 0 fi if [[ -e "$repo_dir" ]]; then echo "ERROR: $repo_dir exists but is not a git repository" exit 1 fi echo "Cloning $repo_url into $repo_dir..." git clone "$repo_url" "$repo_dir" } ensure_git_repo_updated() { local repo_url="$1" local repo_dir="$2" local log_file="$3" if [[ -d "$repo_dir/.git" ]]; then echo "Updating existing repo..." >> "$log_file" git -C "$repo_dir" pull --ff-only >> "$log_file" 2>&1 elif [[ -e "$repo_dir" ]]; then echo "ERROR: $repo_dir exists but is not a git repository" >> "$log_file" exit 1 else echo "Cloning repo..." >> "$log_file" git clone "$repo_url" "$repo_dir" >> "$log_file" 2>&1 fi } ensure_pi_gen() { ensure_git_repo_present "$PI_GEN_REPO" "$PI_GEN_DIR" } get_today_build() { local dir="$1" local suffix="$2" local today today=$(date +%Y-%m-%d) for f in "$dir"/"${today}"-*"$suffix"; do [[ -e "$f" && -s "$f" ]] && { echo "$f"; return 0; } done return 1 } find_single_artifact() { local dir="$1" local pattern="$2" local description="$3" local artifacts=() mapfile -t artifacts < <(find "$dir" -maxdepth 1 -type f -name "$pattern" -print 2>/dev/null | sort) if (( ${#artifacts[@]} == 0 )); then echo "ERROR: No $description produced" >> "$LOGFILE" return 1 fi if (( ${#artifacts[@]} > 1 )); then echo "ERROR: Multiple $description files found; refusing to guess which one to publish" >> "$LOGFILE" printf ' %s\n' "${artifacts[@]}" >> "$LOGFILE" return 1 fi printf '%s\n' "${artifacts[0]}" } has_today_remote_arm64() { ssh "$REMOTE" " for f in $REMOTE_DIR/$ZIP_NAME; do [ -e \"\$f\" ] && [ -s \"\$f\" ] && exit 0 done exit 1 " } generate_links_notes() { # Create/update "latest" symlink ln -sfn "$FINAL_NAME" "$AMD64_DIR/hibbian-latest.iso" ln -sfn "$FINAL_NAME.sha256" "$AMD64_DIR/hibbian-latest.iso.sha256" # Generate release notes generate_release_notes ln -sfn "${FINAL_NAME%.iso}-packages.txt" "$AMD64_DIR/hibbian-latest-packages.txt" ln -sfn "${FINAL_NAME%.iso}-release-notes.txt" "$AMD64_DIR/hibbian-latest-release-notes.txt" ln -sfn "${FINAL_NAME%.iso}-changes.txt" "$AMD64_DIR/hibbian-latest-changes.txt" } ensure_all_diffs() { echo "Checking diffs..." # Collect package files (oldest → newest) mapfile -t FILES < <(ls -1 "$OUTPUT_DIR/amd64"/*-packages.txt 2>/dev/null | sort) # Need at least 2 files to compare if (( ${#FILES[@]} < 2 )); then echo "Not enough package lists to generate diffs" return fi # Iterate safely: previous → current for ((i=1; i<${#FILES[@]}; i++)); do PREVIOUS="${FILES[$((i-1))]}" CURRENT="${FILES[$i]}" # Skip if either file is missing or empty [[ ! -s "$PREVIOUS" || ! -s "$CURRENT" ]] && { echo "Skipping empty/missing pair:" echo " PREV: $PREVIOUS" echo " CURR: $CURRENT" continue } base=$(basename "$CURRENT" -packages.txt) DIFF_FILE="$AMD64_DIR/${base}-changes.txt" # Skip if already exists [[ -f "$DIFF_FILE" ]] && continue echo "Generating diff for $base..." # Use temp files (safe + debuggable) TMP_PREV=$(mktemp) TMP_CURR=$(mktemp) sort "$PREVIOUS" > "$TMP_PREV" sort "$CURRENT" > "$TMP_CURR" { echo "Changes since last build" echo "========================" echo "" echo "Previous: $(basename "$PREVIOUS")" echo "Current: $(basename "$CURRENT")" echo "" echo "New packages:" comm -13 "$TMP_PREV" "$TMP_CURR" || true echo "" echo "Removed packages:" comm -23 "$TMP_PREV" "$TMP_CURR" || true echo "" echo "Version changes:" join -j1 "$TMP_PREV" "$TMP_CURR" \ | awk '$2 != $3 { printf "%-30s %s -> %s\n", $1, $2, $3 }' } > "$DIFF_FILE" rm -f "$TMP_PREV" "$TMP_CURR" done } rebuild_metrics() { METRICS_FILE="$OUTPUT_DIR/build-history.csv" > "$METRICS_FILE" for pkg in "$OUTPUT_DIR"/amd64/*-packages.txt; do base=$(basename "$pkg" -packages.txt) DATE=$(date -r "$pkg" "+%Y-%m-%d %H:%M") PKG_COUNT=$(wc -l < "$pkg" 2>/dev/null || echo 0) CHANGES_FILE="$AMD64_DIR/${base}-changes.txt" UPDATED_COUNT=$(grep -c "->" "$CHANGES_FILE" 2>/dev/null || echo 0) echo "$DATE,$base,$PKG_COUNT,$UPDATED_COUNT" >> "$METRICS_FILE" done } generate_index() { INDEX_FILE="$OUTPUT_DIR/index.html" echo "Generating HTML index..." cat > "$INDEX_FILE" < Hibbian Downloads

Hibbian Weekly Builds

EOF echo "

Latest Downloads

" >> "$INDEX_FILE" echo "