DOCKER_COMPOSE  = docker-compose

EXEC_PHP        = $(DOCKER_COMPOSE) exec php
EXEC_SAFE_PHP   = $(DOCKER_COMPOSE) exec -u www-data php
EXEC_DB         = $(DOCKER_COMPOSE) exec db

SYMFONY         = $(EXEC_SAFE_PHP) php -d memory_limit=-1 bin/console
COMPOSER        = $(DOCKER_COMPOSE) exec -e COMPOSER_MEMORY_LIMIT=-1 php composer

CURRENT_DATE = `date "+%Y-%m-%d_%H-%M-%S"`

##
## Project
## -------
##

install: ## Install and start the project
install: build start
.PHONY: install

build: ## Build the Docker images
	$(DOCKER_COMPOSE) build --force-rm --compress
.PHONY: build

start: ## Start the project
	$(DOCKER_COMPOSE) up -d --remove-orphans --force-recreate
.PHONY: start

start-light: ## Start the project, only required containers
	$(DOCKER_COMPOSE) up -d --force-recreate --remove-orphans db php
.PHONY: simple-start

stop: ## Stop the project
	$(DOCKER_COMPOSE) stop
.PHONY: stop

kill: ## Stop all containers
	$(DOCKER_COMPOSE) kill
	$(DOCKER_COMPOSE) down --volumes --remove-orphans
.PHONY: kill

reset: ## Stop and start a fresh install of the project
reset: kill install
.PHONY: reset

clean: ## Stop the project and remove generated files and configuration
clean: kill
	rm -rf vendor var/cache/* var/logs/*
.PHONY: clean

##
## Tools
## -----
##
ps:
	$(DOCKER_COMPOSE) ps
.PHONY: ps

logs:
	$(DOCKER_COMPOSE) logs
.PHONY: logs

db-restore: ## First last dump
db-restore:
	docker-compose exec -T db mysql -uroot -prootpass emma-db < emma-db.sql
.PHONY: db-restore

db-dump: ## Dump database
db-dump:
	docker-compose exec -T db mysqldump -uroot -prootpass emma-db > emma-db.sql
.PHONY: db-dump

enter: #Enter php container as root
enter:
	$(EXEC_PHP) bash
.PHONY: enter

safe-enter: #Enter php container as www-data
safe-enter:
	$(EXEC_SAFE_PHP) bash
.PHONY: safe-enter

php-logs: #Enter php container and show logs
php-logs:
	$(DOCKER_COMPOSE) logs -f php
.PHONY: php-logs
