#!/bin/sh

# Override the build hook of the automated builds on Docker Hub.
# The build hook is running in the root of the git sandbox. That is the directory contaning the file RELEASE.

# See https://docs.docker.com/docker-hub/builds/advanced/ for the list of preset environment variables.
# Additionally, the undocumented environment variable $BUILD_PATH is used. $BUILD_PATH can be set
# in the input field Build Context of the Docker Hub automated build GUI.

# The path to the Dockerfile is given relative to ./$BUILD_PATH in order to be compatible
# with the default build hook in Docker Hub. The default value of the Build Context is '/'. The build command works with this approach,
# because the resulting path will then be something like .//otobo.web.dockerfile.

# These commands can be uncommented for debugging:
#pwd
#ls
#docker -v
#printenv

# Specify the target in order to stay compatible with newer versions of OTOBO.
# In newer version different image are built with the same Dockerfile.
TARGET_OPTION=$(case $DOCKERFILE_PATH in
    "otobo.web.dockerfile")                  echo "--target=otobo-web" ;;
    "otobo.kerberos.web.dockerfile")         echo "--target=otobo-web-kerberos" ;;
    "otobo.elasticsearch.dockerfile")        echo "--target=otobo-elasticsearch" ;;
    "otobo.nginx.dockerfile")                echo "--target=otobo-nginx-webproxy" ;;
    "otobo.nginx-kerberos.dockerfile")       echo "--target=otobo-nginx-kerberos-webproxy" ;;
    "otobo.selenium-chrome.dockerfile")      echo "--target=otobo-selenium-chrome" ;;
    *)                                       echo "" ;;
esac)

# build the Docker image
# add the option '--progress plain' for seeing the printed output
docker build\
 --build-arg "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')"\
 --build-arg "DOCKER_TAG=$DOCKER_TAG"\
 --build-arg "GIT_COMMIT=$SOURCE_COMMIT"\
 --build-arg "GIT_BRANCH=$SOURCE_BRANCH"\
 --build-arg "GIT_REPO=$(git config --get remote.origin.url)"\
 -f ./$BUILD_PATH/$DOCKERFILE_PATH\
 -t $IMAGE_NAME\
 $TARGET_OPTION\
 ./$BUILD_PATH
