# This file is used to test if images pushed to the bluemix mdelder
# repository were correctly pushed by pulling the images back.
# It can be used in a travis build by using "make test_pull" as a script
# in the .travis.yml. If you do use it, be sure it's included in the Makefile
# as: include Makefile.test

# AppVersion is set as the AppVersion to be compiled into the controller binary.
# It's used as the default version of the 'acmesolver' image to use for ACME
# challenge requests, and any other future provider that requires additional
# image dependencies will use this same tag.
ifeq ($(APP_VERSION),)
APP_VERSION := $(if $(shell cat VERSION 2> /dev/null),$(shell cat VERSION 2> /dev/null),0.3.0)
endif

# Get a list of all binaries to be built
CMDS := $(shell find ./cmd/ -maxdepth 1 -type d -exec basename {} \; | grep -v cmd)
GIT_COMMIT := $(shell git rev-parse HEAD)
# docker_pull_controller, etc
DOCKER_PULL_TARGETS := $(addprefix docker_pull_, $(CMDS))
# docker_pull_individual_controller, etc
DOCKER_PULL_INDIVIDUAL_TARGETS := $(addprefix docker_pull_individual_, $(CMDS))

.PHONY: clean_images1 clean_images2 test_pull $(DOCKER_PULL_TARGETS) $(DOCKER_PULL_INDIVIDUAL_TARGETS)

test_pull: clean_images1 $(DOCKER_PULL_TARGETS) clean_images2 $(DOCKER_PULL_INDIVIDUAL_TARGETS)

clean_images1 clean_images2:
	docker 2>/dev/null 1>&2 rmi -f `docker images -q` || true
	docker images
	@echo "All images cleaned out."

$(DOCKER_PULL_TARGETS):
	$(eval DOCKER_PULL_CMD := $(subst docker_pull_,,$@))
	$(eval IMAGE_NAME := $(APP_NAME)-$(DOCKER_PULL_CMD))

	@echo "Pulling multi-arch image from old repository (mdelder)."
	docker pull $(IMAGE_REPO)/$(IMAGE_NAME):$(RELEASE_TAG)
	@echo "DONE"

	docker images

$(DOCKER_PULL_INDIVIDUAL_TARGETS):
	$(eval DOCKER_PULL_CMD := $(subst docker_pull_individual_,,$@))
	$(eval IMAGE_VERSION ?= $(APP_VERSION)-$(GIT_COMMIT))
	$(eval IMAGE_NAME := $(APP_NAME)-$(DOCKER_PULL_CMD))
	$(eval IMAGE_NAME_ARCH := $(IMAGE_NAME)$(IMAGE_NAME_ARCH_EXT))

	@echo "Pulling individual images from old repository (mdelder)."
	docker pull $(IMAGE_REPO)/$(IMAGE_NAME_ARCH):$(IMAGE_VERSION)
	@echo "DONE"

	docker images
