FORCE_REBUILD ?= 0
OPENAGER_RELEASE ?= stable
OPENAGER_BUILD ?= latest
OPENAGER_REPO ?= openager
OPENAGER_SERICES ?= nodered

BUILD_ARGS := --build-arg OPENAGER_REPO=$(OPENAGER_REPO)

ifeq ($(FORCE_REBUILD), 1)
  BUILD_ARGS := $(BUILD_ARGS) --no-cache
endif


all:	build-all

release: tag-all push-all

build:
	$(MAKE) BUILD_ARGS="$(BUILD_ARGS)" OPENAGER_REPO="$(OPENAGER_REPO)" OPENAGER_RELEASE="$(OPENAGER_RELEASE)" -C $(OPENAGER_SERICE) build

tag:
	docker tag $(OPENAGER_REPO)/$(OPENAGER_SERICE):latest $(OPENAGER_REPO)/$(OPENAGER_SERICE):$OPENAGER_BUILD)

push:
	docker push $(OPENAGER_REPO)/$(OPENAGER_SERICE):latest
	docker push $(OPENAGER_REPO)/$(OPENAGER_SERICE):$OPENAGER_BUILD)

%-all:
	@$(foreach SERVICE, $(OPENAGER_SERICES), $(MAKE) --no-print-directory OPENAGER_SERICE=$(SERVICE) $(subst -all,;,$@))

clean:
	docker-compose stop
	docker-compose rm
	docker network prune

prepare:
	docker pull debian:stretch-slim
	docker pull etherpad/etherpad
	FORCE_REBUILD=1 $(MAKE)

.PHONY: all build tag push clean prepare
