#!/usr/bin/env bash
set -euo pipefail

# =============================================================================
# Build dc-backend Docker-Image und push auf Registry
# =============================================================================

REGISTRY="registry.express-o.org"
IMAGE_NAME="frigosped/dc-backend"
TAG="${1:-latest}"
FULL_IMAGE="$REGISTRY/$IMAGE_NAME:$TAG"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "=== dc-backend Build & Push ==="
echo "Image : $FULL_IMAGE"
echo ""

# --- Build ---
echo "→ Build Image..."
podman build \
  --format docker \
  --tag "$FULL_IMAGE" \
  --label "build.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
  --label "build.version=$TAG" \
  "$SCRIPT_DIR"

# Bei explizitem Tag zusätzlich 'latest' setzen
if [ "$TAG" != "latest" ]; then
  podman tag "$FULL_IMAGE" "$REGISTRY/$IMAGE_NAME:latest"
  echo "→ Auch getaggt als: $REGISTRY/$IMAGE_NAME:latest"
fi

# --- Push ---
echo "→ Push $FULL_IMAGE ..."
podman push "$FULL_IMAGE"

if [ "$TAG" != "latest" ]; then
  echo "→ Push $REGISTRY/$IMAGE_NAME:latest ..."
  podman push "$REGISTRY/$IMAGE_NAME:latest"
fi

echo ""
echo "✓ Fertig: $FULL_IMAGE"
