#!/usr/bin/env bash set -Eeuo pipefail # Increment when package payload/installation semantics change without a source commit. PACKAGING_REVISION=7 OPNSENSE_PACKAGING_REVISION=4 FREEBSD_NATIVE_PACKAGING_REVISION=2 log() { printf '%s %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$*" } fatal() { log "ERROR: $*" >&2 exit 1 } require_cmd() { command -v "$1" >/dev/null 2>&1 || fatal "required command not found: $1" } trim_trailing_slash() { local value=$1 while [[ "$value" == */ ]]; do value=${value%/}; done printf '%s' "$value" } yaml_quote() { python3 - "$1" <<'PY' import json, sys print(json.dumps(sys.argv[1])) PY } read_first_field() { local file=$1 key=$2 awk -v key="$key" 'BEGIN{IGNORECASE=1} $0 ~ "^" key ":[[:space:]]*" {sub(/^[^:]+:[[:space:]]*/, ""); print; exit}' "$file" } atomic_write() { local path=$1 local tmp="${path}.tmp.$$" cat > "$tmp" mv -f "$tmp" "$path" } is_true() { case "${1:-}" in 1|true|TRUE|yes|YES|on|ON) return 0 ;; *) return 1 ;; esac } require_positive_integer() { local name=$1 value=$2 [[ "$value" =~ ^[1-9][0-9]*$ ]] || fatal "$name must be a positive integer" } # Keep the newest N artifacts matching a package family. An optional exclude # glob is useful for repositories that contain both rustproxy-* and # rustproxy-docker-agent-* files in the same directory. prune_package_files() { local directory=$1 pattern=$2 keep=$3 exclude_pattern=${4:-} require_positive_integer PACKAGE_RETENTION "$keep" [[ -d "$directory" ]] || return 0 local -a find_args=("$directory" -maxdepth 1 -type f -name "$pattern") if [[ -n "$exclude_pattern" ]]; then find_args+=( ! -name "$exclude_pattern" ) fi mapfile -t stale < <( find "${find_args[@]}" -printf '%T@ %p\n' \ | sort -nr \ | awk -v keep="$keep" 'NR>keep {$1=""; sub(/^ /,""); print}' ) local path for path in "${stale[@]}"; do [[ -n "$path" ]] || continue log "package retention: removing $path" rm -f -- "$path" done } prune_release_dirs() { local releases_dir=$1 keep=$2 protected=${3:-} require_positive_integer RELEASE_RETENTION "$keep" [[ -d "$releases_dir" ]] || return 0 mapfile -t stale < <( find "$releases_dir" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' \ | sort -nr \ | awk -v keep="$keep" 'NR>keep {$1=""; sub(/^ /,""); print}' ) local path for path in "${stale[@]}"; do [[ -n "$path" && "$path" != "$protected" ]] || continue log "release retention: removing $path" rm -rf -- "$path" done }