# Project variables
LOGIN = xhonchi00_xbartom00_xtiahnd00_xvasylv00
PYTHON = python3

# Virtual Environment variables
VENV = ../venv
VENV_PYTHON = $(VENV)/bin/python
VENV_PIP = $(VENV)/bin/pip

.PHONY: all pack clean test doc run stddev help venv

# Default target - sets up the environment
all: venv
	@echo "Project initialized: venv created and dependencies installed."

# Rule to create the virtual environment and install dependencies
$(VENV_PYTHON):
	$(PYTHON) -m venv $(VENV)
	$(VENV_PIP) install --upgrade pip
	$(VENV_PIP) install -r requirements.txt

# Alias target for ease of use
venv: $(VENV_PYTHON)

# Run the GUI using the virtual environment's Python
run: venv
	$(VENV_PYTHON) gui.py

# Tests for the mathematical library using the virtual environment
test: venv
	$(VENV_PYTHON) -m pytest test_mathlib.py

test_gui: venv
	$(VENV_PYTHON) -m pytest test_gui.py

# doxygen documentation
doc:
	@echo "Generating documentation with Doxygen..."
	doxygen Doxyfile

# Starts the program for calculating standard deviation
stddev: venv

	@echo '#!/bin/bash' > stddev
	@echo '$(VENV_PYTHON) profiling.py "$$@"' >> stddev
	@chmod +x stddev
	
	@echo "Profiling starter created. Ready to run"

# Delete all temporary, generated files, folders, and the virtual environment
clean:
	rm -rf __pycache__ .pytest_cache ../.pytest_cache
	rm -rf ../$(LOGIN) ../$(LOGIN).zip
	rm -rf $(VENV)
	rm -rf ../doc
	rm -rf stddev
	@echo "CLEAN DONE: All temporary and generated files have been removed."

# Pack the project into a ZIP archive for submission
pack: clean doc
	@echo "Preparing project for submission..."
	mkdir -p ../$(LOGIN)/doc
	mkdir -p ../$(LOGIN)/install
	mkdir -p ../$(LOGIN)/repo
	
	# TODO copy documentation to the doc folder
	cp -r ../doc/html/* ../$(LOGIN)/doc/
	# TODO copy installation instructions to the install folder
	cp install.sh ../$(LOGIN)/install/
	cp uninstall.sh ../$(LOGIN)/install/
	
	# copy source code to the repo folder
	rsync -av --progress ../ ../$(LOGIN)/repo \
		--exclude $(LOGIN) \
		--exclude 'install.sh' \
		--exclude 'uninstall.sh' \
		--exclude 'input*.txt' \
		--exclude doc/ \
		--exclude 'HEAD' \
		--exclude 'config' \
		--exclude 'description' \
		--exclude info/ \
		--exclude hooks/ \
		--exclude refs/ \
		--exclude branches/ \
		--exclude objects

	cd .. && zip -r $(LOGIN).zip $(LOGIN)
	@echo "Archive $(LOGIN).zip created successfully. Ready for submission."

help:
	@echo "------------------------------------------------------------------"
	@echo "              Help for Makefile"
	@echo "------------------------------------------------------------------"
	@echo "  make all    - Prepare project (create venv, install dependencies)"
	@echo "  make pack   - Pack project into ZIP archive"
	@echo "  make clean  - Delete temporary and generated files (including venv)"
	@echo "  make test   - Run tests for the mathematical library"
	@echo "  make doc    - Generate program documentation (Doxygen)"
	@echo "  make run    - Run the program (GUI)"
	@echo "  make stddev - Run the program for calculating standard deviation"
	@echo "  make help   - Display this help message"
	@echo "------------------------------------------------------------------"