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

EMISSARY_VERSION ?= 
GIT_VERSION ?= $(shell git describe --always)

DOCKER_IMAGE_NAME ?= bornholm/emissary
DOCKER_IMAGE_TAG ?= $(GIT_VERSION)$(if $(shell git diff --stat),-dirty,)

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 )

.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=$(shell git describe --always)' \
			-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

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

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

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

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

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

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

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

docker-build:
	docker build -t $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) .

docker-release:
	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: tools/gitea-release/bin/gitea-release.sh goreleaser
	mkdir -p .gitea-release
	rm -rf .gitea-release/*

	cp dist/*.tar.gz .gitea-release/

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

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

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