#!/usr/bin/env bash
#
# build-stamp.sh — the build stamp both image builders pass to the Containerfile.
#
# Sourced by build/build-image.sh and build/build-iso.sh, the same way
# build/privilege.sh is: they are the two scripts that turn the image into
# something installable, and an unstamped artifact is the one shape a support
# conversation cannot place.
#
# The Containerfile's ALLOY_BUILD_STAMP defaults empty and stamps nothing, so a
# bare `podman build` keeps its layer cache and honestly says nothing about a
# build number. These scripts always pass one. See the stamping block in the
# Containerfile for the three-clock scheme (product / build / Fedora base).
#
# Format is <YYYYMMDD>.<serial>, UTC. The serial is the time of day, which
# distinguishes same-day rebuilds without keeping a counter anywhere. UTC and
# not local time so two machines in two timezones cannot mint the same stamp
# from different days.

# Append the stamp to a BUILD_ARGS array, unless the caller already passed one
# by hand. Hand-passing wins: `--build-arg ALLOY_BUILD_STAMP=20260816.1` is how
# a release build pins a stamp it decided elsewhere, and a second value would
# leave which one podman honoured up to argument order.
#
# Usage: stamp_build_args BUILD_ARGS   (the array's NAME, not its contents)
stamp_build_args() {
  local -n args="$1"
  local existing
  for existing in "${args[@]}"; do
    case "$existing" in
      ALLOY_BUILD_STAMP=*) return 0 ;;
    esac
  done
  args+=(--build-arg "ALLOY_BUILD_STAMP=$(date -u +%Y%m%d.%H%M%S)")
}
