# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

include Makefile.common

.PHONY: help all clean dev check checklist tidy

vendor:
	go mod vendor

push: vendor ci check-dirty ## Push the current branch to remote server
	git push origin $$(git rev-parse --abbrev-ref HEAD)

branch:  ## Create a new branch and checkout it. Usage: make branch (interactive)
	@./tools/branch.sh

dev: checklist check

# Note from Fede:
# See https://github.com/golang/go/issues/44129
# If issues run: go env -w GOFLAGS=-mod=mod
# Run swag just in case
ci: swag fmt tidy check ## Run formatters (could modify the code), checks and linters

check: check-static dependencies-check ## Run checks and linters

download-versions:
	curl --url "https://app.quantex.com.ar/linter_version.txt" -o $(linter_version_file)

test: ## Run all the tests
	go test ./...

VALID_ENV_TYPES := prod dev demo open-demo
check-env:
	@if [ "$(filter $(e),$(VALID_ENV_TYPES))" != "$(e)" ]; then \
		echo "\033[31mError: Invalid environment type $(e). Types can be: prod, dev, demo, open-demo"; \
		exit 1; \
	fi;
	# set dev as default
	$(eval e := dev)

print-version: check-env ## Print the version of the latest build
	OUT_PATH=$(DEFAULT_OUT_PATH) tools/print-version.sh $(e)

build: check-env swag vendor only-build # Build a native version. Set e=environment: prod, dev, demo, open-demo

only-build: check-env
	@echo "Building for $(e) environment..."
	env OUT_PATH=$(DEFAULT_OUT_PATH) GOARCH=amd64 GOOS=linux tools/build.sh $(e)

linux-build: check-env swag # Build a linux version for prod environment. Set e=environment: prod, dev, demo, open-demo
	env OUT_PATH=$(DEFAULT_OUT_PATH) GOARCH=amd64 GOOS=linux tools/build.sh $(e)

deploy: # Deploy to remote server. Set e=environment: prod, dev, demo, open-demo; s=serverName; i=instance; e.g. make deploy e=dev s=nonprodFix i=dpl
	make build e=$(e) && qscp build/out/distribution/qfixdpl.gz $(s):/home/quantex/qfixtb/dpl/

open-demo:
	make deploy e=open-demo s=nonprodFix

fmt: download-versions # Apply the Go formatter to the code
	cd tools/check; unset GOPATH; GOBIN=$$PWD/../bin go install mvdan.cc/gofumpt@$(call get_version,gofumpt);
	@echo "running fmt..."
	@echo $(GIT_TREE_STATE)
	@echo "> running gofumpt..."
	@tools/bin/gofumpt -l -w main.go 2>&1
	@tools/bin/gofumpt -l -w $(FILES) 2>&1

swag: download-versions
	@echo "installing swag..."
	cd tools/check; unset GOPATH; GOBIN=$$PWD/../bin go install github.com/swaggo/swag/cmd/swag@$(call get_version,swag);
	@echo "running swag..."
	rm -f src/client/api/rest/docs/swagger.json
	rm -f src/client/api/rest/docs/swagger.yaml
	rm -f src/client/api/rest/docs/docs.go
	mkdir -p src/client/api/rest/docs
	echo "package docs" > src/client/api/rest/docs/docs.go   # This empty file is needed for the initial build. Then is overwritten by Swag.
	tools/bin/swag init --parseDependency -g server.go -d src/client/api/rest -o src/client/api/rest/docs/

check-static: download-versions
	@echo "installing golangci-lint..."
	cd tools/check; unset GOPATH; GOBIN=$$PWD/../bin go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(call get_version,golangci-lint);
	@echo "running golangci-lint..."
	@echo $$($(PACKAGE_DIRECTORIES))

	tools/bin/golangci-lint run -v -c tools/check/golangci.yml --timeout 5m0s ./...

check-dirty:
	$(if $(filter dirty,$(GIT_TREE_STATE)), $(error git state is not clean <$(GIT_TREE_STATE)>))

dependencies-check: download-versions
	@echo "installing govulncheck..."
	cd tools/check; unset GOPATH; GOBIN=$$PWD/../bin go install golang.org/x/vuln/cmd/govulncheck@$(call get_version,govulncheck);
	@echo "running govulncheck..."
	@tools/bin/govulncheck -show verbose ./...

gogenerate: download-versions
	@echo "installing go-enum..."
	cd tools/check; unset GOPATH; GOBIN=$$PWD/../bin go install github.com/abice/go-enum@$(call get_version,go-enum);
	@echo "running go generate ./.."
	./tools/check/check-gogenerate.sh

tidy:
	@echo "running go mod tidy..."
	./tools/check/check-tidy.sh

server_coverage:
ifeq ($(TARGET), "")
	$(GOBUILDCOVERAGE) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(COVERAGE_SERVER_LDFLAGS) $(CHECK_FLAG)' -o ../bin/bitlab-coverage
else
	$(GOBUILDCOVERAGE) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(COVERAGE_SERVER_LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)'
endif

checklist:
	cat checklist.md

linter_version_file = .linter_version.txt

define get_version
$(shell grep "^$(1):" $(linter_version_file) | cut -d: -f2 | tr -d ' ')
endef

init: ## Install dependencies to run the project
	make swag
	make gogenerate
	go install ./...

# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help
