.PHONY:
	install-tools \
	install \
	unit-test \
	help \
	clean \
	link-systemd-units \
	unlink-systemd-units \
	run-clad \
	status-clad \
	reload-clad \
	manpages \
	docs \
	distribution-tarball \
	html-docs \
	release

# Project directory path - /home/<user>/.../command-line-assistant
PROJECT_DIR := $(shell pwd)
DATA_DEVELOPMENT_PATH := $(PROJECT_DIR)/data/development

## Systemd specifics
# https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-subst-1
# Virtualenv bin path for clad
CLAD_VENV_BIN := $(subst /,\/,$(PROJECT_DIR)/.venv/bin/clad)
# Systemd development unit
CLAD_SYSTEMD_DEVEL_PATH := $(DATA_DEVELOPMENT_PATH)/systemd/clad-devel.service
# DBus development conf
CLAD_DBUS_DEVEL_PATH := $(DATA_DEVELOPMENT_PATH)/dbus/com.redhat.lightspeed.conf
# Systemd user unit, which is generated from devel unit
CLAD_SYSTEMD_USER_PATH := $(DATA_DEVELOPMENT_PATH)/systemd/clad-user.service
# Systemd path on the system to place the user unit
SYSTEMD_USER_UNITS := ~/.config/systemd/user
# Path to local XDG_CONFIG_DIRS to load config file
XDG_CONFIG_DIRS := $(subst /,\/,$(DATA_DEVELOPMENT_PATH)/xdg)

# Use recursive assignment so UV is evaluated each time it's used
UV = $(shell command -v uv || echo "$$HOME/.local/bin/uv")

PKGNAME := command-line-assistant
VERSION := 0.5.0

default: help

install-tools: ## Install required utilities/tools
	@command -v uv > /dev/null || { echo >&2 "uv is not installed. Installing..."; curl -LsSf https://astral.sh/uv/install.sh | sh; }
	@$(UV) --version

install: install-tools ## Sync all required dependencies for Command Line Assistant to work
	@$(UV) sync --extra dev --extra docs

unit-test: ## Unit test cla
	@echo "Running tests..."
	@$(UV) run pytest
	@echo "Tests completed."

unit-test-coverage: ## Unit test cla with coverage
	@echo "Running tests..."
	@$(UV) run pytest --cov --junitxml=junit.xml -o junit_family=legacy
	@echo "Tests completed."

coverage: ## Generate coverage report from unit-tests
	@$(UV) run coverage xml

coverage-html: ## Generate coverage report from unit-tests as html
	@$(UV) run coverage html

help: ## Show available make commands
	@echo 'Usage: make <OPTIONS> ... <TARGETS>'
	@echo ''
	@echo 'Available targets are:'
	@echo ''
	@grep -E '^[ a-zA-Z0-9_.-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'
	@echo ''

clean: ## Clean project files
	@echo 'Cleanup project specific files...'
	@find . -name '__pycache__' -exec rm -fr {} +
	@find . -name '*.pyc' -exec rm -f {} +
	@find . -name '*.pyo' -exec rm -f {} +
	@rm -rf htmlcov \
	   .pytest_cache \
	   command_line_assistant.egg-info \
	   .ruff_cache \
	   .coverage \
	   .tox \
	   junit.xml \
	   coverage.xml \
	   .mypy_cache \
	   dist \
	   $(PKGNAME)-$(VERSION).tar.gz
	$(MAKE) -C docs clean
	$(MAKE) -C data/release/selinux

link-systemd-units: ## Link the systemd units to ~/.config/systemd/user
	@echo "Linking the systemd units from $(CLAD_SYSTEMD_DEVEL_PATH) to $(SYSTEMD_USER_UNITS)/clad.service"
	@sed -e 's/{{ EXEC_START }}/$(CLAD_VENV_BIN)/'			   \
		 -e 's/{{ CONFIG_FILE_PATH }}/$(XDG_CONFIG_DIRS)/' \
		 -e 's/{{ DBUS_SESSION_ADDRESS }}/$(subst /,\/,$(DBUS_SESSION_BUS_ADDRESS))/' \
	     $(CLAD_SYSTEMD_DEVEL_PATH) > $(CLAD_SYSTEMD_USER_PATH)
	@ln -s $(CLAD_SYSTEMD_USER_PATH) $(SYSTEMD_USER_UNITS)/clad.service
	@sudo ln -s $(CLAD_DBUS_DEVEL_PATH) /etc/dbus-1/system.d/com.redhat.lightspeed.conf

unlink-systemd-units: ## Unlink the systemd units from ~/.config/systemd/user
	@echo "Unlinking the systemd units from $(SYSTEMD_USER_UNITS)/clad.service"
	@unlink $(SYSTEMD_USER_UNITS)/clad.service
	@sudo unlink /etc/dbus-1/system.d/com.redhat.lightspeed.conf

clad: ## Run clad on the system
	@XDG_CONFIG_DIRS=$(XDG_CONFIG_DIRS) $(CLAD_VENV_BIN)

run-clad: ## Run the clad under systemd
	@systemctl start --user clad

status-clad: ## Check the status for clad
	@journalctl --user -fu clad

reload-clad: ## Reload clad systemd unit
	@systemctl --user daemon-reload
	@systemctl restart --user clad

man: ## Build manpages
	# Build the manpages and change the builddir to match data/release
	# Also change the doctrees cache to still use the original build directory.
	$(MAKE) BUILDDIR=../data/release SPHINXOPTS=-d=build -C docs man

html-docs: ## Build html docs
	$(MAKE) -C docs html

distribution-tarball: clean ## Generate distribution tarball
	mkdir dist
	tar --create \
		--gzip \
		--file /tmp/$(PKGNAME)-$(VERSION).tar.gz \
		--exclude=.git \
		--exclude=.vscode \
		--exclude=.github \
		--exclude=.gitignore \
		--exclude=.copr \
		--exclude=.venv \
		--exclude=.ruff_cache \
		--exclude=data/development \
		--exclude=scripts \
		--exclude=docs \
		--exclude=tests \
		--exclude=.ropeproject \
		--exclude=.gitlab-ci.yml \
		--exclude=.readthedocs.yaml \
		--exclude=podman-compose.yaml \
		--exclude=tox.ini \
		--exclude=renovate.json \
		--exclude=.pre-commit-config.yaml \
		--exclude=.packit.yaml \
		--exclude=sonar-project.properties \
		--exclude=dist \
		--transform s/^\./$(PKGNAME)-$(VERSION)/ \
		. && mv /tmp/$(PKGNAME)-$(VERSION).tar.gz dist

release: ## Interactively bump the version (major, minor, or patch)
	@echo "Current version: $(VERSION)"
	@echo "Select version to bump:"
	@echo "  1) Major ($(shell echo $(VERSION) | awk -F. '{print $$1+1".0.0"}'))"
	@echo "  2) Minor ($(shell echo $(VERSION) | awk -F. '{print $$1"."($$2+1)".0"}'))"
	@echo "  3) Patch ($(shell echo $(VERSION) | awk -F. '{print $$1"."$$2"."($$3+1)}'))"
	@echo "  4) Custom version"
	@echo -n "Enter choice [1-4]: "
	@read choice; \
	case $$choice in \
		1) new_version=$$(echo $(VERSION) | awk -F. '{print $$1+1".0.0"}');; \
		2) new_version=$$(echo $(VERSION) | awk -F. '{print $$1"."($$2+1)".0"}');; \
		3) new_version=$$(echo $(VERSION) | awk -F. '{print $$1"."$$2"."($$3+1)}');; \
		4) echo -n "Enter custom version (X.Y.Z format): "; read new_version;; \
		*) echo "Invalid choice"; exit 1;; \
	esac; \
	echo "Bumping version to $$new_version"; \
	python scripts/prepare_release.py $$new_version

build-container: ## Build a container image
	podman build -t rhel-ligspeed/command-line-assistant:latest .

launch-container: ## Launch CLA container
	podman run -u : --detach -it --name command-line-assistant rhel-ligspeed/command-line-assistant:latest
