.DELETE_ON_ERROR:
.ONESHELL:
.SHELLFLAGS   := -eu -o pipefail -c
MAKEFLAGS     += --warn-undefined-variables
MAKEFLAGS     += --no-builtin-rules
SHELL         := bash

.PHONY: help tree init run view clean debug build install config

.DEFAULT_GOAL := help

VENV           = .venv
ACTIVATE       = $(VENV)/bin/activate
PIP            = $(VENV)/bin/pip
PYTHON         = $(VENV)/bin/python
BREWPYTHON     = /opt/homebrew/bin/python3
PYGITIGNORE    = $(HOME)/Repos/gitignore/Python.gitignore
GIT_ORG        = ts
GIT_REPO       = $$(basename $$(pwd))
GIT_URL        = http://git.tsmetallic.com/$(GIT_ORG)/$(GIT_REPO)
PACKAGE        = tsm
PROJECT        = tsm


help:
	@echo -e ""
	@echo -e "  Help - Available Make Commands"
	@echo -e " ====================================================================="
	@echo -e ""
	@echo -e "  Setup Commands:"
	@echo -e ""
	@echo -e "      make init     :  Initialize version control and link remote"
	@echo -e "      make install  :  Install pkg/module in local environment"
	@echo -e ""
	@echo -e "  Dev Commands:"
	@echo -e ""
	@echo -e "      make tree     :  Show project's file system excluding caches"
	@echo -e "      make view     :  Display a snapshot of each database table"
	@echo -e "      make config   :  Display runtime configuration"
	@echo -e "      make debug    :  Toggle the configuration option for debug mode"
	@echo -e ""
	@echo -e "  Test Commands:"
	@echo -e ""
	@echo -e "      make test     :  Execute full test suite"
	@echo -e "      make utest    :  Execute unit tests"
	@echo -e "      make itest    :  Execute integration tests"
	@echo -e ""
	@echo -e "  Build Commands:"
	@echo -e ""
	@echo -e "      make build    :  Compile project to wheel distribution"
	@echo -e "      make clean    :  Remove cache files and build artifacts"
	@echo -e ""

tree:
	@tree -I "*.egg-info|__pycache__"

config: config.json
	@cat config.json | jq

debug:
	@/Users/tjsharley/Projects/tsm/.venv/bin/tsm-tgl

view:
	@source $(ACTIVATE) && $(PYTHON) src/heroes/view.py database.db team
	@source $(ACTIVATE) && $(PYTHON) src/heroes/view.py database.db hero

build: $(ACTIVATE)
	@source $(ACTIVATE) && $(PYTHON) -m build

install: $(ACTIVATE)
	@source $(ACTIVATE) && $(PYTHON) -m pip install -e .

run: $(ACTIVATE) $(EPSCRIPT)
	@source $(ACTIVATE) && $(EPSCRIPT)

$(ACTIVATE): requirements.txt
	@$(BREWPYTHON) -m venv $(VENV)
	@$(PYTHON) -m pip install --upgrade pip
	@$(PIP) install -r requirements.txt

clean:
	@rm -rf src/$(PKGNAME)/__pycache__
	@rm -rf proc/out/*

requirements.txt:
	@pip freeze --exclude-editable >> 'requirements.txt'

init: .git

.git: .gitignore
	@git init .
	@git add .
	@git commit -a -m 'Initial commit'
	@git remote add origin $(GIT_URL)
	@git push -u origin main

.gitignore:
	@cp $(PYGITIGNORE) .gitignore
