#!/usr/bin/env bash

# if there is one variable, set local_build to true, this build images from
# local git instead of checking to make sure you are on head of develop/sim
if [ $# -eq 1 ]; then
    local_build=true
else
    local_build=false
fi

scripts="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd ${scripts}/..
marvel2_dir="$(pwd)"

# names of the sub directories from where img_build.sh should be run
subdirs="kong kong/kafka/databricks-scheduler-consumer kong/kafka/echo-sim-results-consumer kong/kafka/run-summaries-consumer jobz frontend"

echo
echo "===== Starting all image builds from ${marvel2_dir}"
for dirname in $subdirs; do
    # always start from the marvel2 dir
    cd $marvel2_dir
    cd $dirname
    echo
    echo "===== Building image in $(pwd)"

    if [ -f ./img_build.sh ]; then
        build_script="./img_build.sh"
    else
        build_script="${scripts}/img_build.sh"
    done

    if [ "$local_build" = true ]
    then
        ${build_script} --local_build
    else
        ${build_script}
    fi
    if [ $? -ne 0 ]; then
        echo
        echo "===== ERROR in Building image in $(pwd)"
        exit 1
    fi
done

echo
echo "===== SUCCESS in Building all images"
exit 0
