#!/bin/bash
set -e

# Check if we are in the right place
if [ ! -d ./images ]; 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 images
    $BUILD_COMMAND images/base -t rosetta/base
    $BUILD_COMMAND images/slurmbase -t rosetta/slurmbase
    $BUILD_COMMAND images/slurmcluster -t rosetta/slurmcluster    
    $BUILD_COMMAND images/slurmclustermaster -t rosetta/slurmclustermaster    
    $BUILD_COMMAND images/slurmclusterworker -t rosetta/slurmclusterworker    
    $BUILD_COMMAND images/dregistry -t rosetta/dregistry
    $BUILD_COMMAND images/webapp -t rosetta/webapp

    
else

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

fi
