include .env

.PHONY: up down stop prune ps shell shell_root wp logs

default: up

#######################################
# Variable admin
#######################################

REQUIRED_BINS := docker
$(foreach bin,$(REQUIRED_BINS),\
    $(if $(shell command -v $(bin) 2> /dev/null),$(true),$(error Please install `$(bin)`)))

ifeq ($(SITE_URL),)
  $(error SITE_URL is not set)
endif

ABRA ?= abra

DOCKER_COMPOSE ?= docker-compose

ifeq ($(DOCKER_SUDO), 1)
  DOCKER = sudo docker
  _DOCKER_COMPOSE = sudo $(DOCKER_COMPOSE)
else
  DOCKER = docker
  _DOCKER_COMPOSE = $(DOCKER_COMPOSE)
endif

#######################################
# Core commands
#######################################

## Start up containers.
up:
	@echo "Starting up containers for $(PROJECT_NAME)..."
	$(_DOCKER_COMPOSE) pull
	$(_DOCKER_COMPOSE) up -d --remove-orphans

## Stop containers.
down: stop

## Start containers without updating.
start:
	@echo "Starting containers for $(PROJECT_NAME) from where you left off..."
	@$(_DOCKER_COMPOSE) start

## Stop containers.
stop:
	@echo "Stopping containers for $(PROJECT_NAME)..."
	@$(_DOCKER_COMPOSE) stop

## Remove containers and their volumes.
## 	You can optionally pass an argument with the service name to prune single container
prune:
	@echo "Removing containers for $(PROJECT_NAME)..."
	@$(_DOCKER_COMPOSE) down -v $(filter-out $@,$(MAKECMDGOALS))

ps:
	@$(_DOCKER_COMPOSE) ps

shell:
	@$(_DOCKER_COMPOSE) exec -u user wordpress bash

shell_root:
	@$(_DOCKER_COMPOSE) exec wordpress bash

## Executes `wp cli`
## 	Doesn't support --flag arguments.
wp:
	$(_DOCKER_COMPOSE) exec -u user wordpress wp $(filter-out $@,$(MAKECMDGOALS))

## composer
## 	Doesn't support --flag arguments.
composer:
	$(_DOCKER_COMPOSE) exec -u user wordpress composer $(filter-out $@,$(MAKECMDGOALS))

## Show vontainers' logs
## 	You can optinally pass an argument with the service name to limit logs
logs:
	@$(_DOCKER_COMPOSE) logs -f $(filter-out $@,$(MAKECMDGOALS))

## Check that all required variables are defined
check:
	@echo "$$(tput setaf 125)Watch out for any lines starting '-', which indicate settings in .env.sample which aren't in .env$$(tput sgr0)"
	diff -u <(grep -v '^#' .env.sample | grep -v '^$$' | sed -e 's/=.*//g' | sort) <(grep -v '^#' .env | grep -v '^$$' | sed -e 's/=.*//g' | sort) || true
	@echo "$$(tput setaf 125)The following command shouldn't show any differences; if it does, re-copy WORDPRESS_CONFIG_EXTRA fr4om .env.sample to .env$$(tput sgr0)"
	diff -wu <(grep 'CONFIG_EXTRA' .env.sample ) <(grep 'CONFIG_EXTRA' .env)

#######################################
# Content management
#######################################

## Download wp-content files from site
uploads_fetch:
	@echo -n "About to download ~1.5GB of data, overwriting existing data/uploads.tar.gz. - are you sure? [Y/n] " && read ans && if [ $${ans:-'Y'} = 'n' ]; then \
		printf $(_ERROR) "Aborting as requested\n"; \
		exit 1 ; \
	else \
		printf $(_SUCCESS) "OK" "Continuing" ; \
		$(ABRA) app run --no-tty $(SITE_URL) app tar --owner=0 --group=0 --no-same-owner --no-same-permissions -czf- /var/www/html/wp-content/uploads/ > data/uploads.tar.gz; \
	fi

## Load latest wp-content from data/uploads.tar.gz
uploads_load:
	tar -C wp-content --strip-components=4 -xzf data/uploads.tar.gz 

#######################################
# Wacky commands you probably won't need to use
#######################################

## Download database from dev site
db_fetch:
	$(ABRA) app run $(SITE_URL) db bash -c 'mariadb-dump -u root -p"$$(cat /run/secrets/db_root_password)" wordpress' | gzip > data/dbdump.sql.gz

## Load latest database from ~/.abra/backups
db_load:
	zcat data/dbdump.sql.gz | $(_DOCKER_COMPOSE) exec -T db mysql -u wordpress -pwordpress wordpress

## Replace site URL
fix_url:
	$(_DOCKER_COMPOSE) exec -u user wordpress wp --url=https://$(SITE_URL) search-replace --all-tables-with-prefix 'https://$(SITE_URL)' 'http://$(PROJECT_NAME).localhost'

set_local_password:
	$(_DOCKER_COMPOSE) exec -u user wordpress wp user update $(USERNAME) --user_pass=password

db_pull: db_fetch db_load fix_url

## Sync wp-content from site
uploads_pull: uploads_fetch uploads_load

## Download wp-content files from site
plugins_fetch:
	$(ABRA) app run --no-tty $(SITE_URL) app tar --owner=0 --group=0 --no-same-owner --no-same-permissions -czf- /var/www/html/wp-content/plugins/ > data/plugins.tar.gz

## Load latest plugins from data/plugins.tar.gz
plugins_load:
	tar -C wp-content --strip-components=4 -xzf data/plugins.tar.gz 

## Sync plugins from site
plugins_pull: plugins_fetch plugins_load

# "Arguments" for makefiles..
# https://stackoverflow.com/a/6273809/1826109
%:
