# JoinDomainGUI — diagnostic reseau AD + jointure de domaine
# `make` seul (ou `make help`) affiche les cibles disponibles.

.DEFAULT_GOAL := help

VENV        := .build-venv-linux
PY          := $(VENV)/bin/python
PIP         := $(VENV)/bin/pip
VERSION     := $(shell sed -n 's/^__version__ = "\(.*\)"/\1/p' domainjoin/__init__.py)
DOMAIN      ?= h3adm.lan
BRANCH      := $(shell git rev-parse --abbrev-ref HEAD)
REMOTES     := $(shell git remote)

##@ 📦 Installation

$(VENV)/bin/python:
	python3 -m venv --system-site-packages $(VENV)
	$(PIP) install --upgrade pip -q
	$(PIP) install -q -r requirements.txt

.PHONY: install
install: $(VENV)/bin/python ## Cree l'environnement virtuel et installe les dependances
	@echo "✅ Environnement pret : $(VENV)"

##@ 🚀 Utilisation

.PHONY: run
run: install ## Lance l'interface graphique
	$(PY) main.py

.PHONY: diagnose
diagnose: install ## Lance le diagnostic reseau en CLI (make diagnose DOMAIN=exemple.lan)
	$(PY) main.py diagnose --domain $(DOMAIN)

##@ 🏗️  Compilation

.PHONY: build
build: build-linux build-windows ## Compile les binaires Linux et Windows

.PHONY: build-linux
build-linux: ## Compile le binaire Linux autonome (dist/JoinDomainGUI)
	./packaging/build_linux.sh

.PHONY: build-windows
build-windows: ## Compile le .exe Windows depuis Linux via Wine (dist/windows/JoinDomainGUI.exe)
	./packaging/build_windows_wine.sh

##@ 🚢 Publication

.PHONY: remotes
remotes: ## Liste les depots distants configures
	@git remote -v | awk '$$3 == "(push)" { printf "  %-10s %s\n", $$1, $$2 }'

.PHONY: publish
publish: ## Pousse la branche courante et les tags vers tous les depots (Gitea)
	@git diff --quiet HEAD || { echo "❌ Modifications non commitees — commit avant de publier."; exit 1; }
	@for remote in $(REMOTES); do \
		echo "🚀 $$remote ← $(BRANCH)"; \
		git push $$remote $(BRANCH) || exit 1; \
		git push $$remote --tags || exit 1; \
	done
	@echo "✅ Publie sur : $(REMOTES)"

##@ 🧹 Nettoyage

.PHONY: clean
clean: ## Supprime les artefacts de compilation et les caches Python
	rm -rf build dist
	find . -path ./$(VENV) -prune -o -name '__pycache__' -type d -print0 | xargs -0 rm -rf

.PHONY: distclean
distclean: clean ## Nettoyage complet : + environnements virtuels, prefixe Wine et telechargements
	rm -rf $(VENV) .build-venv-windows .build-wine .build-wine-cache

##@ ℹ️  Aide

.PHONY: version
version: ## Affiche la version courante
	@echo "JoinDomainGUI $(VERSION)"

.PHONY: help
help: ## Affiche cette aide
	@echo "JoinDomainGUI $(VERSION)"
	@awk 'BEGIN {FS = ":.*?## "} \
		/^[a-zA-Z0-9_.-]+:.*?##/ { printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2 } \
		/^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) }' $(MAKEFILE_LIST)
	@echo ""
