# Set an output prefix, which is the local directory if not specified
PREFIX?=$(shell pwd)
.DEFAULT_GOAL := build

# Version tag
VERSION := 1.0

# Setup name variables for the package/tool
NAME := nos-comptes
BIN_NAME := noscomptes
PKG := github.com/kratisto/nos-comptes

DOCKER_IMAGE_NAME := $(NAME)
# GO env vars
ifeq ($(GOPATH),)
	GOPATH:=~/go
endif
GO=$(firstword $(subst :, ,$(GOPATH)))

.PHONY: deps
deps: ## Get all vendor dependencies
	@echo "+ $@"
	go mod vendor

.PHONY: clean
clean: local-clean ## Clean your generated files
	@echo "+ $@"
	docker image rm poketools:$(VERSION) || true

.PHONY: local-build
local-build: local-clean local-format ## Build locally the binary
	@echo "+ $@"
	go build -o $(GO)/bin/$(BIN_NAME) .

.PHONY: local-clean
local-clean: ## Cleanup locally any build binaries or packages
	@echo "+ $@"
	@$(RM) $(BIN_NAME)

.PHONY: local-format
local-format: ## format locally all files
	@echo "+ $@"
	gofmt -s -l -w .

.PHONY: build
build: sources-image ## Build the docker image with application binary
	@echo "+ $@"
	docker build --no-cache \
				 -f containers/Dockerfile \
				 --build-arg SOURCES_IMAGE=$(NAME)-sources:$(VERSION) \
				 -t poketools:$(VERSION) .

GIT_CREDENTIALS?=$(shell cat ~/.git-credentials 2> /dev/null)
.PHONY: sources-image
sources-image: ## Generate a Docker image with only the sources
	@echo "+ $@"
	docker build -t $(NAME)-sources:$(VERSION) -f containers/Dockerfile.sources .


.PHONY: local-run-dependencies
local-run-dependencies: ## Run the dependencies of the server (launch the containers/docker-compose.local.yml)
	@echo "+ $@"
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.local.yml down || true;
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.local.yml pull;
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.local.yml up -d --build

.PHONY: local-run-golang
local-run-golang: ## Build the server and run it
	@echo "+ $@"
	BUILD_GOLANG_CMD=local-build \
	LAUNCH_GOLANG_CMD="$(GO)/bin/$(BIN_NAME)" \
	$(MAKE) local-launch-golang

.PHONY: local-launch-golang
local-launch-golang: ## Build the server and run it
	@echo "+ $@"
	PID=`ps -ax | egrep "\b$(BIN_NAME)"| cut -d " " -f 1`; \
		kill $$PID || true
	$(MAKE) $(BUILD_GOLANG_CMD)
	DB_PORT=`docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.local.yml port database 5432  | cut -f2 -d':'`; \
		$(LAUNCH_GOLANG_CMD) serve --port 8081 --loglevel debug  --logformat text --dbconnectionuri "postgresql://postgres:postgres@localhost:$$DB_PORT/nos_comptes?sslmode=disable"

.PHONY: local-run
local-run: local-run-dependencies local-run-golang ## Run the server with its dependencies

.PHONY: local-run-app
local-run-app: ## Run the app of the server (launch the containers/docker-compose.yml)
	@echo "+ $@"
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.yml down || true;
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.yml pull;
	@docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.yml up -d --build

.PHONY: bdd-test
bdd-test: local-run-app
	go get github.com/DATA-DOG/godog/cmd/godog
	DB_PORT=`docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.yml port database 5432  | cut -f2 -d':'`;
	API_PORT=`docker-compose -p $(DOCKER_IMAGE_NAME)-uuid -f containers/docker-compose.yml port api 8080  | cut -f2 -d':'`;
	$(GOPATH)/bin/godog


