#!/bin/bash -e

SUDO=""
if [[ $EUID -ne 0 ]]; then
  SUDO="sudo -E"
fi

if test -z "${DIR}"; then
    echo "This script should not be called directly."
    exit -1
fi

USER="docker"
GROUP="docker"

# build image(s) in order (to satisfy dependencies)
for dep in '.8.*' '.7.*' '.6.*' '.5.*' '.4.*' '.3.*' '.2.*' '.1.*' ''; do
    for dockerfile in `find "${DIR}" -name "Dockerfile${dep}" -print`; do
        image=$(head -n 1 "$dockerfile" | grep '# ' | cut -d' ' -f2)
        if test -z "$image"; then image="$IMAGE"; fi

        if grep -q 'AS build' "$dockerfile"; then
            ${SUDO} docker build --network=host --file="$dockerfile" --target build -t "$image:build" "$DIR" $(env | grep -E '_(proxy|REPO|VER)=' | sed 's/^/--build-arg /') --build-arg USER=${USER} --build-arg GROUP=${GROUP} --build-arg UID=$(id -u) --build-arg GID=$(id -g)
        fi

        ${SUDO} docker build --network=host --file="$dockerfile" -t "$image:latest" "$DIR" $(env | grep -E '_(proxy|REPO|VER)=' | sed 's/^/--build-arg /') --build-arg USER=${USER} --build-arg GROUP=${GROUP} --build-arg UID=$(id -u) --build-arg GID=$(id -g)
    done
done
