# TODO: change project name
PROJECT = 
MAIN = cmd/main.go
INTERNAL := $(shell find internal -name *.go -print)
DOCFILES := $(shell find docs -name *.html -print)
GIT= gitlab.wtotem.net/webtotem
REGISTRY=harbor.wtotem.net/webtotem
BIN=bin/$(PROJECT)
COMPILEFLAGS= -v -ldflags '-w -s'

# check - special target for checking necessary environment variable
check:
ifndef PROJECT
	$(error PROJECT is undefined)
endif

# build - target for building go application
build: check $(BIN)

$(BIN): $(MAIN) $(INTERNAL)
	go build $(COMPILEFLAGS) -o $(BIN) $(MAIN)

# docker - target for building docker image with go application
# Need WT_TAG environment variable
docker: check
ifndef WT_TAG
	$(error WT_TAG is undefined)
endif
	docker build -t $(REGISTRY)/$(PROJECT):$(WT_TAG) -f build/Dockerfile .

# push - target for pushing docker image into registry
push: check
ifndef WT_TAG
	$(error WT_TAG is undefined)
endif
	docker push $(REGISTRY)/$(PROJECT):$(WT_TAG)

# clean - target for cleaning project
clean: check clean-docs
	go clean
	rm -rf bin/$(PROJECT)

# docs - target for generating project documentation
# if your code placed not only in internal directory then you need to add more dependencies in this targets 
docs: check docs/internal

docs/internal: $(INTERNAL)
	GIT=$(GIT) PROJECT=$(PROJECT) TARGET=internal ./scripts/docs.sh

# clean-docs - target for cleaning all automatic generated documentation
# if your code placed not only in internal directory then you need to add more dependencies in this targets 
clean-docs: check clean-docs-internal

clean-docs-internal: check
	rm -rf docs/internal

lint: check
	golangci-lint run -E revive -E gosec -E stylecheck -E bodyclose -E gofmt -E unparam -E sqlclosecheck

test: check
	go test -v ./...