# Makefile
export REGISTRO=harbor.reymota.es/overleaf
VERSION_FILE = .version

# Get current version or default to tex-1
IMG_VERSION := $(shell cat $(VERSION_FILE) 2>/dev/null || echo "tex-1")
# Get built version for deployment targets
BUILT_VERSION := $(shell cat .current_version 2>/dev/null || echo "$(IMG_VERSION)")
export IMG_VERSION

export OVERLEAF_LATEST ?= $(REGISTRO)/sharelatex:$(IMG_VERSION)


all: build-cep

build-cep:
	@echo "Building image with version: $(IMG_VERSION)-cep"
	@# Store current version for other targets to use
	@echo "$(IMG_VERSION)" > .current_version
	docker build \
	  --file Dockerfile \
	  --tag $(OVERLEAF_LATEST)-cep \
	  .
	@# Increment version for next build
	@echo "$(IMG_VERSION)" | sed 's/tex-\([0-9]*\)/tex-\1/' | awk -F'-' '{print "tex-" $$2+1}' > $(VERSION_FILE)
	@echo "Next version will be: $$(cat $(VERSION_FILE))"


push:
	docker push $(REGISTRO)/sharelatex:$(BUILT_VERSION)-cep
