# VERSION
# If no version is specified as a parameter of make, the last git hash
# value is taken.
VERSION?=$(shell git describe --abbrev=0)+$(shell date +'%Y%m%d%H%I%S')

# GOPROXY settings
# If no GOPROXY environment variable available, the pre-defined GOPROXY from go
# env to download and validate go modules is used. Exclude downloading and
# validation of all private modules which are pre-defined in GOPRIVATE. If no
# GOPRIVATE variable defined, the variable of go env is used.
GOPROXY?=$(shell go env GOPROXY)
GOPRIVATE?=$(shell go env GOPRIVATE)

# CONTAINER_RUNTIME
# The CONTAINER_RUNTIME variable will be used to specified the path to a
# container runtime. This is needed to start and run a container images.
CONTAINER_RUNTIME?=$(shell which docker)
CONTAINER_IMAGE_VERSION?=latest

# EXECUTABLE
EXECUTABLE=flucky

# BINARIES
# ==============================================================================
EXECUTABLE_TARGETS:= \
	bin/linux/amd64/${EXECUTABLE} \
	bin/linux/arm/5/${EXECUTABLE} \
	bin/linux/arm/7/${EXECUTABLE} \
	bin/tmp/${EXECUTABLE}

${EXECUTABLE}: bin/tmp/${EXECUTABLE}

bin/linux/amd64/${EXECUTABLE}: bindata
	CGO_ENABLED=1 \
	GOOS=linux \
	GOARCH=amd64 \
	GOPROXY=${GOPROXY} \
	GOPRIVATE=${GOPRIVATE} \
		go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}

bin/linux/arm/5/${EXECUTABLE}: bindata
	CGO_ENABLED=1 \
	GOOS=linux \
	GOARCH=arm \
	GOARM=5 \
	GOPROXY=${GOPROXY} \
	GOPRIVATE=${GOPRIVATE} \
		go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}

bin/linux/arm/7/${EXECUTABLE}: bindata
	CGO_ENABLED=1 \
	GOOS=linux \
	GOARCH=arm \
	GOARM=7 \
	GOPROXY=${GOPROXY} \
	GOPRIVATE=${GOPRIVATE} \
		go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}

bin/tmp/${EXECUTABLE}: bindata
	CGO_ENABLED=1 \
	GOPROXY=${GOPROXY} \
	GOPRIVATE=${GOPRIVATE} \
		go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}

# GO-BINDATA
# ==============================================================================
BINDATA_TARGETS := \
	pkg/repository/db/bindataSQL.go \

PHONY+=bindata
bindata: clean ${BINDATA_TARGETS}

pkg/repository/db/bindataSQL.go:
	go-bindata -pkg db -o ./pkg/repository/db/bindataSQL.go pkg/repository/db/postgres/*** pkg/repository/db/sqlite3/***

# CLEAN
# ==============================================================================
PHONY+=clean
clean:
	rm --force ${BINDATA_TARGETS} || true
	rm --force --recursive bin/ || true

# TEST
# ==============================================================================
PHONY+=test/unit
test/unit: bindata
	go test -v -race -coverprofile=coverage.txt -covermode=atomic -timeout 600s -count=1 ./pkg/...

PHONY+=test/integration
test/integration:
	go test -v -count=1 -timeout 600s ./it/...

test/coverage: test/unit
	go tool cover -html=coverage.txt

# CONTAINER IMAGE STEPS
# ==============================================================================
PHONY+=container-image/build/amd64
container-image/build/amd64:
	${CONTAINER_RUNTIME} build \
		--build-arg BASE_IMAGE=docker.io/library/alpine:3.11.2 \
		--build-arg BUILD_IMAGE=docker.io/volkerraschek/build-image:latest \
		--build-arg EXECUTABLE=${EXECUTABLE} \
		--build-arg EXECUTABLE_TARGET=bin/linux/amd64/${EXECUTABLE} \
		--build-arg GOPROXY=${GOPROXY} \
		--build-arg GOPRIVATE=${GOPRIVATE} \
		--build-arg VERSION=${VERSION} \
		--file Dockerfile \
		--no-cache \
		--tag docker.io/volkerraschek/flucky:${CONTAINER_IMAGE_VERSION} \
		--tag volkerraschek/flucky:${CONTAINER_IMAGE_VERSION} \
		.

PHONY+=container-image/push/amd64
container-image/push/amd64: container-image/build/amd64
	${CONTAINER_RUNTIME} login docker.io \
		--username ${CONTAINER_IMAGE_REGISTRY_USER} \
		--password ${CONTAINER_IMAGE_REGISTRY_PASSWORD}
	${CONTAINER_RUNTIME} push docker.io/volkerraschek/flucky:${CONTAINER_IMAGE_VERSION}

# CONTAINER STEPS - BINARY
# ==============================================================================
# current os
PHONY+=container-run/${EXECUTABLE}
container-run/${EXECUTABLE}:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

# build all binaries for any operating system
PHONY+=container-run/all
container-run/all:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

PHONY+=${EXECUTABLE_TARGETS:%=container-run/%}
${EXECUTABLE_TARGETS:%=container-run/%}:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

# CONTAINER STEPS - GO-BINDATA
# ==============================================================================
PHONY+=container-run/bindata
container-run/bindata:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

# CONTAINER STEPS - CLEAN
# ==============================================================================
PHONY+=container-run/clean
container-run/clean:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

# CONTAINER STEPS - TEST
# ==============================================================================
PHONY+=container-run/test/unit
container-run/test/unit:
	$(MAKE) container-run COMMAND=${@:container-run/%=%}

# GENERAL CONTAINER COMMAND
# ==============================================================================
PHONY+=container-run
container-run:
	${CONTAINER_RUNTIME} run \
		--rm \
		--volume ${PWD}:/workspace \
		--volume /var/run/docker.sock:/var/run/docker.sock \
		docker.io/volkerraschek/build-image:latest \
			make ${COMMAND} \
				GOPROXY=${GOPROXY} \
				GOPRIVATE=${GOPRIVATE} \
				VERSION=${VERSION:v%=%}

# REMOTE
# ==============================================================================
PHONY+=${FLUCKY_REMOTE:%=remote/%}
remote/${FLUCKY_REMOTE}: bin/linux/arm/7/${EXECUTABLE}
	scp bin/linux/arm/7/${EXECUTABLE} root@${FLUCKY_REMOTE}:/usr/local/bin/${EXECUTABLE}
	# ssh root@${FLUCKY_REMOTE} 'mkdir --parent /etc/bash_completion.d || true'
	# ssh root@${FLUCKY_REMOTE} 'flucky completion bash > /etc/bash_completion.d/flucky.sh && chmod +x /etc/bash_completion.d/flucky.sh'
	# ssh root@${FLUCKY_REMOTE} 'flucky completion zsh > /etc/bash_completion.d/flucky.zsh && chmod +x /etc/bash_completion.d/flucky.zsh'
	ssh root@${FLUCKY_REMOTE} 'chmod +x /usr/local/bin/${EXECUTABLE}'

# PHONY
# ==============================================================================
# Declare the contents of the PHONY variable as phony.  We keep that information
# in a variable so we can use it in if_changed.
.PHONY: ${PHONY}