.DEFAULT_GOAL := start
CURRENT_DIRECTORY := $(shell pwd)
APP_CONFIGS_DIR := ./application/configs

# Use this to create the docker.properties file
# TODO: after docker.properties is deprecated, this target should just create
#       secrets.env from the example file
configs:
	rm -f application/configs/secrets.env
	cp application/configs/docker.shadow.properties application/configs/docker.properties

start: replace-configs up

verbose:
	@ant -buildfile build/build.xml replace-configs
	@docker-compose --verbose up -d

clean:
	@docker-compose kill
	@docker-compose rm --force

# Copy shadow files to non-shadow file ext, perform token replacement, and
# overwrite config files ONLY if changed. This avoids busting docker cache!
replace-configs:
	@ant -buildfile build/build.xml replace-configs

# Delete all non-shadow config files (except docker.properties).
# WARNING: this will invalidate a lot of docker-cached steps.
clean-configs:
	@find ./application/configs/ -maxdepth 1 ! -name '*.shadow.*' ! -name 'zf_config.php' ! -name 'docker.properties' -name '*.*' -delete

build:
	@docker-compose build

build_with_force:
	@docker-compose build --force-rm --no-cache

up:
	@docker-compose up -d

rebuild: clean replace-configs build up

rebuild_all: clean replace-configs build_with_force up

stop:
	@docker-compose stop

status:
	@docker-compose ps

cli:
	@docker-compose run --rm webserver bash

log:
	@tail -f build/logs/vapi-error.log

restart:
	@docker-compose kill
	@docker-compose start webserver

test:
	@FRISBY_VAPI_HOST=http://vapi.192.168.99.100.xip.io ant -buildfile build/build.xml frisby

# Start nginx reverse proxy if not already started, rewrite configs, rebuild container, run tests
jenkins-frisby-pr:
	make space || true
	cd $(APP_CONFIGS_DIR); cp docker.shadow.properties docker.properties
	@sed -i "s|JENKINS_GITHUB_OAUTH_TOKEN|$(JENKINS_GITHUB_OAUTH_TOKEN)|" $(APP_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_KEY|$(JENKINS_AWS_ACCESS_KEY)|" $(APP_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_SECRET|$(JENKINS_AWS_ACCESS_SECRET)|" $(APP_CONFIGS_DIR)/docker.properties
	# Must come before anything else or we get error "Couldn't find env file"
	ant -buildfile build/build.xml replace-configs
	./docker/prep_jenkins_env.sh
	docker-compose kill webserver
	docker-compose rm --force webserver
	docker-compose build
	docker-compose up -d webserver
	# TODO: reenable frisby tests
	# FRISBY_VAPI_HOST=http://vapi-${ghprbPullId}.127.0.0.1.xip.io ant -buildfile build/build.xml frisby
	# For now, just check if containers are created properly
	docker-compose ps

# Delete all untagged images; cleans up disk space in docker-machine VM: http://blog.benhall.me.uk/2015/01/boot2docker-runs-disk-space/
# This is safe! It won't delete your latest built docker container because it will be tagged "latest", not "<none>"
space:
	@docker rmi `docker images | grep '<none>' | tr -s ' ' | cut -d ' ' -f 3`

.PHONY: build configs start verbose clean replace-configs clean-configs rebuild stop status cli log restart test jenkins-frisby-pr space


docker_down:
	docker compose down --remove-orphans

docker_unit_lint:
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

ci_unit_lint: ci_clean test_results docker_unit_lint

test_results:
	mkdir -p build/logs
	chmod a+w build/logs

ci_clean: docker_down
	rm -rf build/logs

update_composer_lock:
	docker compose run --rm --build update-composer-lock
