-include environ.inc
.PHONY: deps dev build install image release test clean tr tr-merge

export CGO_ENABLED=0
VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION")
COMMIT=$(shell git rev-parse --short HEAD || echo "$COMMIT")
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GOCMD=go
GOVER=$(shell go version | grep -o -E 'go1\.17\.[0-9]+')

DESTDIR=/usr/local/bin

ifeq ($(BRANCH), master)
IMAGE := prologic/msgbus
TAG := latest
else
IMAGE := prologic/msgbus
TAG := dev
endif

all: preflight build

preflight:
	@./preflight.sh

deps:

dev : DEBUG=1
dev : build
	@./msgbus -v
	@./msgbusd -v

cli:
	@$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \
		-ldflags "-w \
		-X $(shell go list).Version=$(VERSION) \
		-X $(shell go list).Commit=$(COMMIT)" \
		./cmd/msgbus/...

server: generate
	@$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \
		-ldflags "-w \
		-X $(shell go list).Version=$(VERSION) \
		-X $(shell go list).Commit=$(COMMIT)" \
		./cmd/msgbusd/...

build: cli server

generate:
	@if [ x"$(DEBUG)" = x"1"  ]; then		\
	  echo 'Running in debug mode...';	\
	fi

install: build
	@install -D -m 755 msgbusd $(DESTDIR)/msgbusd
	@install -D -m 755 msgbus $(DESTDIR)/msgbus

ifeq ($(PUBLISH), 1)
image: generate
	@docker build --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" -t $(IMAGE):$(TAG) .
	@docker push $(IMAGE):$(TAG)
else
image: generate
	@docker build --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" -t $(IMAGE):$(TAG) .
endif

release: generate
	@./tools/release.sh

fmt:
	@$(GOCMD) fmt ./...

test:
	@CGO_ENABLED=1 $(GOCMD) test -d -v -cover -race -timeout 30s ./...

coverage:
	@CGO_ENABLED=1 $(GOCMD) test -d -v -cover -race -timeout 30s -cover -coverprofile=coverage.out  ./...
	@$(GOCMD) tool cover -html=coverage.out

bench:
	@CGO_ENABLED=1 $(GOCMD) test -d -v -cover -race -timeout 30s -cover -coverprofile=coverage.out  ./...
	@CGO_ENABLED=1 $(GOCMD) test -race -benchtime=1x -cpu 16 -benchmem -bench "^(Benchmark)" .

clean:
	@git clean -f -d -X

tr:
	@goi18n merge -outdir ./internal/langs ./internal/langs/active.*.toml

tr-merge:
	@goi18n merge -outdir ./internal/langs ./internal/langs/active.*.toml ./internal/langs/translate.*.toml 
