TOPDIR=$(shell readlink -f .|sed -e 's/\/docker\/.*//')
MKINCLUDE=$(TOPDIR)/docker/mk
MKBUILD=$(TOPDIR)/docker/build

all: build

TAG ?= 6

REPOSITORY_BASE ?= http://osm-download.etsi.org/repository/osm/debian
RELEASE         ?= ReleaseFOUR-daily
REPOSITORY_KEY  ?= OSM%20ETSI%20Release%20Key.gpg
REPOSITORY      ?= testing
NO_CACHE        ?= --no-cache

LOWER_MDG = $(shell echo $(MDG) | tr '[:upper:]' '[:lower:]')

CONTAINER_NAME ?= $(LOWER_MDG)

CMD_DOCKER_ARGS ?= -q
DOCKER_ARGS     = $(CMD_DOCKER_ARGS)

DEPS := MON IM LCM RO common osmclient devops NBI policy-module Keystone N2VC lightui

DEPS_TARGETS = $(addprefix $(MKBUILD)/.dep_, $(DEPS))

Q=@

$(MKBUILD):
	$Qmkdir -p $(MKBUILD)

$(MKBUILD)/.dep_policy-module:
	$Q$(MKINCLUDE)/get_version.sh -r $(REPOSITORY) -R $(RELEASE) -k $(REPOSITORY_KEY) -u $(REPOSITORY_BASE) -m POL -p policy-module > $@

$(MKBUILD)/.dep_lightui:
	$Q$(MKINCLUDE)/get_version.sh -r $(REPOSITORY) -R $(RELEASE) -k $(REPOSITORY_KEY) -u $(REPOSITORY_BASE) -m LW-UI -p lightui > $@

$(MKBUILD)/.dep_%:
	$Q$(MKINCLUDE)/get_version.sh -r $(REPOSITORY) -R $(RELEASE) -k $(REPOSITORY_KEY) -u $(REPOSITORY_BASE) -m $* > $@

build: $(MKBUILD) $(DEPS_TARGETS)
	$Qdocker build -t opensourcemano/$(LOWER_MDG):$(TAG) \
                   --build-arg RELEASE=$(RELEASE) \
                   --build-arg REPOSITORY=$(REPOSITORY) \
                   --build-arg REPOSITORY_KEY=$(REPOSITORY_KEY) \
                   --build-arg REPOSITORY_BASE=$(REPOSITORY_BASE) \
                   --build-arg MON_VERSION==$(shell cat $(MKBUILD)/.dep_MON) \
                   --build-arg IM_VERSION==$(shell cat $(MKBUILD)/.dep_IM) \
                   --build-arg RO_VERSION==$(shell cat $(MKBUILD)/.dep_RO) \
                   --build-arg LCM_VERSION==$(shell cat $(MKBUILD)/.dep_LCM) \
                   --build-arg COMMON_VERSION==$(shell cat $(MKBUILD)/.dep_common) \
                   --build-arg OSMCLIENT_VERSION==$(shell cat $(MKBUILD)/.dep_osmclient) \
                   --build-arg NBI_VERSION==$(shell cat $(MKBUILD)/.dep_NBI) \
                   --build-arg POL_VERSION==$(shell cat $(MKBUILD)/.dep_policy-module) \
                   --build-arg DEVOPS_VERSION==$(shell cat $(MKBUILD)/.dep_devops) \
                   --build-arg N2VC_VERSION==$(shell cat $(MKBUILD)/.dep_N2VC) \
                   --build-arg LWUI_VERSION==$(shell cat $(MKBUILD)/.dep_lightui) \
                   $(DOCKER_ARGS) .

clean:
	rm -f $(MKBUILD)/.dep*

tag:
	docker tag opensourcemano/$(CONTAINER_NAME):$(INPUT_TAG) opensourcemano/$(LOWER_MDG):$(TAG)

push: tag
	docker push opensourcemano/$(LOWER_MDG):$(TAG)
