#!/bin/bash
set -e

# Check if we are in the right place
if [ ! -d ./services ]; then
    echo "You must run this command from the project's root folder."
    exit 1
fi


# Set service and cacheing
if [[ "x$1" == "xnocache" ]] ; then
    NOCACHE=True
    SERVICE=""
elif [[ "x$2" == "xnocache" ]] ; then
    NOCACHE=True 
    SERVICE=$1
else
    NOCACHE=False
    SERVICE=$1
fi

if [[ "x$NOCACHE" == "xTrue" ]] ; then
    BUILD_COMMAND="docker build --no-cache"
else
    BUILD_COMMAND="docker build"
fi

if [[ "x$SERVICE" == "x" ]] ; then
    
    # Build all services
    $BUILD_COMMAND services/base -t rosetta/base
    $BUILD_COMMAND services/slurmbase -t rosetta/slurmbase
    $BUILD_COMMAND services/slurmcluster -t rosetta/slurmcluster    
    $BUILD_COMMAND services/slurmclustermaster -t rosetta/slurmclustermaster    
    $BUILD_COMMAND services/slurmclusterworker -t rosetta/slurmclusterworker    
    $BUILD_COMMAND services/dregistry -t rosetta/dregistry
    $BUILD_COMMAND services/webapp -t rosetta/webapp

    
else

    # Build a specific image
    $BUILD_COMMAND services/$SERVICE -t rosetta/$SERVICE

fi
