# Définition des variables
PACKAGE = fr.monlouyan.bakefile
ENTRY = Main
BUILDDIR = ./build/
DOCDIR = ./documentation/javadoc
JARNAME = bakefile.jar
CLASSP = ./build
MANIFESTPATH = Manifest.MF
SOURCEDIR = ./src/
TESTDIR = ./tests/

# Liste des fichiers source et leurs correspondances en .class
BAKECLI_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/BakeCLI.class
TIMESTAMPMANAGER_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/TimestampManager.class
BAKEFILEPARSER_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/BakefileParser.class
DEPENDENCYRESOLVER_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/DependencyResolver.class
COMMANDEXECUTOR_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/CommandExecutor.class
BAKEENGINE_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/BakeEngine.class
RULE_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/Rule.class
MAIN_CLASS = $(BUILDDIR)fr/monlouyan/bakefile/Main.class

# Fichiers source
BAKECLI_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/BakeCLI.java
TIMESTAMPMANAGER_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/TimestampManager.java
BAKEFILEPARSER_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/BakefileParser.java
DEPENDENCYRESOLVER_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/DependencyResolver.java
COMMANDEXECUTOR_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/CommandExecutor.java
BAKEENGINE_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/BakeEngine.java
RULE_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/Rule.java
MAIN_SRC = $(SOURCEDIR)fr/monlouyan/bakefile/Main.java

# Cible principale
all: init $(MAIN_CLASS) $(JARNAME) deploy-tests

# Initialiser les répertoires nécessaires
init:
	@mkdir -p $(BUILDDIR)
	@mkdir -p $(BUILDDIR)fr/monlouyan/bakefile/

$(MAIN_CLASS): $(MAIN_SRC) $(BAKECLI_CLASS) $(BAKEENGINE_CLASS)
	@echo "Compilation de $(MAIN_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(MAIN_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(RULE_CLASS): $(RULE_SRC) $(BAKECLI_CLASS)
	@echo "Compilation de $(RULE_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(RULE_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(TIMESTAMPMANAGER_CLASS): $(TIMESTAMPMANAGER_SRC)
	@echo "Compilation de $(TIMESTAMPMANAGER_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(TIMESTAMPMANAGER_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(DEPENDENCYRESOLVER_CLASS): $(DEPENDENCYRESOLVER_SRC) $(RULE_CLASS)
	@echo "Compilation de $(DEPENDENCYRESOLVER_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(DEPENDENCYRESOLVER_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(COMMANDEXECUTOR_CLASS): $(COMMANDEXECUTOR_SRC) $(RULE_CLASS) $(TIMESTAMPMANAGER_CLASS)
	@echo "Compilation de $(COMMANDEXECUTOR_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(COMMANDEXECUTOR_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(BAKEFILEPARSER_CLASS): $(BAKEFILEPARSER_SRC) $(RULE_CLASS)
	@echo "Compilation de $(BAKEFILEPARSER_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(BAKEFILEPARSER_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(BAKEENGINE_CLASS): $(BAKEENGINE_SRC) $(BAKEFILEPARSER_CLASS) $(DEPENDENCYRESOLVER_CLASS) $(COMMANDEXECUTOR_CLASS) $(RULE_CLASS) $(BAKECLI_CLASS)
	@echo "Compilation de $(BAKEENGINE_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(BAKEENGINE_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

$(BAKECLI_CLASS): $(BAKECLI_SRC)
	@echo "Compilation de $(BAKECLI_SRC)..."
	@javac -cp $(CLASSP) -d $(BUILDDIR) $(BAKECLI_SRC) -Xlint:unchecked -Xlint:deprecation -sourcepath src

# Création du fichier JAR (on s'assure que tout est compilé)
$(JARNAME): $(MAIN_CLASS) $(RULE_CLASS) $(TIMESTAMPMANAGER_CLASS) $(DEPENDENCYRESOLVER_CLASS) $(COMMANDEXECUTOR_CLASS) $(BAKEFILEPARSER_CLASS)
	@echo "Création du fichier JAR..."
	@jar cfm $(JARNAME) $(MANIFESTPATH) -C $(BUILDDIR) fr/monlouyan/bakefile
	@echo "Fichier JAR créé : $(JARNAME)"

# Génération de la documentation Javadoc
javadoc:
	@echo "Génération de la documentation Javadoc..."
	@javadoc -d $(DOCDIR) -sourcepath $(SOURCEDIR) -subpackages $(PACKAGE)
	@echo "Documentation Javadoc générée dans $(DOCDIR)"

see-javadoc: javadoc
	@echo "Ouverture de la documentation Javadoc sur Firefox..."
	@firefox $(DOCDIR)/index.html

# Déploiement du JAR dans les répertoires de test
deploy-tests: $(JARNAME)
	@find $(TESTDIR) -type d -name 'bake' -exec cp $(JARNAME) {} \;

# Nettoyage des fichiers générés
clean:
	@echo "Nettoyage des fichiers générés..."
	@rm -rf $(BUILDDIR)
	@rm -rf $(DOCDIR)
	@rm -f $(JARNAME)
	@find $(TESTDIR) -name "$(JARNAME)" -delete
	@echo "Nettoyage terminé."

# Cibles spéciales
.PHONY: all init javadoc deploy-tests clean