#!/usr/bin/env bash
# =============================================================================
# felhom-samba — infra image build+push helper (R-7 slice 1).
# Run on the build server (192.168.0.180). Mirrors build.sh usage.
#
#   ./build-samba-image.sh 1.0.0            # build local only
#   ./build-samba-image.sh 1.0.0 --push     # build + push to Gitea registry
#
# NEVER tags :latest (a floating tag breaks reproducible pins). The controller pins the
# exact tag in the generated compose. After --push, verify ANONYMOUS pull works (the guest
# pulls infra images without creds — same as the controller self-update path).
# =============================================================================
set -euo pipefail

VERSION="${1:?usage: build-samba-image.sh <version> [--push]}"
ACTION="${2:-}"

REGISTRY="gitea.dooplex.hu/admin"
IMAGE="${REGISTRY}/felhom-samba"
CTX="$(cd "$(dirname "$0")/../infra-images/samba" && pwd)"

echo "[INFO] building ${IMAGE}:${VERSION}"
echo "[INFO] context: ${CTX}"
docker build -t "${IMAGE}:${VERSION}" "${CTX}"

if [[ "${ACTION}" == "--push" ]]; then
    echo "[INFO] pushing ${IMAGE}:${VERSION}"
    docker push "${IMAGE}:${VERSION}"
    echo "[INFO] pushed. Verify anonymous pull (guest has no registry creds):"
    echo "       docker logout ${REGISTRY%%/*} 2>/dev/null; docker pull ${IMAGE}:${VERSION}"
else
    echo "[INFO] local build only. To push: ./build-samba-image.sh ${VERSION} --push"
fi
