# === Global variables ===
MAIN_CLASS = fr.iutfbleau.papillon.Start
BUILD_DIR = build
SRC_DIR = src/fr/iutfbleau/papillon
SRC_BUILD = fr\iutfbleau\papillon
LIB_DIR := lib
LIB := $(LIB_DIR)/mariadb
JAR_NAME = papillon.jar
DOC_DIR = doc

# === OS detection ===
ifeq ($(OS),Windows_NT)
	SEP = ;
	RM = del /Q
	RMDIR = rmdir /S /Q
else
	SEP = :
	RM = rm -f
	RMDIR = rm -rf
endif

# === Compilation ===
compile:
	@echo === Compilation du projet ===
	javac -encoding UTF-8 -cp "$(LIB_DIR)$(SEP)$(LIB_DIR)/org/mariadb/jdbc" -d $(BUILD_DIR) $(SRC_DIR)/*.java
	@echo Compilation terminee

# === Run the program ===
run:
	@echo === Execution du programme ===
ifeq ($(OS),Windows_NT)
	java -cp "$(BUILD_DIR);$(LIB_DIR);$(LIB_DIR)/mariadb;$(LIB_DIR)/mariadb/org/mariadb/jdbc" $(MAIN_CLASS)
else
	java -cp "$(BUILD_DIR):$(LIB_DIR):$(LIB_DIR)/mariadb:$(LIB_DIR)/mariadb/org/mariadb/jdbc" $(MAIN_CLASS)
endif

# === Create JAR ===
jar: compile
	@echo === Creation du JAR autonome ===
	echo Main-Class: $(MAIN_CLASS) > manifest.txt
	jar cfm $(JAR_NAME) manifest.txt -C $(BUILD_DIR) .
	@$(RM) manifest.txt 2>nul || true
	@echo JAR cree avec succes: $(JAR_NAME)

# === Run from JAR ===
run-jar:
	@echo === Execution du JAR ===
ifeq ($(OS),Windows_NT)
	java -cp "$(JAR_NAME);$(LIB_DIR);$(LIB_DIR)/mariadb;$(LIB_DIR)/mariadb/org/mariadb/jdbc" $(MAIN_CLASS)
else
	java -cp "$(JAR_NAME):$(LIB_DIR):$(LIB_DIR)/mariadb:$(LIB_DIR)/mariadb/org/mariadb/jdbc" $(MAIN_CLASS)
endif

# === Generate documentation ===
doc:
	@echo === Generation de la documentation Javadoc ===
	javadoc -d $(DOC_DIR) -encoding UTF-8 -cp "$(LIB_DIR)$(SEP)$(LIB_DIR)/org/mariadb/jdbc" $(SRC_DIR)/*.java
	@echo Documentation generee dans le dossier doc/

# === Clean build and docs ===
clean:
	@echo === Nettoyage des fichiers compiles ===
ifeq ($(OS),Windows_NT)
	$(RM) $(BUILD_DIR)\$(SRC_BUILD)\*.class 2>nul || true
	for /d %%i in ($(DOC_DIR)\*) do rmdir /S /Q "%%i" 2>nul || true
	$(RM) $(DOC_DIR)\* 2>nul || true
else
	$(RM) $(BUILD_DIR)/fr/iutfbleau/papillon/*.class
	find $(DOC_DIR) -mindepth 1 -delete 2>/dev/null || true
endif
	@echo Nettoyage termine
