#!/bin/bash
#
# PBA docker clean 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
#
# Cleans the docker images specified by pba-docker-settings.txt from the local image cache.
# 

# params

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

for IMAGE_TAG in ${IMAGE_TAGS} ; do
  OLDIMAGE_ID=$(docker images --format "{{.ID}}" ${IMAGE_NAME}:${IMAGE_TAG})
  if [ -n "${OLDIMAGE_ID}" ] ; then
    echo "INFO: Removing old image ${IMAGE_NAME}:${IMAGE_TAG} with ID ${OLDIMAGE_ID} (though other tags might survive) ..."
    docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || exit 21
    echo "GOOD: Successfully removed image ${IMAGE_NAME}:${IMAGE_TAG}"
  else
    echo "GOOD: No image to remove with name:tag ${IMAGE_NAME}:${IMAGE_TAG}"
  fi
done

exit 0


