PGSQL_CONTAINER_ID=`cat postgres.container.id`
SOURCES=$(wildcard src/main/nim/*.nim) $(wildcard src/main/nim/personal_measure_apipkg/*.nim)

# Variables that can be overriden
# -------------------------------

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

# The version number that will be tagged the container image. You might want to
# override this when doing local development to create local versions that are
# reflect changes not yet committed.
VERSION ?= `git describe`

# The port on the host machine (not the container)
PORT ?= 8100

# The name of the database (used then creating a local Postgres container)
DB_NAME ?= personal_measure

# The database connection string. You would change this to point the API at a
# different database server (default is the local Postgres container).
DB_CONN_STRING ?= host=localhost dbname=$(DB_NAME) user=postgres password=password port=5500

# The API authentication secret (used for hashing passwords, etc.)
AUTH_SECRET ?= 123abc


default: start-postgres serve-docker

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

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

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

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

personal_measure_api: $(SOURCES)
	# Build the API
	nimble build

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

# Running the API locally in a container
# --------------------------------------

serve-docker: personal_measure_api-image
	# 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.
	docker run \
		-e AUTH_SECRET=$(AUTH_SECRET) \
		-e PORT=80 \
		-e "DB_CONN_STRING=$(DB_CONN_STRING)" \
		-e 'KNOWN_ORIGINS=["https://curl.localhost"]' \
		-p 127.0.0.1:$(PORT):80/tcp \
		$(ECR_ACCOUNT_URL)/personal_measure_api:$(VERSION)

# Managing Postgres in a local container
# --------------------------------------
#
#   This supports local development on this machine. These commands rely on a
#   file named `postgres.container.id` to track the existing and ID of the
#   local Postgres instance.

postgres.container.id:
	# This creates a new local Postegres container and initializes the PM API
	# database scheme.
	docker run --name postgres-$(DB_NAME) -e POSTGRES_PASSWORD=password -p 5500:5432 -d postgres > postgres.container.id
	sleep 5
	PGPASSWORD=password psql -p 5500 -U postgres -h localhost -c "CREATE DATABASE $(DB_NAME);"
	db_migrate up -c database-local.json

start-postgres: postgres.container.id
	# Start the existing local Postgres container
	docker start $(PGSQL_CONTAINER_ID)
	sleep 1
	db_migrate up -c database-local.json

stop-postgres: postgres.container.id
	# Stop the existing local Postgres container
	docker stop $(PGSQL_CONTAINER_ID)

delete-postgres-container:
	# Delete the local Postgres container. Note that this will destroy any data
	# in this database instance.
	-docker stop $(PGSQL_CONTAINER_ID)
	docker container rm $(PGSQL_CONTAINER_ID)
	rm postgres.container.id

connect-postgres:
	# Connect to the Postgres instance running in the local container
	PGPASSWORD=password psql -p 5500 -U postgres -h localhost ${DB_NAME}


# Utility
# -------

ecr-auth:
	# Authenticate docker to the AWS private elastic container repository.
	aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 063932952339.dkr.ecr.us-west-2.amazonaws.com

echo-vars:
	@echo \
		" ECR_ACCOUNT_URL=$(ECR_ACCOUNT_URL)\n" \
		"VERSION=$(VERSION)\n" \
		"PORT=$(PORT)\n" \
		"DB_NAME=$(DB_NAME)\n" \
		"DB_CONN_STRING=$(DB_CONN_STRING)\n" \
		"AUTH_SECRET=$(AUTH_SECRET)\n"
