SOURCES=$(wildcard src/*.nim) $(wildcard src/hff_entry_forms_apipkg/*.nim)

# AWS Account URL for the ECR repository
ECR_ACCOUNT_URL ?= 063932952339.dkr.ecr.us-west-2.amazonaws.com

# The port on the host machine (not the container)
ifeq ($(TARGET_ENV),prod)
	PORT=6006
else
	PORT=6005
endif

# The server to target when publishing the API
TARGET_SERVER ?= sobeck.jdb-software.com

# The Notion integration token.
AUTH_SECRET ?= 123abc

VERSION ?=`git describe`

default: serve-docker

# Running the API locally on bare metal
# -------------------------------------

hff_entry_forms_api: $(SOURCES)
	nimble build

# Run the API on this machine. Note that configuration is taken by default
# from the `hff_entry_forms_api.config.json` file, but environment variables
# specified when running make can be used to override these (to change the
# INTEGRATION_TOKEN, for example).
serve: hff_entry_forms_api
	./hff_entry_forms_api serve --debug

# Run tests
# ---------
#unittest:
#	nim c -r src/unittest/nim/runner
#
#functest: DB_NAME = live_budget_test
#functest: DB_CONFIG = test
#functest: hff_entry_forms_api start-postgres
#	echo "\n--------" >> functest-api-server.log
#	./hff_entry_forms_api serve --config hff_entry_forms_api.config.test.json >> functest-api-server.log &
#	-nim c -r src/functest/nim/runner "$(TEST_NAME)"
#	curl -s -X POST localhost:$(PORT)/api/v1/control/shutdown | jq .
#	db_migrate down -c database-$(DB_CONFIG).json
#	PGPASSWORD=password psql -p 5500 -U postgres -h localhost -c "DROP DATABASE $(DB_NAME);"

# Building and deploying the API container image
# ----------------------------------------------

# Build the container image.
build-image: $(SOURCES)
	docker image build -t $(ECR_ACCOUNT_URL)/hff_entry_forms_api:$(VERSION) .

# Push the container image to the private AWS ECR
push-image: build-image
	docker push $(ECR_ACCOUNT_URL)/hff_entry_forms_api:$(VERSION)

# Running locally in a container
# --------------------------------------

# Run the API in a docker container. Note that the configuration loaded into
# the Docker container defines very little of the actual configuration as
# environment variables are used in the deployed environments. Accordingly,
# we must specify them explicitly here.
serve-docker: build-image
	docker run \
		-e INTEGRATION_TOKEN=$(INTEGRATION_TOKEN) \
		-e PORT=80 \
		-p 127.0.0.1:$(PORT):80/tcp \
		$(ECR_ACCOUNT_URL)/hff_entry_forms_api:$(VERSION)

# Utility
# -------

clean:
	-docker image rm $(ECR_ACCOUNT_URL)/hff_entry_forms_api:$(VERSION)

# Authenticate docker to the AWS private elastic container repository.
ecr-auth:
	aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin $(ECR_ACCOUNT_URL)

echo-vars:
	@echo \
		" ECR_ACCOUNT_URL=$(ECR_ACCOUNT_URL)\n" \
		"VERSION=$(VERSION)\n" \
		"PORT=$(PORT)\n" \
		"INTEGRATION_TOKEN=$(INTEGRATION_TOKEN)\n"

publish:
	-rm -r deploy
	-mkdir deploy
	m4 \
		-D "HFF_ENTRY_FORMS_API_VERSION=$(VERSION)" \
		-D "TARGET_ENV=$(TARGET_ENV)" \
		-D "TARGET_PORT=$(PORT)" \
		hff_entry_forms_api.service \
		> deploy/hff_entry_forms_api.$(TARGET_ENV).service
	-ssh deployer@$(TARGET_SERVER) "docker stop hff_entry_forms_api.$(TARGET_ENV).service && sudo systemctl stop hff_entry_forms_api.$(TARGET_ENV)"
	ssh deployer@$(TARGET_SERVER) "aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin $(ECR_ACCOUNT_URL) && docker pull $(ECR_ACCOUNT_URL)/hff_entry_forms_api:$(VERSION)"
	scp \
		deploy/hff_entry_forms_api.$(TARGET_ENV).service \
		deployer@$(TARGET_SERVER):/etc/systemd/system/hff_entry_forms_api.$(TARGET_ENV).service
	ssh deployer@$(TARGET_SERVER) "sudo systemctl daemon-reload"
	ssh deployer@$(TARGET_SERVER) "sudo systemctl start hff_entry_forms_api.$(TARGET_ENV)"