LINT_ARGS ?= --timeout 5m
GORELEASER_VERSION ?= v1.13.1
GORELEASER_ARGS ?= release --snapshot --rm-dist
GITCHLOG_ARGS ?=
SHELL := /bin/bash

BOUNCER_VERSION ?= 
GIT_COMMIT := $(shell git rev-parse --short HEAD)
DATE_VERSION := $(shell date +%Y.%-m.%-d)
FULL_VERSION := v$(DATE_VERSION)-$(GIT_COMMIT)$(if $(shell git diff --stat),-dirty,)

DOCKER_IMAGE_NAME ?= reg.cadoles.com/cadoles/bouncer
DOCKER_IMAGE_TAG ?= $(FULL_VERSION)

GOTEST_ARGS ?= -short

OPENWRT_DEVICE ?= 192.168.1.1

SIEGE_URLS_FILE ?= misc/siege/urls.txt
SIEGE_CONCURRENCY ?= 100

data/bootstrap.d/dummy.yml:
	mkdir -p data/bootstrap.d
	cp misc/bootstrap.d/dummy.yml data/bootstrap.d/dummy.yml

watch: tools/modd/bin/modd deps data/bootstrap.d/dummy.yml ## Watching updated files - live reload
	( set -o allexport && source .env && set +o allexport && tools/modd/bin/modd )

.PHONY: test
test: test-go ## Executing tests

test-go: deps
	( set -o allexport && source .env && set +o allexport && go test -v -count=1 $(GOTEST_ARGS) ./... )

build: build-bouncer ## Build artefacts

build-bouncer: deps ## Build executable
	CGO_ENABLED=0 go build \
		-v \
		-ldflags "\
			-X 'main.GitRef=$(shell git rev-parse --short HEAD)' \
			-X 'main.ProjectVersion=$(FULL_VERSION)' \
			-X 'main.BuildDate=$(shell date --utc --rfc-3339=seconds)' \
		" \
		-o ./bin/bouncer \
		./cmd/bouncer

.env:
	cp -f .env.dist .env

config.yml:
	bin/bouncer config dump > config.yml

run: .env
	( set -o allexport && source .env && set +o allexport && bin/bouncer $(BOUNCER_CMD))

.PHONY: deps
deps: .env

.PHONY: goreleaser
goreleaser: deps
	( set -o allexport && source .env && set +o allexport &&  curl -sfL https://goreleaser.com/static/run | VERSION=$(GORELEASER_VERSION) GORELEASER_CURRENT_TAG="$(FULL_VERSION)" bash /dev/stdin $(GORELEASER_ARGS) )

.PHONY: start-release
start-release:
	if [ -z "$(BOUNCER_VERSION)" ]; then echo "You must define environment variable BOUNCER_VERSION"; exit 1; fi
	
	git flow release start $(BOUNCER_VERSION)

	# Update package.json version
	jq '.version = "$(BOUNCER_VERSION)"' package.json | sponge package.json
	git add  package.json
	git commit -m "chore: bump to version $(BOUNCER_VERSION)" --allow-empty

	echo "Commit you additional modifications then execute 'make finish-release'"

.PHONY: finish-release
finish-release:
	git flow release finish -m "v$(BOUNCER_VERSION)"
	git push --all
	git push --tags

docker-build:
	docker build --pull -t $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) .
	docker tag $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) $(DOCKER_IMAGE_NAME):latest

docker-release:
	docker push $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
	docker push $(DOCKER_IMAGE_NAME):latest

gitea-release: tools/gitea-release/bin/gitea-release.sh goreleaser
	mkdir -p .gitea-release
	rm -rf .gitea-release/*

	cp dist/*.tar.gz .gitea-release/
	cp dist/*.apk .gitea-release/
	cp dist/*.deb .gitea-release/
	cp dist/*.rpm .gitea-release/

	GITEA_RELEASE_PROJECT="bouncer" \
		GITEA_RELEASE_ORG="Cadoles" \
		GITEA_RELEASE_BASE_URL="https://forge.cadoles.com" \
		GITEA_RELEASE_VERSION="$(FULL_VERSION)" \
		GITEA_RELEASE_NAME="$(FULL_VERSION)" \
		GITEA_RELEASE_COMMITISH_TARGET="$(GIT_COMMIT)" \
		GITEA_RELEASE_IS_DRAFT="false" \
		GITEA_RELEASE_BODY="" \
		GITEA_RELEASE_ATTACHMENTS="$$(find .gitea-release/* -type f)" \
		tools/gitea-release/bin/gitea-release.sh

grafterm: tools/grafterm/bin/grafterm
	tools/grafterm/bin/grafterm -c ./misc/grafterm/dashboard.json -v job=bouncer-proxy -r 5s

siege:
	$(eval TMP := $(shell mktemp))
	cat $(SIEGE_URLS_FILE) | envsubst > $(TMP)
	siege -i -b -c $(SIEGE_CONCURRENCY) -f $(TMP)
	rm -rf $(TMP)

tools/gitea-release/bin/gitea-release.sh:
	mkdir -p tools/gitea-release/bin
	curl --output tools/gitea-release/bin/gitea-release.sh https://forge.cadoles.com/Cadoles/Jenkins/raw/branch/master/resources/com/cadoles/gitea/gitea-release.sh
	chmod +x tools/gitea-release/bin/gitea-release.sh

tools/modd/bin/modd:
	mkdir -p tools/modd/bin
	GOBIN=$(PWD)/tools/modd/bin go install github.com/cortesi/modd/cmd/modd@latest

tools/grafterm/bin/grafterm:
	mkdir -p tools/grafterm/bin
	GOBIN=$(PWD)/tools/grafterm/bin go install github.com/slok/grafterm/cmd/grafterm@v0.2.0

bench:
	go test -bench=. -run '^$$' -count=10 ./...

tools/benchstat/bin/benchstat:
	mkdir -p tools/benchstat/bin
	GOBIN=$(PWD)/tools/benchstat/bin go install golang.org/x/perf/cmd/benchstat@latest

full-version:
	@echo $(FULL_VERSION)

.bouncer-token:
	bin/bouncer auth create-token --role writer --subject dev-writer > .bouncer-token

run-redis:
	docker kill bouncer-redis || exit 0
	docker run --rm -t \
		--name bouncer-redis \
		-v $(PWD)/data/redis:/data \
		-p 6379:6379 \
		redis:alpine3.17 \
		redis-server --save 60 1 --loglevel warning

redis-shell:
	docker exec -it \
		bouncer-redis \
		redis-cli

run-prometheus:
	docker kill bouncer-prometheus || exit 0
	docker run --rm -t \
		--name bouncer-prometheus \
		--network host \
		-v $(PWD)/misc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
		prom/prometheus