#!/usr/bin/env bash
# Build or rebuild the derp container image.
# Usage: tools/build [--no-cache]

# shellcheck source=tools/_common.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/_common.sh"
cd "$PROJECT_DIR" || exit 1

args=()
[[ "${1:-}" == "--no-cache" ]] && args+=(--no-cache)

dim "Building image..."
$COMPOSE build "${args[@]}"

size=$(podman image inspect "$IMAGE_NAME" --format '{{.Size}}' 2>/dev/null || true)
if [[ -n "$size" ]]; then
    human=$(numfmt --to=iec-i --suffix=B "$size" 2>/dev/null || echo "${size} bytes")
    info "Image built ($human)"
else
    info "Image built"
fi
