# Copyright © 2026 Thomas von Dein

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#
# no need to modify anything below
tool      = esctl
VERSION   = $(shell grep VERSION config.go | head -1 | cut -d '"' -f2)
archs     = darwin freebsd linux windows
PREFIX    = /usr/local
UID       = root
GID       = 0
HAVE_POD := $(shell pod2text -h 2>/dev/null)

BRANCH      = $(shell git branch --show-current)
COMMIT      = $(shell git rev-parse --short=8 HEAD)
BUILD       = $(shell date +%Y.%m.%d.%H%M%S) 
GOVERSION   = $(shell grep -E "^go" go.mod | cut -d' ' -f2)
APIVERSION  = $(shell grep github.com/elastic/go-elasticsearch go.mod | cut -d' ' -f2)
APISPEC     = "https://www.elastic.co/docs/api/doc/elasticsearch.json"

all: buildlocal


# pkg/es/openspec.go:
# 	echo "package es" > pkg/es/openspec.go
# 	echo >> pkg/es/openspec.go
# 	echo "const Openspec = \`" >> pkg/es/openspec.go
# 	curl -o - $(APISPEC) | sed "s/\`/'/g" >> pkg/es/openspec.go
# 	echo "\`" >> pkg/es/openspec.go

assets/openapi.json:
	curl -o assets/openapi.json $(APISPEC)

buildlocal: assets/openapi.json
	CGO_LDFLAGS='-static' go build -tags osusergo,netgo -ldflags "-extldflags=-static" \
     -ldflags "-X codeberg.org/scip/esctl/pkg/cfg.BRANCH=$(BRANCH) \
               -X codeberg.org/scip/esctl/pkg/cfg.COMMIT=$(COMMIT) \
			   -X codeberg.org/scip/esctl/pkg/cfg.GOVERSION=$(GOVERSION) \
			   -X codeberg.org/scip/esctl/pkg/cfg.APIVERSION=$(APIVERSION) \
			   -X codeberg.org/scip/esctl/pkg/cfg.BUILD=$(BUILD)" \
     -o $(tool)
	strip $(tool)

install: buildlocal
	install -d -o $(UID) -g $(GID) $(PREFIX)/bin
	install -d -o $(UID) -g $(GID) $(PREFIX)/man/man1
	install -o $(UID) -g $(GID) -m 555 $(tool)  $(PREFIX)/sbin/
	install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/

clean:
	rm -rf $(tool) coverage.out testdata t/out pkg/es/openspec.go

test: clean buildlocal
	make -C t test

testlint: test lint

lint-basic:
	golangci-lint run --enable-only errcheck,govet,ineffassign,staticcheck,unused

lint:
	golangci-lint run --show-stats=false

testfuzzy: clean
	go test -fuzz ./... $(ARGS)

singletest:
	@echo "Call like this: make singletest TEST=TestPrepareColumns ARGS=-v"
	go test -run $(TEST) $(ARGS)

cover-report:
	go test ./... -cover -coverprofile=coverage.out
	go tool cover -html=coverage.out

goupdate:
	go get -t -u=patch ./...

buildall:
	./mkrel.sh $(tool) $(VERSION)

release:
	gh release create v$(VERSION) --generate-notes

show-versions: buildlocal
	@echo "### esctl version:"
	@./gfn -V

	@echo
	@echo "### go module versions:"
	@go list -m all

	@echo
	@echo "### go version used for building:"
	@grep -m 1 go go.mod

.PHONY: profile
profile: buildlocal
	./esctl api ls --profile-file cpu.profile
	go tool pprof -text esctl cpu.profile
	go tool pprof --http localhost:8888 ./esctl cpu.profile

docker-up:
	make -C t up

docker-waitup:
	make -C t up

docker-down:
	make -C t down
