# Outils 
JAVAC = javac
JAVA = java
JAVADOC = javadoc
ARGS =

# Dossiers 
SRC = src
BIN = build
DOC = docjava
LIB = lib/

# Package 
PKG_PATH = fr/iutfbleau/sae

# Points d’entrée 
MAIN_CONVERTER = fr.iutfbleau.sae.Convertisseur
MAIN_VIEWER    = fr.iutfbleau.sae.Viewer

# Séparateur classpath
ifeq ($(OS),Windows_NT)
    SEP = ;
else
    SEP = :
endif

#  Règle par défaut 
all: \
	$(BIN)/$(PKG_PATH)/Convertisseur.class \
	$(BIN)/$(PKG_PATH)/Viewer.class

# Compilation des classes main
$(BIN)/$(PKG_PATH)/Convertisseur.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/ConverterController.class \
	$(BIN)/$(PKG_PATH)/ConverterWindow.class \
	$(BIN)/$(PKG_PATH)/ExportButtonListener.class \
	$(SRC)/$(PKG_PATH)/Convertisseur.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/Convertisseur.java

$(BIN)/$(PKG_PATH)/Viewer.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/ViewerWindow.class \
	$(BIN)/$(PKG_PATH)/ViewerControleur.class \
	$(SRC)/$(PKG_PATH)/Viewer.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/Viewer.java

# Dossiers 
$(BIN):
	mkdir -p $(BIN)

$(DOC):
	mkdir -p $(DOC)


$(BIN)/$(PKG_PATH)/mpif/DecodeNode.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mpif/DecodeNode.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/DecodeNode.java

$(BIN)/$(PKG_PATH)/mpif/BitInputStream.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mpif/BitInputStream.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/BitInputStream.java

$(BIN)/$(PKG_PATH)/mpif/BitOutputStream.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mpif/BitOutputStream.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/BitOutputStream.java

$(BIN)/$(PKG_PATH)/GestionErreur.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/GestionErreur.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/GestionErreur.java
	
$(BIN)/$(PKG_PATH)/mhuffman/HuffmanNode.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mhuffman/HuffmanNode.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/HuffmanNode.java



# Compilation des classes mhuffman
$(BIN)/$(PKG_PATH)/mhuffman/ComparateurHuffmanNode.class: $(BIN) \
    $(SRC)/$(PKG_PATH)/mhuffman/ComparateurHuffmanNode.java 
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/ComparateurHuffmanNode.java

$(BIN)/$(PKG_PATH)/mhuffman/ComparateurCanonique.class: $(BIN) \
    $(SRC)/$(PKG_PATH)/mhuffman/ComparateurCanonique.java 
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/ComparateurCanonique.java

$(BIN)/$(PKG_PATH)/mhuffman/CanonicalCode.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mhuffman/CanonicalCode.java \
	$(BIN)/$(PKG_PATH)/mhuffman/ComparateurCanonique.class
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/CanonicalCode.java

$(BIN)/$(PKG_PATH)/mhuffman/FrequencyTable.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mpif/RGBImage.class \
	$(SRC)/$(PKG_PATH)/mhuffman/FrequencyTable.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/FrequencyTable.java

$(BIN)/$(PKG_PATH)/mhuffman/HuffmanTree.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mhuffman/HuffmanNode.class \
	$(BIN)/$(PKG_PATH)/mhuffman/ComparateurHuffmanNode.class \
	$(SRC)/$(PKG_PATH)/mhuffman/HuffmanTree.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mhuffman/HuffmanTree.java



# Compilation des classe mpif
$(BIN)/$(PKG_PATH)/mpif/ComparateurEntreeCanonique.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mpif/ComparateurEntreeCanonique.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/ComparateurEntreeCanonique.java

$(BIN)/$(PKG_PATH)/mpif/Pixel.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/mpif/Pixel.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/Pixel.java

$(BIN)/$(PKG_PATH)/mpif/RGBImage.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mpif/Pixel.class \
	$(SRC)/$(PKG_PATH)/mpif/RGBImage.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/RGBImage.java

$(BIN)/$(PKG_PATH)/mpif/PIFWriter.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mpif/RGBImage.class \
	$(BIN)/$(PKG_PATH)/mpif/BitOutputStream.class \
	$(SRC)/$(PKG_PATH)/mpif/PIFWriter.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/PIFWriter.java

$(BIN)/$(PKG_PATH)/mpif/PIFReader.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mpif/RGBImage.class \
	$(BIN)/$(PKG_PATH)/mpif/BitInputStream.class \
	$(BIN)/$(PKG_PATH)/mpif/DecodeNode.class \
	$(BIN)/$(PKG_PATH)/mpif/ComparateurEntreeCanonique.class \
	$(SRC)/$(PKG_PATH)/mpif/PIFReader.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/mpif/PIFReader.java






# Interface graphique
$(BIN)/$(PKG_PATH)/ImagePreviewPanel.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/ImagePreviewPanel.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/ImagePreviewPanel.java

$(BIN)/$(PKG_PATH)/FrequencyTablePanel.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/FrequencyTablePanel.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/FrequencyTablePanel.java

$(BIN)/$(PKG_PATH)/CodeTablePanel.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/CodeTablePanel.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/CodeTablePanel.java

$(BIN)/$(PKG_PATH)/ViewerWindow.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/ViewerImagePanel.class \
	$(SRC)/$(PKG_PATH)/ViewerWindow.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/ViewerWindow.java

$(BIN)/$(PKG_PATH)/ViewerImagePanel.class: $(BIN) \
	$(SRC)/$(PKG_PATH)/ViewerImagePanel.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/ViewerImagePanel.java





# GROSSE compilation du listener + ConvertController + ConvertWindow car il y a une dependance cirulaire
$(BIN)/$(PKG_PATH)/ConverterController.class \
$(BIN)/$(PKG_PATH)/ExportButtonListener.class \
$(BIN)/$(PKG_PATH)/ThreadSauvegardePIF.class: 
$(BIN)/$(PKG_PATH)/ConverterWindow.class: \
$(SRC)/$(PKG_PATH)/ConverterController.java \
$(SRC)/$(PKG_PATH)/ExportButtonListener.java \
$(SRC)/$(PKG_PATH)/ThreadSauvegardePIF.java \
$(SRC)/$(PKG_PATH)/ConverterWindow.java \
$(BIN)/$(PKG_PATH)/mpif/Pixel.class \
$(BIN)/$(PKG_PATH)/mpif/RGBImage.class \
$(BIN)/$(PKG_PATH)/mhuffman/FrequencyTable.class \
$(BIN)/$(PKG_PATH)/mhuffman/HuffmanTree.class \
$(BIN)/$(PKG_PATH)/mhuffman/CanonicalCode.class \
$(BIN)/$(PKG_PATH)/ImagePreviewPanel.class \
$(BIN)/$(PKG_PATH)/FrequencyTablePanel.class \
$(BIN)/$(PKG_PATH)/CodeTablePanel.class \
$(BIN)/$(PKG_PATH)/ThreadSauvegardePIF.class \
$(BIN)/$(PKG_PATH)/GestionErreur.class \
$(BIN)/$(PKG_PATH)/mpif/PIFWriter.class | $(BIN)
	@$(JAVAC) -cp $(BIN) -d $(BIN) \
	$(SRC)/$(PKG_PATH)/ConverterController.java \
	$(SRC)/$(PKG_PATH)/ExportButtonListener.java \
	$(SRC)/$(PKG_PATH)/ThreadSauvegardePIF.java \
	$(SRC)/$(PKG_PATH)/ConverterWindow.java

#Controleur de viewer
$(BIN)/$(PKG_PATH)/ViewerControleur.class: $(BIN) \
	$(BIN)/$(PKG_PATH)/mpif/RGBImage.class \
	$(BIN)/$(PKG_PATH)/GestionErreur.class \
	$(BIN)/$(PKG_PATH)/mpif/PIFReader.class \
	$(SRC)/$(PKG_PATH)/ViewerControleur.java
	$(JAVAC) -cp $(BIN) -d $(BIN) $(SRC)/$(PKG_PATH)/ViewerControleur.java




# Exécution sans JAR
runnotjar-conv: $(BIN)/$(PKG_PATH)/Convertisseur.class
	$(JAVA) -cp $(BIN) $(MAIN_CONVERTER) $(ARGS)

runnotjar-view: $(BIN)/$(PKG_PATH)/Viewer.class
	$(JAVA) -cp $(BIN) $(MAIN_VIEWER) $(ARGS)

# Création des JAR
jar-conv: $(BIN)/$(PKG_PATH)/Convertisseur.class
	jar cfe pifConverter.jar $(MAIN_CONVERTER) -C $(BIN) .

jar-view: $(BIN)/$(PKG_PATH)/Viewer.class
	jar cfe pifViewer.jar $(MAIN_VIEWER) -C $(BIN) .

jar: jar-conv jar-view

# Exécution des JAR
run-conv: jar-conv
	$(JAVA) -jar pifConverter.jar $(ARGS)

run-view: jar-view
	$(JAVA) -jar pifViewer.jar $(ARGS)

# Documentation 
doc: $(DOC)
	@echo "Generation de la documentation en cours..."
	@$(JAVADOC) -Xdoclint:none -encoding UTF-8 -d $(DOC) -sourcepath $(SRC) -subpackages fr.iutfbleau.sae > /dev/null 2>&1
	@echo "Termine ! Documentation disponible : $(DOC)/index.html"


# Nettoyage 
clean:
	rm -rf $(BIN) $(DOC) *.jar
