LINT_ARGS ?= --timeout 5m
GORELEASER_ARGS ?= release --snapshot --clean
GITCHLOG_ARGS ?=
SHELL := /bin/bash

EMISSARY_VERSION ?= 

DOCKER_IMAGE_NAME ?= reg.cadoles.com/cadoles/emissary
DOCKER_IMAGE_TAG ?= $(MKT_PROJECT_VERSION)

GOTEST_ARGS ?= -short

OPENWRT_DEVICE ?= 192.168.1.1

watch: deps ## Watching updated files - live reload
	( set -o allexport && source .env && set +o allexport && go run -mod=readonly github.com/cortesi/modd/cmd/modd@latest )

clean:
	rm -f .emissary-*
	rm -f emissary.sqlite*
	rm -f server-key.json
	rm -f agent-key.json
	rm -f state.json

.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) ./... )

test-install-script: tools/bin/bash_unit
	tools/bin/bash_unit ./misc/script/test_install.sh

tools/bin/bash_unit:
	mkdir -p tools/bin
	cd tools/bin && bash <(curl -s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh)	


lint: ## Lint sources code
	golangci-lint run --enable-all $(LINT_ARGS)

build: build-emissary ## Build artefacts

build-emissary: build-emissary-server build-emissary-agent

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

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

tmp/server.yml:
	mkdir -p tmp
	bin/server server config dump > tmp/server.yml

tmp/agent.yml:
	mkdir -p tmp
	bin/agent agent config dump > tmp/agent.yml

run-emissary-%: .env
	( set -o allexport && source .env && set +o allexport && bin/$* $(EMISSARY_CMD))

.PHONY: deps
deps: .env .mktools

.PHONY: dump-config
dump-config: build-emissary
	mkdir -p tmp
	./bin/emissary config dump > tmp/config.yml

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

install-git-hooks:
	git config core.hooksPath .githooks

docker-build: docker-build-agent docker-build-server

docker-build-%:
	docker build --target emissary-$* -t $(DOCKER_IMAGE_NAME)-$*:latest .

docker-release: docker-release-agent docker-release-server

docker-release-%:
	docker tag $(DOCKER_IMAGE_NAME)-$*:latest $(DOCKER_IMAGE_NAME)-$*:$(DOCKER_IMAGE_TAG)
	docker push $(DOCKER_IMAGE_NAME)-$*:latest
	docker push $(DOCKER_IMAGE_NAME)-$*:$(DOCKER_IMAGE_TAG)

deploy-openwrt-agent:
	$(MAKE) GOARCH="arm" GORELEASER_ARGS='build --single-target --snapshot --clean' goreleaser

	ssh root@$(OPENWRT_DEVICE) mkdir -p /etc/emissary
	ssh root@$(OPENWRT_DEVICE) mkdir -p /var/lib/emissary
	scp misc/packaging/common/config-agent.yml root@$(OPENWRT_DEVICE):/etc/emissary/agent.yml
	scp misc/packaging/openwrt/emissary-agent.init.sh root@$(OPENWRT_DEVICE):/etc/init.d/emissary-agent
	ssh root@$(OPENWRT_DEVICE) chmod +x /etc/init.d/emissary-agent
	ssh root@$(OPENWRT_DEVICE) mkdir -p /usr/share/emissary
	ssh root@$(OPENWRT_DEVICE) /etc/init.d/emissary-agent enable
	ssh root@$(OPENWRT_DEVICE) /etc/init.d/emissary-agent stop
	scp dist/emissary-agent_linux_arm_6/emissary root@$(OPENWRT_DEVICE):/usr/bin/emissary
	ssh root@$(OPENWRT_DEVICE) /etc/init.d/emissary-agent start

gitea-release: .mktools tools/gitea-release/bin/gitea-release.sh goreleaser changelog
	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 CHANGELOG.md .gitea-release/

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

.emissary-tenant: .emissary-admin-token
	$(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client tenant create --token-file .emissary-admin-token --tenant-label Dev -f json | jq -r '.[0].id' > .emissary-tenant"

.emissary-admin-token:
	$(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml server auth create-token --role admin --output .emissary-admin-token"

.emissary-token: .emissary-tenant
	$(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml server auth create-token --role writer --output .emissary-token --tenant $(shell cat .emissary-tenant)"

AGENT_ID ?= 1

claim-agent: .emissary-token
	$(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client agent claim --agent-thumbprint $(shell go run ./cmd/agent agent show-thumbprint)"

load-sample-specs: .emissary-token
	cat misc/spec-samples/app.emissary.cadoles.com.json | $(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client agent spec update -a $(AGENT_ID) --no-patch --spec-data - --spec-name app.emissary.cadoles.com"
	cat misc/spec-samples/proxy.emissary.cadoles.com.json | $(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client agent spec update -a $(AGENT_ID) --no-patch --spec-data - --spec-name proxy.emissary.cadoles.com"
	cat misc/spec-samples/mdns.emissary.cadoles.com.json | $(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client agent spec update -a $(AGENT_ID) --no-patch --spec-data - --spec-name mdns.emissary.cadoles.com"
	cat misc/spec-samples/uci.emissary.cadoles.com.json | $(MAKE) run-emissary-server EMISSARY_CMD="--debug --config tmp/server.yml client agent spec update -a $(AGENT_ID) --no-patch --spec-data - --spec-name uci.emissary.cadoles.com"

version: .mktools
	@echo $(MKT_PROJECT_VERSION)

update-edge-lib:
	git pull --rebase
	go clean -modcache
	GONOPROXY=forge.cadoles.com/arcad/edge GOPRIVATE=forge.cadoles.com/arcad/edge go get -v -u forge.cadoles.com/arcad/edge
	go mod tidy
	$(MAKE) test
	git add go.mod go.sum
	git commit -m "feat: update arcad/edge dependency"
	git push

.PHONY: changelog
changelog: .mktools
	$(MAKE) MKT_GIT_CHGLOG_ARGS='--next-tag $(MKT_PROJECT_VERSION) --tag-filter-pattern $(MKT_PROJECT_VERSION_CHANNEL) --output CHANGELOG.md' mkt-changelog

.PHONY: mktools
mktools:
	rm -rf .mktools
	curl -q https://forge.cadoles.com/Cadoles/mktools/raw/branch/master/install.sh | $(SHELL)

.mktools:
	$(MAKE) mktools

-include .mktools/*.mk