VERSION_MAJOR = 2
VERSION_MINOR = 1

DIRMARKOV = markov/
DIRGRAMMAR = grammar/
DIRJFLEX = ..
DIRJAVACUP = ..
FINALDISTR = distrib-GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)
DIRTMPDISTR = tmpDistr
DIRTMPDISTRGENRGENS = $(DIRTMPDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)

JAVAC    = javac -classpath ..
JAVACUP  = java -cp $(DIRJAVACUP) java_cup.Main
JAVAFLEX = java -cp $(DIRJFLEX) JFlex.Main
ZIPTOOL  = zip -9 -q -r 
TARTOOL  = tar -cf
TGZTOOL  = tar -czf 



LEX_PARSE = grammar/GenRGenSScanner.flex markov/GenRGenSScanner.flex master/GenRGenSScanner.flex ratexpr/GenRGenSScanner.flex grammar/grammar.cup markov/markov.cup master/master.cup ratexpr/rational.cup ratexpr/preparse.cup

LEX_SOURCES = grammar/GenRGenSScanner.java markov/GenRGenSScanner.java  ratexpr/GenRGenSScanner.java master/GenRGenSScanner.java
CUP_SOURCES = grammar/ParseGrammar.java markov/ParseMarkov.java  ratexpr/parser.java ratexpr/preparser.java  master/ParseMaster.java  grammar/sym.java markov/sym.java  ratexpr/sym.java  master/sym.java
LEXCUP_SOURCES = $(LEX_SOURCES) $(CUP_SOURCES)

GenRGenS_SOURCES = GenRGenS.java GenerationException.java GenerationLink.java Generator.java GeneratorCreationException.java Linker.java GeneratorConfigFile.java  LexicalException.java WorkProgress.java
GenRGenS_IU_SOURCES = Main.java IU/ComptageDialog.java IU/GenRGenSDialog.java IU/HelpDialog.java IU/InOrderJMenu.java IU/IntField.java IU/JTextAreaOutputStream.java IU/MainFrame.java IU/MainFramePrintStream.java IU/MainFrame_AboutBox.java IU/MenuAction.java IU/StringOutputStream.java IU/TextAreaPrintStream.java IU/GenerationSpecificOptionsPanel.java IU/Option.java IU/ProgressDialog.java
GenRGenS_APPLET_SOURCES = Packages.java Applet/MainApplet.java Applet/EasyPrint.java  Applet/AWTGenerationSpecificOptionsPanel.java
UTILITIES_SOURCES =  utility/FileTooSmallException.java utility/IntArray.java utility/LightState.java utility/RandomBigDecimal.java utility/Couple.java utility/FileExtensionFilter.java  utility/ParseRule.java utility/CommandLineParser.java utility/Comptage.java
HOLDER_SOURCES = utility/holder/BooleanHolder.java utility/holder/CharHolder.java utility/holder/Holder.java utility/holder/IntHolder.java utility/holder/StringHolder.java
CALLBACKS_SOURCES = utility/callbacks/Callback.java utility/callbacks/CharCallback.java utility/callbacks/BooleanCallback.java utility/callbacks/IntCallback.java utility/callbacks/StringCallback.java utility/callbacks/VoidCallback.java  utility/callbacks/RealCallback.java

MARKOV_SOURCES = markov/BadMarkovException.java markov/Markov.java markov/MarkovianGenerationConfiguration.java markov/BuildMarkov.java

GRAMMAR_SOURCES = grammar/BadGrammarException.java grammar/ContextFreeGrammar.java grammar/ContextFreeGrammarProduction.java grammar/CountedNCFG.java grammar/DoubleCountedNCFG.java grammar/NormalContextFreeGrammar.java grammar/NormalContextFreeGrammarProduction.java grammar/NormalContextFreeGrammarProductionProduct.java grammar/NormalContextFreeGrammarProductionTerminal.java grammar/NormalContextFreeGrammarProductionUnion.java grammar/GrammarGenerationConfiguration.java

MASTER_SOURCES = master/BadArgumentTypeException.java master/BadMasterFileException.java master/DistributionLaw.java master/Master.java master/WrongNumberOfArgumentsException.java master/MasterGenerationConfiguration.java master/distributions/Addition.java master/distributions/Ceil.java master/distributions/ConstantDouble.java master/distributions/ConstantLong.java master/distributions/Division.java master/distributions/Floor.java master/distributions/Gaussian.java master/distributions/Log.java master/distributions/Pow.java master/distributions/Product.java master/distributions/Round.java master/distributions/SequenceLength.java master/distributions/Substraction.java master/distributions/Uniform.java master/distributions/Min.java master/distributions/Max.java master/distributions/Var.java master/distributions/VarDecl.java 

RATEXPR_SOURCES = ratexpr/Automate.java ratexpr/BadArgumentException.java ratexpr/Etat.java ratexpr/EtatCompose.java ratexpr/GeneratorRational.java ratexpr/GeneratorRationalConfigFile.java ratexpr/RatExpr.java ratexpr/RatExprEtoile.java ratexpr/RatExprOu.java ratexpr/RatExprPlus.java ratexpr/RatExprPoint.java ratexpr/RatExprPuiss.java ratexpr/RatExprTerm.java ratexpr/TestGeneration.java

SOURCES =  $(UTILITIES_SOURCES) $(CALLBACKS_SOURCES) $(HOLDER_SOURCES) $(CUP_SOURCES) $(LEX_SOURCES) $(GenRGenS_SOURCES) $(MARKOV_SOURCES) $(RATEXPR_SOURCES) $(GenRGenS_IU_SOURCES) $(GRAMMAR_SOURCES) $(MASTER_SOURCES) $(GenRGenS_APPLET_SOURCES)
CLASSES = $(SOURCES:.java=.class)



all: GenRGenS

GenRGenS: $(SOURCES) $(CLASSES)

Master: GenRGenS

distribution :
	tar -C .. -cvf ../tmp123456789.tar  $(patsubst %,GenRGenS/%,$(filter-out $(LEXCUP_SOURCES),$(SOURCES))) GenRGenS/Makefile $(patsubst %,GenRGenS/%,$(LEX_PARSE)) JFlex java_cup GenRGenS/Manifest.MF README LICENCE GenRGenS/IU/bin
	rm -rf ../$(FINALDISTR)
	rm -rf ../$(DIRTMPDISTR)
	mkdir ../$(FINALDISTR)
	mkdir ../$(DIRTMPDISTR)
	mkdir ../$(DIRTMPDISTRGENRGENS)
	tar -xvf ../tmp123456789.tar -C ../$(DIRTMPDISTRGENRGENS)
	rm -f ../tmp123456789.tar
	cd ../$(DIRTMPDISTR) && $(ZIPTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-src.zip  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)
	cd ../$(DIRTMPDISTRGENRGENS) && jar cfe ../../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-src.jar GenRGenS.Main  .
	$(TARTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-src.tar -C ../$(DIRTMPDISTR)  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR) 
	$(TGZTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-src.tgz -C ../$(DIRTMPDISTR)  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR) 
	make -C ../$(DIRTMPDISTRGENRGENS)/GenRGenS
	cd ../$(DIRTMPDISTR) && $(ZIPTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-bin.zip  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)
	cd ../$(DIRTMPDISTRGENRGENS) && jar cfe ../../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-bin.jar GenRGenS.Main  .
	$(TARTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-bin.tar -C ../$(DIRTMPDISTR)  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR) 
	$(TGZTOOL) ../$(FINALDISTR)/GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR)-bin.tgz -C ../$(DIRTMPDISTR)  GenRGenS$(VERSION_MAJOR).$(VERSION_MINOR) 
	#rm -rf ../$(DIRTMPDISTR)

# generic rules :
#################

.SUFFIXES: .mli .ml .cmi .cmo .cmx .mll .mly .java .class

.mll.ml:
	ocamllex $<

.mly.ml: 
	ocamlyacc $<

.mli.cmi:
	$(JAVAC) -c $(BYTEFLAGS) $<

.ml.cmo:
	$(JAVAC) -c $(BYTEFLAGS) $<

.ml.o:
	$(JAVACOPT) -c $(OPTFLAGS) $<

.ml.cmx:
	$(JAVACOPT) -c $(OPTFLAGS) $<

.java.class :
	$(JAVAC) $<

grammar/GenRGenSScanner.java : grammar/GenRGenSScanner.flex
	$(JAVAFLEX) -d grammar grammar/GenRGenSScanner.flex

markov/GenRGenSScanner.java : markov/GenRGenSScanner.flex
	$(JAVAFLEX) -d markov markov/GenRGenSScanner.flex

master/GenRGenSScanner.java : master/GenRGenSScanner.flex
	$(JAVAFLEX) -d master master/GenRGenSScanner.flex

ratexpr/GenRGenSScanner.java : ratexpr/GenRGenSScanner.flex
	$(JAVAFLEX) -d ratexpr ratexpr/GenRGenSScanner.flex

grammar/ParseGrammar.java grammar/sym.java : grammar/grammar.cup
	$(JAVACUP) -parser ParseGrammar grammar/grammar.cup 
	mv ParseGrammar.java sym.java grammar/

markov/ParseMarkov.java markov/sym.java : markov/markov.cup
	$(JAVACUP) -parser ParseMarkov markov/markov.cup 
	mv ParseMarkov.java sym.java markov/

master/ParseMaster.java master/sym.java : master/master.cup
	$(JAVACUP) -parser ParseMaster master/master.cup 
	mv ParseMaster.java sym.java master/

ratexpr/parser.java ratexpr/preparser.java ratexpr/sym.java : ratexpr/rational.cup ratexpr/preparse.cup
	$(JAVACUP) -parser preparser ratexpr/preparse.cup 
	$(JAVACUP) -parser parser ratexpr/rational.cup 
	mv parser.java preparser.java sym.java ratexpr/

# clean and doc
##################

GENERATED = $(LEX_SOURCES) $(CUP_SOURCES)
WINDOW_TITLE = 'GenRGenS v1.0'
PACKAGES = GenRGenS.utility.callbacks GenRGenS.utility GenRGenS.ratexpr GenRGenS.master GenRGenS.grammar GenRGenS.markov  GenRGenS
DOCTITLE = 'GenRGenS<sup>v1.0</sup>'


clean :
	rm -f $(GENERATED) 
	rm -f $(CLASSES)

doc :
	javadoc -use -version -author -private -splitindex -sourcepath .. -d ../javadoc -windowtitle $(WINDOW_TITLE) -doctitle $(DOCTITLE) $(PACKAGES)