#!/usr/bin/make -f
SHELL = bash

REPO ?= $(shell go list -m)
VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop")

HUB_IMAGE ?= truecloudlab/frostfs
HUB_TAG ?= "$(shell echo ${VERSION} | sed 's/^v//')"

GO_VERSION ?= 1.21
LINT_VERSION ?= 1.56.1
TRUECLOUDLAB_LINT_VERSION ?= 0.0.5
PROTOC_VERSION ?= 25.0
PROTOC_GEN_GO_VERSION ?= $(shell go list -f '{{.Version}}' -m google.golang.org/protobuf)
PROTOGEN_FROSTFS_VERSION ?= $(shell go list -f '{{.Version}}' -m git.frostfs.info/TrueCloudLab/frostfs-api-go/v2)
PROTOC_OS_VERSION=osx-x86_64
ifeq ($(shell uname), Linux)
	PROTOC_OS_VERSION=linux-x86_64
endif
STATICCHECK_VERSION ?= 2023.1.6
ARCH = amd64

BIN = bin
RELEASE = release
DIRS = $(BIN) $(RELEASE)

# List of binaries to build.
CMDS = $(notdir $(basename $(wildcard cmd/frostfs-*)))
BINS = $(addprefix $(BIN)/, $(CMDS))

# .deb package versioning
OS_RELEASE = $(shell lsb_release -cs)
PKG_VERSION ?= $(shell echo $(VERSION) | sed "s/^v//" | \
			sed -E "s/(.*)-(g[a-fA-F0-9]{6,8})(.*)/\1\3~\2/" | \
			sed "s/-/~/")-${OS_RELEASE}

OUTPUT_LINT_DIR ?= $(abspath $(BIN))/linters
LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION)-v$(TRUECLOUDLAB_LINT_VERSION)
TMP_DIR := .cache
PROTOBUF_DIR ?= $(abspath $(BIN))/protobuf
PROTOC_DIR ?= $(PROTOBUF_DIR)/protoc-v$(PROTOC_VERSION)
PROTOC_GEN_GO_DIR ?= $(PROTOBUF_DIR)/protoc-gen-go-$(PROTOC_GEN_GO_VERSION)
PROTOGEN_FROSTFS_DIR ?= $(PROTOBUF_DIR)/protogen-$(PROTOGEN_FROSTFS_VERSION)
STATICCHECK_DIR ?= $(abspath $(BIN))/staticcheck
STATICCHECK_VERSION_DIR ?= $(STATICCHECK_DIR)/$(STATICCHECK_VERSION)

GOPLS_VERSION ?= v0.15.1
GOPLS_DIR ?= $(abspath $(BIN))/gopls
GOPLS_VERSION_DIR ?= $(GOPLS_DIR)/$(GOPLS_VERSION)

FROSTFS_CONTRACTS_PATH=$(abspath ./../frostfs-contract)
LOCODE_DB_PATH=$(abspath ./.cache/locode_db)
LOCODE_DB_VERSION=v0.4.0

.PHONY: help all images dep clean fmts fumpt imports test lint docker/lint
		prepare-release debpackage pre-commit unpre-commit

# To build a specific binary, use it's name prefix with bin/ as a target
# For example `make bin/frostfs-node` will build only storage node binary
# Just `make` will build all possible binaries
all: $(DIRS) $(BINS)

# help target
include help.mk

$(BINS): $(DIRS) dep
	@echo "⇒ Build $@"
	CGO_ENABLED=0 \
	go build -v -trimpath \
	-ldflags "-X $(REPO)/misc.Version=$(VERSION)" \
	-o $@ ./cmd/$(notdir $@)

$(DIRS):
	@echo "⇒ Ensure dir: $@"
	@mkdir -p $@

# Prepare binaries and archives for release
.ONESHELL:
prepare-release: docker/all
	@for file in `ls -1 $(BIN)/frostfs-*`; do
		cp $$file $(RELEASE)/`basename $$file`-$(ARCH)
		strip $(RELEASE)/`basename $$file`-$(ARCH)
		tar -czf $(RELEASE)/`basename $$file`-$(ARCH).tar.gz $(RELEASE)/`basename $$file`-$(ARCH)
	done

# Pull go dependencies
dep:
	@printf "⇒ Download requirements: "
	CGO_ENABLED=0 \
	go mod download && echo OK
	@printf "⇒ Tidy requirements : "
	CGO_ENABLED=0 \
	go mod tidy -v && echo OK

# Build export-metrics
export-metrics: dep
	@printf "⇒ Build export-metrics\n"
	CGO_ENABLED=0 \
	go build -v -trimpath -o bin/export-metrics ./scripts/export-metrics

# Regenerate proto files:
protoc:
	@if [ ! -d "$(PROTOC_DIR)" ] || [ ! -d "$(PROTOC_GEN_GO_DIR)" ] || [ ! -d "$(PROTOGEN_FROSTFS_DIR)" ]; then \
		make protoc-install; \
	fi
	@for f in `find . -type f -name '*.proto' -not -path './bin/*'`; do \
		echo "⇒ Processing $$f "; \
		$(PROTOC_DIR)/bin/protoc \
			--proto_path=.:$(PROTOC_DIR)/include:/usr/local/include \
			--plugin=protoc-gen-go=$(PROTOC_GEN_GO_DIR)/protoc-gen-go \
			--plugin=protoc-gen-go-frostfs=$(PROTOGEN_FROSTFS_DIR)/protogen \
			--go-frostfs_out=. --go-frostfs_opt=paths=source_relative \
			--go_out=. --go_opt=paths=source_relative \
			--go-grpc_opt=require_unimplemented_servers=false \
			--go-grpc_out=. --go-grpc_opt=paths=source_relative $$f; \
	done

# Install protoc
protoc-install:
	@rm -rf $(PROTOBUF_DIR)
	@mkdir $(PROTOBUF_DIR)
	@echo "⇒ Installing protoc... "
	@wget -q -O $(PROTOBUF_DIR)/protoc-$(PROTOC_VERSION).zip 'https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS_VERSION).zip'
	@unzip -q -o $(PROTOBUF_DIR)/protoc-$(PROTOC_VERSION).zip -d $(PROTOC_DIR)
	@rm $(PROTOBUF_DIR)/protoc-$(PROTOC_VERSION).zip
	@echo "⇒ Installing protoc-gen-go..."
	@GOBIN=$(PROTOC_GEN_GO_DIR) go install -v google.golang.org/protobuf/...@$(PROTOC_GEN_GO_VERSION)
	@echo "⇒ Instaling protogen FrostFS plugin..."
	@GOBIN=$(PROTOGEN_FROSTFS_DIR) go install -mod=mod -v git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/protogen@$(PROTOGEN_FROSTFS_VERSION)

# Build FrostFS component's docker image
image-%:
	@echo "⇒ Build FrostFS $* docker image "
	@docker build \
		--build-arg REPO=$(REPO) \
		--build-arg VERSION=$(VERSION) \
		--rm \
		-f .docker/Dockerfile.$* \
		-t $(HUB_IMAGE)-$*:$(HUB_TAG) .

# Build all Docker images
images: image-storage image-ir image-cli image-adm

# Build dirty local Docker images
dirty-images: image-dirty-storage image-dirty-ir image-dirty-cli image-dirty-adm

# Run `make %` in Golang container
docker/%:
	docker run --rm -t \
	-v `pwd`:/src \
	-w /src \
	-u "$$(id -u):$$(id -g)" \
	--env HOME=/src \
	golang:$(GO_VERSION) make $*


# Run all code formatters
fmts: fumpt imports

# Reformat imports
imports:
	@echo "⇒ Processing goimports check"
	@goimports -w cmd/ pkg/ misc/

# Run gofumpt
fumpt:
	@echo "⇒ Processing gofumpt check"
	@gofumpt -l -w cmd/ pkg/ misc/

# Run Unit Test with go test
test: GOFLAGS ?= "-count=1"
test:
	@echo "⇒ Running go test"
	@GOFLAGS="$(GOFLAGS)" go test ./...

# Run pre-commit
pre-commit-run:
	@pre-commit run -a --hook-stage manual

# Install linters
lint-install:
	@rm -rf $(OUTPUT_LINT_DIR)
	@mkdir $(OUTPUT_LINT_DIR)
	@mkdir -p $(TMP_DIR)
	@rm -rf $(TMP_DIR)/linters
	@git -c advice.detachedHead=false clone --branch v$(TRUECLOUDLAB_LINT_VERSION) https://git.frostfs.info/TrueCloudLab/linters.git $(TMP_DIR)/linters
	@@make -C $(TMP_DIR)/linters lib CGO_ENABLED=1 OUT_DIR=$(OUTPUT_LINT_DIR)
	@rm -rf $(TMP_DIR)/linters
	@rmdir $(TMP_DIR) 2>/dev/null || true
	@CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION)

# Run linters
lint:
	@if [ ! -d "$(LINT_DIR)" ]; then \
		make lint-install; \
	fi
	$(LINT_DIR)/golangci-lint run

# Install staticcheck
staticcheck-install:
	@rm -rf $(STATICCHECK_DIR)
	@mkdir $(STATICCHECK_DIR)
	@GOBIN=$(STATICCHECK_VERSION_DIR) go install honnef.co/go/tools/cmd/staticcheck@$(STATICCHECK_VERSION)

# Run staticcheck
staticcheck-run:
	@if [ ! -d "$(STATICCHECK_VERSION_DIR)" ]; then \
		make staticcheck-install; \
	fi
	@$(STATICCHECK_VERSION_DIR)/staticcheck ./...

# Install gopls
gopls-install:
	@rm -rf $(GOPLS_DIR)
	@mkdir $(GOPLS_DIR)
	@GOBIN=$(GOPLS_VERSION_DIR) go install golang.org/x/tools/gopls@$(GOPLS_VERSION)

# Run gopls
gopls-run:
	@if [ ! -d "$(GOPLS_VERSION_DIR)" ]; then \
		make gopls-install; \
	fi
	@if [[ $$(find . -type f -name "*.go" -print | xargs $(GOPLS_VERSION_DIR)/gopls check | tee /dev/tty | wc -l) -ne 0 ]]; then \
		exit 1; \
	fi

# Run linters in Docker
docker/lint:
	docker run --rm -t \
	-v `pwd`:/src \
	-u `stat -c "%u:%g" .` \
	--env HOME=/src \
	golangci/golangci-lint:v$(LINT_VERSION) bash -c 'cd /src/ && make lint'

# Activate pre-commit hooks
pre-commit:
	pre-commit install -t pre-commit -t commit-msg

# Deactivate pre-commit hooks
unpre-commit:
	pre-commit uninstall -t pre-commit -t commit-msg

# Print version
version:
	@echo $(VERSION)

# Delete built artifacts
clean:
	rm -rf .cache
	rm -rf $(BIN)
	rm -rf $(RELEASE)

# Package for Debian
debpackage:
	dch -b --package frostfs-node \
			--controlmaint \
			--newversion $(PKG_VERSION) \
			--distribution $(OS_RELEASE) \
			"Please see CHANGELOG.md for code changes for $(VERSION)"
	dpkg-buildpackage --no-sign -b

# Cleanup deb package build directories
debclean:
	dh clean

# Download locode database
locode-download:
	mkdir -p $(TMP_DIR)
	@wget -q -O ./$(TMP_DIR)/locode_db.gz 'https://git.frostfs.info/TrueCloudLab/frostfs-locode-db/releases/download/${LOCODE_DB_VERSION}/locode_db.gz'
	gzip -dfk ./$(TMP_DIR)/locode_db.gz

# Start dev environment
env-up: all
	docker compose -f dev/docker-compose.yml up -d
	@if [ ! -d "$(FROSTFS_CONTRACTS_PATH)" ]; then \
		echo "Frostfs contracts not found"; exit 1; \
	fi
	${BIN}/frostfs-adm --config ./dev/adm/frostfs-adm.yml morph init --contracts ${FROSTFS_CONTRACTS_PATH}
	${BIN}/frostfs-adm --config ./dev/adm/frostfs-adm.yml morph refill-gas --storage-wallet ./dev/storage/wallet.json --gas 10.0
	@if [ ! -f "$(LOCODE_DB_PATH)" ]; then \
		make locode-download; \
	fi

# Shutdown dev environment
env-down:
	docker compose -f dev/docker-compose.yml down
	docker volume rm -f frostfs-node_neo-go
	rm -f ./.cache/.frostfs-ir-state
	rm -f ./.cache/.frostfs-node-state
	rm -rf ./.cache/storage