#!/bin/bash
#
# PBA docker build wrapper
#
# (C) ITEG IT-Engineers GmbH 2010-2026
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# pba-docker-build [extraBuildOpts]
#
# Builds a docker image based on pba-docker-settings.txt
# 

# params

. pba-docker-checksettings || exit $?
# exit values <20 come from there

EXTRA_OPTS="$*"
if [ -n "${EXTRA_OPTS}" ] ; then
  echo "INFO: Picking up extra build options '${EXTRA_OPTS}'"
fi

test -f pba-docker-build.log && mv -v pba-docker-build.log pba-docker-build.log.bak

export TAGOPTIONS=""
for IMAGE_TAG in ${IMAGE_TAGS} ; do
  TAGOPTIONS="${TAGOPTIONS} -t ${IMAGE_NAME}:${IMAGE_TAG}"
done

echo "INFO: Calling   docker build ${TAGOPTIONS} --progress plain ${BUILD_OPTIONS} ${EXTRA_OPTS} -f Dockerfile ." |tee pba-docker-build.log

docker build ${TAGOPTIONS} --progress plain ${BUILD_OPTIONS} ${EXTRA_OPTS} -f Dockerfile . 2>&1 |tee -a pba-docker-build.log
PRC=${PIPESTATUS[0]}
if [ "${PRC}" != "0" ] ; then
  echo "BAD: Docker build gave as bad exit code '${PRC}', see pba-docker-build.log which starts with full docker build call"
  exit 22
fi

echo "GOOD: Successfully built image ${IMAGE_NAME} with tag(s) ${IMAGE_TAGS}"

exit 0


