# chess-record makefile
# =====================

SHELL			:= /usr/bin/env bash

PROJECT_ID 		:= config
PROJECT_NAME	?= MouseSoft Config
BIN_SUFFIX		:=

TMPDIR			?= $(CURDIR)/tmp
OUTDIR			?= $(CURDIR)/out
BINDIR			?= $(OUTDIR)/bin

VERSION			?= $(strip $(shell ./scripts/version.sh))
VERSION_NUMBER	:= $(strip $(shell ./scripts/version.sh number))

BUILD_ARCH		?= $(shell go env GOARCH)
BUILD_OS			?= $(shell go env GOOS)

GOCMD			:= go
GOTEST			:= $(GOCMD) test
GOVET			:= $(GOCMD) vet
ECHO_CMD		:= echo -e

GREEN  := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE  := $(shell tput -Txterm setaf 7)
CYAN   := $(shell tput -Txterm setaf 6)
RESET  := $(shell tput -Txterm sgr0)

.DEFAULT_GOAL := all

version: ## Version of the project to be built
	@echo $(VERSION)
.PHONY:version

version-number: ## Version number of the project to be built
	@echo $(VERSION_NUMBER)
.PHONY:version-number

## Build

all: clean vendor test ## Build binary
.PHONY:all

vendor: ## Copy of all packages needed to support builds and tests in the vendor directory
	$(GOCMD) mod vendor
	@$(ECHO_CMD) "Vendor\t\t${GREEN}[OK]${RESET}"
.PHONY:vendor

clean: ## Remove build related files
	@rm -fr $(TMPDIR)
	@rm -fr $(OUTDIR)
	@$(ECHO_CMD) "Clean\t\t${GREEN}[OK]${RESET}"
.PHONY:clean

## Test

test: ## Run the tests of the project
ifeq ($(EXPORT_RESULT), true)
	@mkdir -p $(OUTDIR)
	$(eval OUTPUT_OPTIONS = | go-junit-report -set-exit-code > $(OUTDIR)/junit-report.xml)
endif
	$(GOTEST) -v $(GO_OPT) ./... $(OUTPUT_OPTIONS)
	@$(ECHO_CMD) "Test\t\t${GREEN}[OK]${RESET}"
.PHONY:test

coverage: ## Run the tests of the project and export the coverage report.
	@mkdir -p out
	$(GOTEST) -cover -covermode=count -coverprofile=$(OUTDIR)/profile.cov ./...
	$(GOCMD) tool cover -func $(OUTDIR)/profile.cov
ifeq ($(EXPORT_RESULT), true)
ifeq ($(COVERAGE_FORMAT), html)
	gocov convert $(OUTDIR)/profile.cov | gocov-html > $(OUTDIR)/coverage.html
else
	gocov convert $(OUTDIR)/profile.cov | gocov-xml > $(OUTDIR)/coverage.xml
endif
endif
	@$(ECHO_CMD) "Coverage\t${GREEN}[OK]${RESET}"
.PHONY:coverage

## Lint

lint: ## Run all available linters.
	go vet ./...
	errcheck ./...
	staticcheck ./...
#	usestdlibvars ./...
	shadow ./...
	@$(ECHO_CMD) "Lint\t\t${GREEN}[OK]${RESET}"
.PHONY:lint

GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
GOLANGCI_VERSION := v2.0.2

golangci-lint-install: ## Install golangci-lint util
	curl -sfL ${GOLANGCI_URL} | sh -s -- -b ${GOPATH}/bin ${GOLANGCI_VERSION}
.PHONY:lint-golangci-install

golangci-lint: ## Run golangci-lint linter
	@golangci-lint run
	@$(ECHO_CMD) "GolangCI Lint\t${GREEN}[OK]${RESET}"
.PHONY:golangci-lint

## Help

help: ## Show this help.
	@$(ECHO_CMD) ''
	@$(ECHO_CMD) 'Usage:'
	@$(ECHO_CMD) '  ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
	@$(ECHO_CMD) ''
	@$(ECHO_CMD) 'Targets:'
	@awk 'BEGIN {FS = ":.*?## "} { \
		if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf "    ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
		else if (/^## .*$$/) {printf "  ${CYAN}%s${RESET}\n", substr($$1,4)} \
		}' $(MAKEFILE_LIST)
.PHONY:help