EDGE_CLI_RELEASE_NAME ?= latest
GIT_REPOSITORY := "edge-yams"
GIT_VERSION := $(shell git describe --always)
DATE_VERSION := $(shell date +%Y.%-m.%-d)
FULL_VERSION := v$(DATE_VERSION)-$(GIT_VERSION)

export PATH := $(PATH)

init:
	. ${NVM_DIR}/nvm.sh && nvm install && nvm use
	npm install
	curl -k https://forge.cadoles.com/Cadoles/Jenkins/raw/branch/master/resources/com/cadoles/common/add-letsencrypt-ca.sh | sudo bash
	sudo apt-get install jq

rebuild:
	npm run build

watch:
	npm run watch_build
	
edge: tools/edge/bin/edge tools/yq/bin/yq

run:
	edge-cli app run -p ./build --log-level 0 

package: 
	rm -rf tmp
	mkdir -p tmp
	cp -r build/* ./tmp/
	tools/yq/bin/yq -i '.version = "$(FULL_VERSION)"' ./tmp/manifest.yml
	rm -rf dist
	mkdir -p dist
	edge-cli app package -d ./tmp -o ./dist

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

tools/edge/bin/edge: tools/gitea-download/bin/gitea-download.sh
	mkdir -p tools/edge/bin
	GITEA_DOWNLOAD_PROJECT="edge" \
		GITEA_DOWNLOAD_ORG="arcad" \
		GITEA_DOWNLOAD_BASE_URL="https://forge.cadoles.com" \
		GITEA_DOWNLOAD_RELEASE_NAME="$(EDGE_CLI_RELEASE_NAME)" \
		GITEA_DOWNLOAD_TARGET_DIRECTORY="tools/edge" \
		tools/gitea-download/bin/gitea-download.sh
	rm -f  tools/edge/edge-cli.deb
	mv tools/edge/edge-cli*.deb tools/edge/edge-cli.deb
	sudo dpkg -i tools/edge/edge-cli.deb

tools/gitea-release/bin/gitea-release.sh:
	mkdir -p tools/gitea-release/bin
	curl --insecure --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/gitea-download/bin/gitea-download.sh:
	mkdir -p tools/gitea-download/bin
	curl --output tools/gitea-download/bin/gitea-download.sh https://forge.cadoles.com/Cadoles/Jenkins/raw/branch/master/resources/com/cadoles/gitea/gitea-download.sh --insecure
	chmod +x tools/gitea-download/bin/gitea-download.sh

tools/yq/bin/yq:
	mkdir -p tools/yq/bin
	curl -L --output tools/yq/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_amd64
	chmod +x tools/yq/bin/yq