include .env
export $(shell sed 's/=.*//' .env)

SHELL := /bin/sh
PROJECTNAME ?= agenda_culturel
APP_NAME := $(PROJECTNAME)
BACKEND_APP_NAME := $(APP_NAME)-backend
REDIS_APP_NAME := $(APP_NAME)-redis


DOCKERCOMPOSE=$(shell if command -v docker-compose 2>&1 >/dev/null; then echo "docker-compose"; else echo "docker compose"; fi)
define HELP

Manage $(PROJECTNAME). Usage:

make lint           	Run linter
make format         	Run formatter
make test           	Run tests
make super-user     	Create super user
make make-migrations 	Make migrations
make migrate        	Migrate
make build-dev      	Build and run dev environment
make stop-dev       	Stop dev environment
make stop-prod      	Stop prod environment
make build-prod     	Build and run prod environment
make restar-prod        Restart prod environment
make all            	Show help

endef

export HELP

help:
	@echo "$$HELP"

lint:
	@bash ./scripts/lint.sh

format:
	@bash ./scripts/format.sh

test:
	@bash ./scripts/test.sh

super-user:
	docker exec -it $(BACKEND_APP_NAME) sh "-c" \
	"python manage.py createsuperuser"

make-migrations:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python manage.py makemigrations"

migrate:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python manage.py migrate"

create-categories:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python3 manage.py runscript create_categories"

create-reference-locations:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python3 manage.py runscript create_reference_locations"

cleanup-unused-media:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python3 manage.py cleanup_unused_media"

make-translations:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python3 manage.py makemessages --locale fr"

build-translations:
	docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \
	"python3 manage.py compilemessages"

build-dev:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKERCOMPOSE) -f docker-compose.yml up --build -d

build-dev-log:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKERCOMPOSE) -f docker-compose.yml up --build

build-prod:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKERCOMPOSE) -f docker-compose.prod.yml up --build -d

build-prod-log:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKERCOMPOSE) -f docker-compose.prod.yml up --build

stop-dev:
	@$(DOCKERCOMPOSE) -f docker-compose.yml down

stop-prod:
	@$(DOCKERCOMPOSE) -f docker-compose.prod.yml down

up-dev: #use up-dev to attach a running environment and have an access to the logs
	@$(DOCKERCOMPOSE) -f docker-compose.yml up

up-prod:
	@$(DOCKERCOMPOSE) -f docker-compose.prod.yml up

restart-backend-prod:
	$(DOCKERCOMPOSE) -f docker-compose.prod.yml restart backend

clean-builder-cache: # useful for huge overlay directory
	docker builder prune

restart-prod:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKERCOMPOSE) -f docker-compose.prod.yml restart

clear-redis:
	docker exec -i $(REDIS_APP_NAME) redis-cli FLUSHALL

all: help

.PHONY: help lint format test super-user make-migrations migrate build-dev build-prod stop-dev stop-prod all
