.ONESHELL:
SHELL := bash
# https://github.com/JordanMartinez/purescript-cookbook/blob/master/makefile
# set -e = bash immediately exits if any command has a non-zero exit status.
# set -u = a reference to any shell variable you haven't previously
#    defined -- with the exceptions of $* and $@ -- is an error, and causes
#    the program to immediately exit with non-zero code.
# set -o pipefail = the first non-zero exit code emitted in one part of a
#    pipeline (e.g. `cat file.txt | grep 'foo'`) will be used as the exit
#    code for the entire pipeline. If all exit codes of a pipeline are zero,
#    the pipeline will emit an exit code of 0.
.SHELLFLAGS := -eu -o pipefail -c

.PHONY: all
all: prod

.PHONY: prod
prod: requirements.txt ## Starts a production webserver.
	source venv/bin/activate
	export PYTHONPATH="${PYTHONPATH}:src"
	python3 src/mastodon_toot_follower/server/waitress_server.py

.PHONY: dev
dev: requirements.txt ## Starts a development webserver.
	source venv/bin/activate
	export PYTHONPATH="${PYTHONPATH}:src"
	python3 src/mastodon_toot_follower/server/flask_server.py

.PHONY: docker-image
docker-image: ## Builds the mastodon-toot-follower:latest Docker image.
	sudo docker build --pull \
      --tag mastodon-toot-follower:latest .

.PHONY: cli
cli: requirements.txt ## Runs the commandline version.
	source venv/bin/activate
	export PYTHONPATH="${PYTHONPATH}:src"
	python3 src/mastodon_toot_follower/cli.py $(MASTODON_TOOT_URL)

.PHONY: requirements.txt
requirements.txt: ## Creates a virtual environment including the python dependencies.
	if ! [ -d venv ]; then
	  python3 -m venv venv
	  source venv/bin/activate
	  python3 -m pip install -r requirements.txt
	fi

.PHONY: clean
clean: ## Removes all created files.
	rm -rf venv ~/.MastodonTootFollower
	sudo rm -rf data

.PHONY: help
help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'