# General settings
APP_NAME := pluralityspace
PYTHON ?= python3

# Development server
SERVER_LISTEN ?= 0.0.0.0:8042

# Deployment settings
STAGE ?= production
GIT_CRYPT_KEY ?= ~/.git-crypt-keys/$(APP_NAME)-$(STAGE).key
DOCKER_COMPOSE_YML := .deployment/$(STAGE)/docker-compose.yml
APP_CONTAINER_NAME := django

.PHONY: run docker-build docker-run decrypt deploy log-show live-collectstatic live-migrate live-shell

# Default target: none
all:


### Local development

# Run django development server
run:
	$(PYTHON) manage.py runserver $(SERVER_LISTEN)

# Build docker image (without docker-compose)
docker-build:
	docker build -t $(APP_NAME):latest .

# Run docker image (without docker-compose)
docker-run:
	docker run --rm -ti -p 8000:8000 $(APP_NAME):latest


### Deployment

# Unlock git-crypted production secrets
decrypt:
	git-crypt unlock $(GIT_CRYPT_KEY)

# Deploy on production using docker-compose
deploy:
	docker-compose -f $(DOCKER_COMPOSE_YML) up --build --detach

# Show logs for running container
log-show:
	docker-compose -f $(DOCKER_COMPOSE_YML) logs -f

# Run django 'collectstatic' command on live container
live-collectstatic:
	docker-compose -f $(DOCKER_COMPOSE_YML) exec ${APP_CONTAINER_NAME} manage.py collectstatic --no-input --clear

# Run django 'migrate' command on live container
live-migrate:
	docker-compose -f $(DOCKER_COMPOSE_YML) exec ${APP_CONTAINER_NAME} manage.py migrate --no-input

# Run shell in live container
live-shell:
	docker-compose -f $(DOCKER_COMPOSE_YML) exec ${APP_CONTAINER_NAME} sh
