#!/bin/bash

if [[ -n ${PHP_VERSION} ]]; then
  IMAGEID=$(docker image ls -q registry.lakedrops.com/docker/l3d/php-${PHP_VERSION}:${VERSION})
  echo "Updating the image ..."
  STATUS=$(docker pull registry.lakedrops.com/docker/l3d/php-${PHP_VERSION}:${VERSION})
  if [[ "$STATUS" == *"Status: Image is up to date"* ]]; then
    echo "Already up to date"
  else
    echo "Image updated"
    while true; do
      ID=$(docker container ls -q -f ancestor=${IMAGEID})
      if [[ -n ${ID} ]]; then
        echo "Removing outdated container ..."
        docker stop ${ID}
        docker rm ${ID}
      else
        break
      fi
    done
  fi
fi