#----------------------
# Parse makefile arguments
#----------------------
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)

#----------------------
# Silence GNU Make
#----------------------
ifndef VERBOSE
MAKEFLAGS += --no-print-directory
endif

#----------------------
# Load .env file
#----------------------
ifneq ("$(wildcard .env)","")
include .env
export
else
endif

#----------------------
# Terminal
#----------------------

GREEN  := $(shell tput -Txterm setaf 2)
WHITE  := $(shell tput -Txterm setaf 7)
YELLOW := $(shell tput -Txterm setaf 3)
RESET  := $(shell tput -Txterm sgr0)

#------------------------------------------------------------------
# - Add the following 'help' target to your Makefile
# - Add help text after each target name starting with '\#\#'
# - A category can be added with @category
#------------------------------------------------------------------

.PHONY: build test

HELP_FUN = \
	%help; \
	while(<>) { \
		push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
		print "\n"; \
		for (sort keys %help) { \
			print "${WHITE}$$_${RESET \
		}\n"; \
		for (@{$$help{$$_}}) { \
			$$sep = " " x (32 - length $$_->[0]); \
			print "  ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
		}; \
		print ""; \
	}

help: ##@other Show this help.
	@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)

#----------------------
# ci-build
#----------------------

ci-build: ##@ci-build Builds and releases
	wails build -platform=windows/amd64
	@echo "Zipping binary"
	cp ./build/bin/eq-expansion-switcher.exe "./build/bin/ProjectEQ Expansion Switcher.exe"
	zip -r -j ./eq-expansion-switcher-windows-amd64.zip "./build/bin/ProjectEQ Expansion Switcher.exe"
	gh-release --assets=eq-expansion-switcher-windows-amd64.zip -y

#----------------------
# image build
#----------------------

image-build: ##@build Builds docker image
	docker build -t akkadius/wails:v2.0.3 ./containers/workspace

image-publish: ##@build Publishes docker image
	docker push akkadius/wails:v2.0.3

image-build-and-publish: ##@build Builds and publishes docker image
	make image-build
	make image-publish