.DEFAULT_GOAL := start
CURRENT_DIRECTORY := $(shell pwd)
API_ROOT_DIR = /tmp/master/api
API_CONFIGS_DIR := $(API_ROOT_DIR)/application/configs
ORCHARD_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

# 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.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 .gitignore ! -name '*.php' ! -name 'docker.properties' -name '*.*' -delete

start: replace-configs
	@docker-compose up -d

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

build:
	@docker-compose build

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

up:
	mkdir -p ./public/tmp  # Release Builder requires /var/www/html/public/tmp to exist
	@docker-compose up -d

rebuild: clean replace-configs build up

rebuild_all: clean replace-configs build_with_force up

#
# EXPERIMENTAL! These 3 tasks are OS X only!
#

# Install docker-osx-dev
install-dev:
	@sudo curl -o /usr/local/bin/docker-osx-dev https://raw.githubusercontent.com/brikis98/docker-osx-dev/master/src/docker-osx-dev \
	&& sudo chmod +x /usr/local/bin/docker-osx-dev \
	&& docker-osx-dev install

# Start the file watcher
watch:
	@docker-osx-dev

# Use this in a new shell window after `make watch`
dev:
	@docker-compose up -d

#
# End of EXPERIMENTAL tasks
#

error:
	@tail -f build/logs/error_log

access:
	@tail -f build/logs/access_log

# Drop into a bash shell in the running webserver container
cli:
	@docker exec -it orchard_webserver_1 bash

oa:
	@open -a "Google Chrome" http://oa.192.168.99.100.xip.io

ws:
	@open -a "Google Chrome" http://workstation.192.168.99.100.xip.io

spinach:
	@cd tests/spinach; ./run_spinach.sh

cukes:
	@cd tests/cucumber; ./run_cukes.sh

# 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`

# Jenkins acceptance test PR build executes this target
jenkins:
	@echo "Deleting all untagged docker images!"
	@make space || true
	# Set up api repo
	git clone git@github.com:theorchard/api.git $(API_ROOT_DIR) || true
	cd $(API_ROOT_DIR); git pull
	# Replace secrets in api's docker.properties with Jenkins env vars
	cd $(API_CONFIGS_DIR); cp docker.shadow.properties docker.properties
	@sed -i "s|JENKINS_GITHUB_OAUTH_TOKEN|$(JENKINS_GITHUB_OAUTH_TOKEN)|" $(API_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_KEY|$(JENKINS_AWS_ACCESS_KEY)|" $(API_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_SECRET|$(JENKINS_AWS_ACCESS_SECRET)|" $(API_CONFIGS_DIR)/docker.properties
	cd $(API_ROOT_DIR); ant -buildfile build/build.xml replace-configs
	# Replace secrets in orchard's docker.properties with Jenkins env vars
	cd $(ORCHARD_CONFIGS_DIR); cp docker.shadow.properties docker.properties
	@sed -i "s|JENKINS_GITHUB_OAUTH_TOKEN|$(JENKINS_GITHUB_OAUTH_TOKEN)|" $(ORCHARD_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_KEY|$(JENKINS_AWS_ACCESS_KEY)|" $(ORCHARD_CONFIGS_DIR)/docker.properties
	@sed -i "s|JENKINS_AWS_ACCESS_SECRET|$(JENKINS_AWS_ACCESS_SECRET)|" $(ORCHARD_CONFIGS_DIR)/docker.properties
	# Also replace references to docker-machine VM IP address (192.168.99.10-)
	sed -i "s/192\.168\.99\.10-/$(JENKINS_DOCKER_HOST)/g" $(ORCHARD_CONFIGS_DIR)/docker.properties
	# Needs to come before docker-compose commands, or else we could get error "Couldn't find env file"
	ant -buildfile build.xml replace-configs
	# Bring up the entire orchard env, for Linux Docker host
	docker-compose -f ./docker/staging.yml kill
	docker-compose -f ./docker/staging.yml rm --force
	docker-compose -f ./docker/staging.yml build
	docker-compose -f ./docker/staging.yml up -d
	# to test this line locally, do: JOB_NAME=fake make spinach
	cd tests/spinach; ./run_spinach.sh || true
	# TODO: replace spinach with cukes:
	# cd tests/cucumber; ./run_cukes.sh
	@echo "[Makefile] dumping webserver httpd error log to Jenkins Console"
	docker exec -t docker_webserver_1 tail -n 100 /var/log/httpd/error_log

test_calc:
	APPLICATION_ENV=testing ./vendor/bin/phpunit --testsuite PaymentCalculator --configuration tests/phpunit.xml --bootstrap scripts/Accounting/PaymentCalculator/bootstrap.php

.PHONY: build configs replace-configs clean-configs start clean rebuild install-dev watch dev error access cli oa ws spinach cukes space jenkins test_calc

docker_down:
	docker compose down --remove-orphans

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

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

ci_clean: docker_down
	rm -rf build/logs

ci_unit_lint: ci_clean test_results docker_unit_lint

update_composer_lock:
	docker compose run --rm --build update-lockfile
