PYTHON := python3
PIP    := pip3
PREFIX := /usr/local
MANDIR := $(PREFIX)/share/man/man1

.PHONY: install uninstall update bundle clean help

help:
	@echo ""
	@echo "  APEX — Autonomous Privilege Escalation eXecutor"
	@echo ""
	@echo "  make install    Install apex system-wide + build DB"
	@echo "  make uninstall  Remove apex"
	@echo "  make update     Refresh knowledge base (apex-update)"
	@echo "  make bundle     Build single-file bundles (dist/)"
	@echo "  make clean      Remove build artifacts"
	@echo ""

install:
	@echo "[*] Installing APEX..."
	$(PIP) install . --break-system-packages 2>/dev/null || $(PIP) install .
	@echo "[*] Installing man page..."
	@mkdir -p $(MANDIR)
	gzip -k -f man/apex.1 -c > /tmp/apex.1.gz
	install -m 0644 /tmp/apex.1.gz $(MANDIR)/apex.1.gz
	mandb 2>/dev/null || true
	@echo "[*] Running initial database build..."
	apex-update
	@echo ""
	@echo "[+] Done. Run 'apex' to start."
	@echo "[+] Man page: 'man apex'"

uninstall:
	$(PIP) uninstall apex -y 2>/dev/null || true
	rm -f $(MANDIR)/apex.1.gz
	mandb 2>/dev/null || true
	@echo "[+] APEX removed."

update:
	apex-update

bundle:
	@echo "[*] Building single-file bundles..."
	$(PYTHON) -m apex.bundle
	@echo "[+] Bundles in dist/"

clean:
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -name "*.pyc" -delete 2>/dev/null || true
	rm -rf build/ dist/ *.egg-info/
	@echo "[+] Clean."
