OPENWRT_DEVICE ?= 192.168.1.1
BACKUP_DATE ?=

GIT_VERSION := $(shell git describe --always)
DATE_VERSION := $(shell date +%Y.%m.%d)
FULL_VERSION := v$(DATE_VERSION)-$(GIT_VERSION)

OPENWRT_VERSION ?= 22.03.2
OPENWRT_TARGET ?= mvebu/cortexa9
OPENWRT_TARGET_DASHED ?= $(shell echo $(OPENWRT_TARGET) | sed 's|/|-|')
OPENWRT_PROFILE ?= linksys_wrt1200ac
OPENWRT_PACKAGES ?= $(shell cat packages.txt)
EXTRA_IMAGE_NAME ?= emissary-$(FULL_VERSION)
BIN_DIR_NAME_SUFFIX ?= 

IMAGEBUILDER_URL ?= https://downloads.openwrt.org/releases/$(OPENWRT_VERSION)/targets/$(OPENWRT_TARGET)/openwrt-imagebuilder-$(OPENWRT_VERSION)-$(OPENWRT_TARGET_DASHED).Linux-x86_64.tar.xz

IMAGEBUILDER_ARCHIVE_PATH := tmp/imagebuilder-$(OPENWRT_VERSION)-$(OPENWRT_TARGET_DASHED).tar.xz
IMAGEBUILDER_DIR_PATH := $(PWD)/imagebuilder/$(OPENWRT_VERSION)/$(OPENWRT_TARGET)
IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH := $(IMAGEBUILDER_DIR_PATH)/packages
IMAGEBUILDER_CUSTOM_FILES_DIR_PATH := $(IMAGEBUILDER_DIR_PATH)/files

BIN_DIR := "$(shell readlink -f bin)/$(OPENWRT_VERSION)/$(OPENWRT_TARGET)/$(OPENWRT_PROFILE)$(BIN_DIR_NAME_SUFFIX)"

GITEA_DOWNLOAD_RELEASE_NAME ?= $(shell cat emissary_release.txt)
EMISSARY_ARCH ?= armv6

EMISSARY_RECONCILIATION_INTERVAL ?=
EMISSARY_SERVER_URL ?=

BASE_INSTALL ?= install-emissary-files
ADDITIONAL_INSTALL ?=
ADDITIONAL_OPENWRT_PACKAGES ?=

include targets/*.mk
include install/*.mk

all:

build: $(IMAGEBUILDER_DIR_PATH) $(IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH) $(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)
	# Create artefacts directory
	mkdir -p "$(BIN_DIR)"
	
	# Add local packages to repositories
	sed -i -n -e '/^src imagebuilder file:packages/!p' -e '$$asrc imagebuilder file:packages' -e 's/^option check_signature//' "$(IMAGEBUILDER_DIR_PATH)/repositories.conf"

	# Cleanup old packages signature
	rm -f $(IMAGEBUILDER_DIR_PATH)/Packages $(IMAGEBUILDER_DIR_PATH)/Packages.gz $(IMAGEBUILDER_DIR_PATH)/Packages.sig
	
	# Build firmware
	$(MAKE) \
		-C "$(IMAGEBUILDER_DIR_PATH)" \
		EXTRA_IMAGE_NAME="$(EXTRA_IMAGE_NAME)" \
		PROFILE="$(OPENWRT_PROFILE)" \
		PACKAGES="$(OPENWRT_PACKAGES) $(ADDITIONAL_OPENWRT_PACKAGES)" \
		CONFIG_IPV6=n \
		FILES="$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)" \
		BIN_DIR="$(BIN_DIR)" \
		clean image

$(IMAGEBUILDER_DIR_PATH): $(IMAGEBUILDER_ARCHIVE_PATH)
	mkdir -p "$(IMAGEBUILDER_DIR_PATH)"
	tar -xf "$(IMAGEBUILDER_ARCHIVE_PATH)" --strip-components 1 -C "$(IMAGEBUILDER_DIR_PATH)"

$(IMAGEBUILDER_ARCHIVE_PATH):
	mkdir -p $(shell dirname "$(IMAGEBUILDER_ARCHIVE_PATH)")
	wget -O "$(IMAGEBUILDER_ARCHIVE_PATH)" "$(IMAGEBUILDER_URL)"

.PHONY: $(IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH)
$(IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH):
	rm -rf "$(IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH)"
	mkdir -p packages
	ln -fs "$(shell readlink -f packages)" "$(IMAGEBUILDER_CUSTOM_PACKAGES_DIR_PATH)"

.PHONY: $(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)
$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH):
	rm -rf "$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)"
	mkdir -p "$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)/etc"
	
	echo "# Firmware built with https://forge.cadoles.com/arcad/emissary-firmware" > "$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)/etc/emissary_firmware"
	echo "FIRMWARE_VERSION=$(FULL_VERSION)" >> "$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)/etc/emissary_firmware"
	echo "OPENWRT_PROFILE=$(OPENWRT_PROFILE)" >> "$(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)/etc/emissary_firmware"

	rm -rf files/*
	$(MAKE) $(BASE_INSTALL) $(ADDITIONAL_INSTALL)

	cp -rf files/* $(IMAGEBUILDER_CUSTOM_FILES_DIR_PATH)/

flash:
	OPENWRT_DEVICE=$(OPENWRT_DEVICE) OPENWRT_PROFILE=$(OPENWRT_PROFILE) OPENWRT_VERSION=$(OPENWRT_VERSION) misc/script/flash.sh

restore:
	OPENWRT_DEVICE=$(OPENWRT_DEVICE) BACKUP_DATE=$(BACKUP_DATE) misc/script/restore.sh

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

	find bin \
		\( 	-name '*.img.gz' \
			-or -name '*.bin' \
			-or -name '*.img' \
		\) -exec cp {} .gitea-release/ \;

	GITEA_RELEASE_PROJECT="emissary-firmware" \
		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="$$(find .gitea-release/* -type f)" \
		tools/gitea-release/bin/gitea-release.sh

.PHONY: download-emissary-release
download-emissary-release: tools/gitea-download/bin/gitea-download.sh
	rm -rf gitea-dl
	GITEA_DOWNLOAD_PROJECT="emissary" \
		GITEA_DOWNLOAD_ORG="arcad" \
		GITEA_DOWNLOAD_BASE_URL="https://forge.cadoles.com" \
		GITEA_DOWNLOAD_RELEASE_NAME="$(GITEA_DOWNLOAD_RELEASE_NAME)" \
		GITEA_DOWNLOAD_ATTACHMENTS_FILTER="\.tar\.gz$$" \
		tools/gitea-download/bin/gitea-download.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

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
	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

UPX_VERSION := 4.0.2

tools/upx/bin/upx:
	mkdir -p tools/upx/bin
	curl -L --output tools/upx/upx-$(UPX_VERSION)-amd64_linux.tar.xz https://github.com/upx/upx/releases/download/v$(UPX_VERSION)/upx-$(UPX_VERSION)-amd64_linux.tar.xz
	cd tools/upx && tar -xJf upx-$(UPX_VERSION)-amd64_linux.tar.xz && wait $$!
	$(SHELL) -c 'ln -s $$(readlink -f tools/upx/upx-$(UPX_VERSION)-amd64_linux/upx) tools/upx/bin/upx'