#!/bin/bash
set -e

REGISTRY="registry.bcos.cloud"
SHOULD_PUSH=false

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --registry) REGISTRY="$2"; shift ;;
        --push) SHOULD_PUSH=true ;;
    esac
    shift
done

if [[ "$REGISTRY" == *"bcos.dev"* ]]; then
    TAG="dev"
else
    TAG="latest"
fi

BACKEND_IMAGE="$REGISTRY/hiveiq-guide-backend:$TAG"
FRONTEND_IMAGE="$REGISTRY/hiveiq-guide-frontend:$TAG"

echo "Building backend..."
cd backend
mvn clean package -DskipTests -q
docker build -t "$BACKEND_IMAGE" .
cd ..

echo "Building frontend..."
docker build -t "$FRONTEND_IMAGE" ./frontend

if [ "$SHOULD_PUSH" = true ]; then
    echo "Pushing $BACKEND_IMAGE..."
    docker push "$BACKEND_IMAGE"
    echo "Pushing $FRONTEND_IMAGE..."
    docker push "$FRONTEND_IMAGE"
fi

echo "Done: $BACKEND_IMAGE, $FRONTEND_IMAGE"
