
define HELPTEXT
Build Docker images for Redis Modules

make build          # Build Docker image
  OSNICK=nick         # Build for given OSNICK (default: as host)
  BRANCH=branch       # Build for tip of given branch (default: current branch)
  VERSION=version     # Build for give version tag
  OFFICIAL=1          # Create Docker image tags for official repo
  ARTIFACTS=1         # Copy atrifacts from container into local directory (default: 1)
  TAGFILE=file        # Output image names tags info `file`
  
make publish        # Push Docker image to remote repository (requires authentication)
  OSNICK=nick         # Publish for given OSNICK (default: as host)
  BRANCH=branch       # Publish for tip of given branch (default: current branch)
  VERSION=version     # Publish for give version tag
  OFFICIAL=1          # Publish Docker image tags for official repo
  TEST=1              # Run tests inside container

Additional options:
  NOP=1             # Just print commands, don't execute
  VERBOSE=1         # Print more information
  KEEP=1            # Don't delete temporary files
  DOCKERFILE=file   # Generate Dockerfile with given name

endef

ifneq ($(filter help,$(MAKECMDGOALS)),)
HELP=1
else

#----------------------------------------------------------------------------------------------

# defaults
PACK ?= 1
ARTIFACTS ?= 1
TEST ?= 0

DOCKER_ORG ?= redisfab
DOCKER_ORG.official ?= redislabs

DOCKER_TAGS=

ARCH:=$(shell $(READIES)/bin/platform --arch)

ifeq ($(OSNICK),)
OSNICK:=$(shell $(READIES)/bin/platform --osnick)
___:=$(info \# Running with OSNICK=$(OSNICK))
endif

# for artifact gathering
ART_DIR ?= $(ROOT)/bin/artifacts
ART_INT_DIR ?= /var/opt/redislabs/artifacts
export ART_DIR
export ART_INT_DIR

ifneq ($(TAGFILE),)
ifeq ($(filter /%,$(TAGFILE)),)
override TAGFILE:=$(ROOT)/$(TAGFILE)
endif
endif

#----------------------------------------------------------------------------------------------

ifeq ($(VERSION),)
	ifeq ($(BRANCH),)
		BRANCH:=$(shell git rev-parse --abbrev-ref HEAD | sed -e 's/\//_/g' 2> /dev/null)
		___:=$(info \# Running with BRANCH=$(BRANCH))
	endif
	ifeq ($(BRANCH),)
		___:=$(error Either VERSION or BRANCH must be set)
	endif
endif

ifneq ($(VERSION),)
	ifneq ($(BRANCH),)
		___:=$(error Only one of VERSION or BRANCH can be set)
	endif
endif

# e.g redisfab/RedisTimeSeries:edge
DEFAULT_TAG ?= $(DOCKER_ORG)/$(REPO):$(VERSPEC)$(VERSPEC_SUFFIX)-$(ARCH)-$(OSNICK)

#----------------------------------------------------------------------------------------------

ifneq ($(VERSION),)
	VERSPEC=$(VERSION)
	override VERSION:=$(shell echo "$(VERSION)" | sed -e 's/^v\(.*\)/\1/g')
endif

ifneq ($(BRANCH),)
	VERSPEC=$(BRANCH)
endif

#----------------------------------------------------------------------------------------------

ifeq ($(OFFICIAL),1)
	# if OSNICK and OSNICK.official match, then we're building the docker
	# that we should push upstream to dockerhub as edge/1.2/etc.

	#------------------------------------------------------------------------------------------
	ifneq ($(OSNICK.official),$(OSNICK))
		___:=$(info \# OFFICIAL=1 requested, but OSNICK=$(OSNICK) is not OSNICK.official=$(OSNICK.official))

	#------------------------------------------------------------------------------------------
	else # OSNICK.official

		#--------------------------------------------------------------------------------------
		ifneq ($(VERSION),)
			# since we cannot reliably deduce git branch from a version spec, 
			# we assume it's 'x.y' for version 'x.y*'
			VERSION_BRANCH:=$(shell echo "$(VERSION)" | cut -d '.' -f 1-2)

			DOCKER_TAGS += $(DOCKER_ORG.official)/$(REPO):$(VERSION)

			# LATEST tag - if LATEST_BRANCH in the makefile matches our ~version
			ifeq ($(LATEST_BRANCH),$(VERSION_BRANCH))
				# VERSPEC_SUFFIX allows for a custom latest (i.e redisai:latest-cpu)
				DOCKER_TAGS += $(DOCKER_ORG.official)/$(REPO):latest${VERSPEC_SUFFIX}
			endif

			# PREVIEW tag - if PREVIEW_BRANCH in the makefile matches our ~version
			ifeq ($(PREVIEW_BRANCH),$(VERSION_BRANCH))
				# VERSPEC_SUFFIX allows for a custom preview (i.e redisai:latest-cpu)
				DOCKER_TAGS += $(DOCKER_ORG.official)/$(REPO):preview$(VERSPEC_SUFFIX)
			endif
		endif # VERSION

		#--------------------------------------------------------------------------------------
		ifneq ($(BRANCH),)
			ifeq ($(BRANCH),master)
				DOCKER_TAGS += $(DOCKER_ORG.official)/$(REPO):edge$(VERSPEC_SUFFIX)
			endif

			ifeq ($(PREVIEW_BRANCH),$(BRANCH))
				DOCKER_TAGS += $(DOCKER_ORG.official)/$(REPO):preview$(VERSPEC_SUFFIX)
			endif
		endif # BRANCH

	endif # OSNICK.official
endif # OFFICIAL

#----------------------------------------------------------------------------------------------

# defaults for dockerama
NOP ?= 0
KEEP ?= 0
VERBOSE ?= 0

# There are mappings between OSNICK to specific OS values, due to docker containers used
# The global mappings are found here.

include $(READIES)/mk/osnick.defs
include $(READIES)/mk/help.defs

ifneq ($(DOCKERFILE),)
DOCKERFILE_ARG=-f $(DOCKERFILE)
endif

ifeq ($(AT),)
BUILD_DIR=$(ROOT)
else
BUILD_DIR=$(AT)
endif

MODULES_DIR=/usr/lib/redis/modules

define _DOCKER_VARS
	MODULES_DIR
	REDIS_VERSION
	OSNICK
	ARCH
	OS
	PACK
	ARTIFACTS
	TEST
endef
DOCKER_VARS += $(filter %,$(subst $(__NL), ,$(_DOCKER_VARS)))

define DOCKERAMA_CMD
NOP=$(NOP) \
KEEP=$(KEEP) \
$(DOCKERAMA_EXTRA_VARS) \
$(READIES)/bin/dockerama \
	$(DOCKERFILE_ARG) \
	$(foreach V,$(DOCKER_VARS),-d $(V)="$($(V))") \
	--build-dir "$(BUILD_DIR)" \
	--tag "$(DEFAULT_TAG)" \
	$(foreach T,$(DOCKER_TAGS),--extra-tags "$(T)") \
	--env-prefix REDIS \
	--dockeropts "$(DOCKER_OPTS)" \
	$(DOCKER_ARGS)
endef

build:
	@mkdir -p $(ART_DIR)/tests && echo 0 > $(ART_DIR)/tests/status
	@$(DOCKERAMA_CMD)
ifneq ($(TAGFILE),)
	echo "$(DEFAULT_TAG) $(DOCKER_TAGS)" | tr " " "\n" > $(TAGFILE)
endif
ifeq ($(ARTIFACTS),1)
	@IMAGE=$(DEFAULT_TAG) $(MK)/docker-collect-artifacts
	@set -e ;\
	for f in `ls $(ART_DIR)/tests/*.tgz 2> /dev/null`; do \
		tar -C $(ROOT) -xzf $$f ;\
	done
endif
	@E=`cat $(ART_DIR)/tests/status` ;\
	if [[ $$E != 0 ]]; then \
		>&2 echo "Tests failed - aborting." ;\
		false ;\
	fi

publish:
	@$(DOCKERAMA_CMD) --publish-only

#----------------------------------------------------------------------------------------------

endif # ! HELP

include $(MK)/help.defs
include $(MK)/help.rules
