#!/bin/bash

# Script zum Bauen und Pushen der Docker Images zur Registry (Multi-Plattform)
set -e

REGISTRY="docker.citysensor.de"
PROJECT="wetterstation"
PLATFORMS="linux/amd64,linux/arm64"

echo "🔧 Setting up buildx builder..."
# Erstelle oder verwende existierenden Builder
docker buildx create --name multiplatform --use 2>/dev/null || docker buildx use multiplatform

echo ""
echo "🔨 Building and pushing Docker images for ${PLATFORMS}..."

# Baue und pushe alle Images mit buildx
docker buildx build --platform ${PLATFORMS} \
  -t ${REGISTRY}/${PROJECT}/collector:latest \
  --push \
  ./collector

docker buildx build --platform ${PLATFORMS} \
  -t ${REGISTRY}/${PROJECT}/api:latest \
  --push \
  ./api

docker buildx build --platform ${PLATFORMS} \
  -t ${REGISTRY}/${PROJECT}/frontend:latest \
  --push \
  ./frontend

docker buildx build --platform ${PLATFORMS} \
  -t ${REGISTRY}/${PROJECT}/monitor:latest \
  -f monitor/Dockerfile \
  --push \
  .

echo ""
echo "✅ Done! Multi-platform images successfully pushed to ${REGISTRY}"
echo "   Platforms: ${PLATFORMS}"
echo ""
echo "To pull and run on another machine:"
echo "  docker compose pull"
echo "  docker compose up -d"
