.PHONY = build-api, build-webapp, build, test, test-api, test-webapp, test-functional-api, test-functional-webapp, \
	demo, start, status, stop, bootstrap

# Global values for Rookeries components
DB_CONNECTION = "postgresql+psycopg2://admin:password@localhost:5432/rookeries"

# Build the API image
build-api:
	docker build --tag rookeries/api:build api

# Build the webapp image
build-webapp:
	docker build --tag rookeries/webapp:build webapp

# Build all the images
build: build-api build-webapp

# Runs all the tests
test: test-api test-webapp

# Runs api tests
test-api: build-api
	docker-compose up -d db
	docker-compose run api inv test.style
	docker-compose run api inv test.server --db-connection=$(DB_CONNECTION) --verbosity=3

# Runs webapp tests
test-webapp: build-webapp
	docker-compose run webapp npm run lint
	docker-compose run webapp npm run tests-unit

# Runs functional tests for api
test-functional-api: build
	docker-compose up -d db
	docker-compose up -d api
	docker-compose run api inv test.functional --db-connection=$(DB_CONNECTION) --server-host=api --verbosity=3

# Runs functional test for webapp
test-functional-webapp: build
	scripts/verify_sauceconnect_env_ready.sh
	docker-compose up -d
	docker-compose run \
		-e SAUCE_USERNAME=$SAUCE_USERNAME -e SAUCE_ACCESS_KEY=$SAUCE_ACCESS_KEY webapp \
		npm run tests-functional

# Demos Rookeries in a browser
demo:
	docker-compose up -d --remove-orphans
	python -m webbrowser -n http://localhost:3000/

# Starts Rookeries
start: build
	docker-compose up --remove-orphans

# Starts Rookeries
status:
	docker-compose ps

# Shuts down Rookeries
stop:
	docker-compose down

# Bootstrap DB
bootstrap: build
	docker-compose up -d db
	docker-compose run api inv db.bootstrap --db-connection=$(DB_CONNECTION) --reset-db
