#!/bin/bash
set -e

# image lists for different archs that should match what's in the Dockerfile (minus the explicit images IDs)
case "$DOCKER_ENGINE_OSARCH" in
	linux/arm)
		images=(
			hypriot/armhf-busybox:latest
			hypriot/armhf-hello-world:latest
			hypriot/armhf-unshare:latest
		)
		;;
	linux/ppc64le)
		images=(
			ppc64le/busybox:latest
			ppc64le/hello-world:frozen
			ppc64le/unshare:latest
		)
		;;
	linux/s390x)
		images=(
			s390x/busybox:latest
			s390x/hello-world:frozen
			s390x/unshare:latest
		)
		;;
	*)
		images=(
			busybox:latest
			debian:jessie
			hello-world:latest
			jess/unshare:latest
		)
		;;
esac

if ! docker inspect "${images[@]}" &> /dev/null; then
	hardCodedDir='/docker-frozen-images'
	if [ -d "$hardCodedDir" ]; then
		# Do not use a subshell for the following command. Windows CI
		# runs bash 3.x so will not trap an error in a subshell.
		# http://stackoverflow.com/questions/22630363/how-does-set-e-work-with-subshells
		set -x; tar -cC "$hardCodedDir" . | docker load; set +x
	else
		dir="$DEST/frozen-images"
		# extract the exact "RUN download-frozen-image-v2.sh" line from the Dockerfile itself for consistency
		# NOTE: this will fail if either "curl" or "jq" is not installed or if the Dockerfile is not available/readable
		awk '
			$1 == "RUN" && $2 == "./contrib/download-frozen-image-v2.sh" {
				for (i = 2; i < NF; i++)
					printf ( $i == "'"$hardCodedDir"'" ? "'"$dir"'" : $i ) " ";
				print $NF;
				if (/\\$/) {
					inCont = 1;
					next;
				}
			}
			inCont {
				print;
				if (!/\\$/) {
					inCont = 0;
				}
			}
		' ${DOCKER_FILE:="Dockerfile"} | sh -x
		# Do not use a subshell for the following command. Windows CI
		# runs bash 3.x so will not trap an error in a subshell.
		# http://stackoverflow.com/questions/22630363/how-does-set-e-work-with-subshells
		set -x; tar -cC "$dir" . | docker load; set +x
	fi
fi

# tag images to ensure that all integrations work with the defined image names
# then remove original tags as these make problems with later tests (e.g., TestInspectApiImageResponse)
case "$DOCKER_ENGINE_OSARCH" in
	linux/arm)
		docker tag hypriot/armhf-busybox:latest busybox:latest
		docker tag hypriot/armhf-hello-world:latest hello-world:frozen
		docker tag hypriot/armhf-unshare:latest jess/unshare:latest
		docker rmi hypriot/armhf-busybox:latest
		docker rmi hypriot/armhf-hello-world:latest
		docker rmi hypriot/armhf-unshare:latest
		;;
	linux/ppc64le)
		docker tag ppc64le/busybox:latest busybox:latest
		docker tag ppc64le/hello-world:frozen hello-world:frozen
		docker tag ppc64le/unshare:latest jess/unshare:latest
		docker rmi ppc64le/busybox:latest
		docker rmi ppc64le/hello-world:frozen
		docker rmi ppc64le/unshare:latest
		;;
	linux/s390x)
		docker tag s390x/busybox:latest busybox:latest
		docker tag s390x/hello-world:frozen hello-world:frozen
		docker tag s390x/unshare:latest jess/unshare:latest
		docker rmi s390x/busybox:latest
		docker rmi s390x/hello-world:frozen
		docker rmi s390x/unshare:latest
		;;
	*)
		docker tag hello-world:latest hello-world:frozen
		;;
esac
