#-------------------------------------------------------------------------------
# Filename:    Makefile
# Description: LaTeX compilation flow
#-------------------------------------------------------------------------------

include ${MORPHEO_TOPLEVEL}/Makefile.tools
#--------------------------------------------------------------------------------
# User-defined options :
#--------------------------------------------------------------------------------

TYPE_DOC              	= ("doc" "ppt" "poster")

DEFAULT_LANG		= fr
FILE_SOURCE_SED  	= file_source.sed
FILE_SOURCE_STY		= file_source.sty
FILE_SOURCE_TEX		= file_source.tex

#--------------------------------------------------------------------------------
# Directory
#--------------------------------------------------------------------------------
VERSION			= "1.2" # 2007/02/21

DIR_DOCUMENTATION   	= ..
DIR_INCLUDE		= Include
DIR_PACKAGE		= Package
DIR_DOCUMENTS		= Documents
DIR_LANGUAGE		= Language
DIR_TYPE		= Type
DIR_GRAPH		= Graph
DIR_SCHEMA		= Schema
DIR_SCHEMA_EPS		= Schema_eps
DIR_SCHEMA_JPG		= Schema_jpg
DIR_TEX			= Source

#--------------------------------------------------------------------------------
# Directory
#--------------------------------------------------------------------------------
ENV_LATEX		= $(EXPORT) TEXINPUTS=$(DIR_INCLUDE):$$TEXINPUTS
LATEX_WITH_ENV		= $(ENV_LATEX); $(LATEX)

#--------------------------------------------------------------------------------
# File
#--------------------------------------------------------------------------------

ALL_FILES 		= $(wildcard $(DIR_TYPE)/*)

TEX_FILES 		= $(wildcard $(DIR_TEX)/*.tex)
FILES     		= $(patsubst $(DIR_TEX)/%.tex,%    ,$(TEX_FILES))
DVI_FILES	 	= $(patsubst $(DIR_TEX)/%.tex,%.dvi,$(TEX_FILES))
PS_FILES  		= $(patsubst $(DIR_TEX)/%.tex,%.ps ,$(TEX_FILES))
PDF_FILES 		= $(patsubst $(DIR_TEX)/%.tex,%.pdf,$(TEX_FILES))
EPS_FILES		= $(patsubst $(DIR_GRAPH)/%.p,$(DIR_SCHEMA_EPS)/%.eps,$(wildcard $(DIR_GRAPH)/*.p))		\
			  $(patsubst $(DIR_SCHEMA)/%.fig,$(DIR_SCHEMA_EPS)/%.eps,$(wildcard $(DIR_SCHEMA)/*.fig))	\
			  $(patsubst $(DIR_SCHEMA)/%.eps,$(DIR_SCHEMA_EPS)/%.eps,$(wildcard $(DIR_SCHEMA)/*.eps))

#--------------------------------------------------------------------------------
# The reset of this file shall not be editted
#--------------------------------------------------------------------------------
.PHONY 			: all clean view help doc doc_all new delete
.SECONDARY 		: $(DVI_FILES) $(PS_FILES) $(PDF_FILES) $(EPS_FILES) $(DIR_SCHEMA_EPS) $(DIR_SCHEMA_JPG)

all 			: help

doc			: $(DIR_TEX)  $(EPS_FILES) 
			@											\
			for file in $(ALL_FILES); do                                				\
				file=`$(BASENAME) $$file`;							\
				type=`$(CAT) $(DIR_TYPE)/$$file`;						\
						           		                			\
				for lang in `$(CAT) $(DIR_LANGUAGE)/$$file`; do                        		\
					if $(TEST) ! -f "$(DIR_TEX)/$$file-$$lang.tex"; then			\
			                        $(ECHO) "s/@LANG/$$lang/"                     >  $$$$;          \
			                        $(ECHO) "s/@FILE/$$file/"                     >> $$$$;          \
			                        $(ECHO) "s/@DIRDOC/$(DIR_DOCUMENTS)\/$$file/" >> $$$$;        	\
			                        $(ECHO) "s/@DIRSCHEMA/$(DIR_SCHEMA_EPS)/"     >> $$$$;        	\
		        	                $(CAT) $(DIR_INCLUDE)/$$type-$(FILE_SOURCE_SED) | $(SED) -f $$$$ > $(DIR_TEX)/$$file-$$lang.tex; \
						$(RM)   $$$$;                                        		\
					fi;									\
				done;										\
			done;                                       		                	
			@$(MAKE) doc_all

doc_all			: $(PDF_FILES)

#--------------------------------------------------------------------------------
# Generation of the formatted text:
# regular flow tex -> dvi -> ps -> pdf.
#--------------------------------------------------------------------------------
%.pdf 			: %.ps
			@$(ECHO) "Génération du fichier $*.pdf"
			@$(PS2PDF) $< $@
			@$(CP) $@ $(DIR_DOCUMENTATION)

%.ps 			: %.dvi
			@$(ECHO) "Génération du fichier $*.ps"
			@$(DVIPS) -o $@ $<

%.dvi 			: $(DIR_TEX)/%.tex
			@$(ECHO) "Génération du fichier $*.dvi"
			@#touch $(patsubst %.tex,%.ind,$<)
			@$(LATEX_WITH_ENV) $<
			@$(LATEX_WITH_ENV) $< > /dev/null
			@#$(MAKEINDEX) $(patsubst %.tex,%.idx,$<)
			@citation=`$(GREP) "citation" $*.aux`; \
	                 if $(TEST) -n "$$citation" ; then  \
			  $(BIBTEX) $* ; \
			fi
			@$(LATEX_WITH_ENV) $< > /dev/null
			@$(LATEX_WITH_ENV) $< > /dev/null

#--------------------------------------------------------------------------------
# New - Delete - Rename
#--------------------------------------------------------------------------------
new			:
			@											\
			$(ECHO) "";                       			                		\
			$(ECHO) "Choose a type of a document    : (0 is	default)";				\
			CPT=0;                                      		                		\
			declare -a type_doc=$(TYPE_DOC);							\
			for type in $${type_doc[@]}; do                                  			\
				$(ECHO) "$$CPT) $$type";                     		                	\
				CPT=$$(($$CPT+1));                  		                		\
			done;                                       		                		\
			declare -i NUM_TYPE;									\
			$(READ) NUM_TYPE;			    		                		\
														\
			if $(TEST) "$$NUM_TYPE" -ge "$$CPT"; then                               		\
				NUM_TYPE=0;                                  		        		\
			fi;											\
			declare    TYPE=$${type_doc[$$NUM_TYPE]};						\
														\
			declare    FILENAME;									\
			$(ECHO) "";                       			                		\
			$(ECHO) "Give the name of the documents :";						\
			$(READ) FILENAME;									\
														\
			if $(TEST) -z "$$FILENAME" -o -d "$(DIR_TYPE)/$$FILENAME";				\
			then											\
				$(ECHO) "Invalid name : string is empty, or filename is already used";		\
				$(EXIT);									\
			fi;											\
														\
			$(ECHO) "Create file      : $(DIR_LANGUAGE)/$$FILENAME";                 		\
			$(ECHO) "                   (Default language : $(DEFAULT_LANG))";			\
			$(ECHO) "$(DEFAULT_LANG)" > $(DIR_LANGUAGE)/$$FILENAME;                  		\
														\
			$(ECHO) "Create file      : $(DIR_TYPE)/$$FILENAME";                    		\
			$(ECHO) $$TYPE > $(DIR_TYPE)/$$FILENAME;						\
														\
			$(ECHO) "Create file      : $(DIR_PACKAGE)/$$FILENAME.sty";                    		\
			$(CP) $(DIR_INCLUDE)/$$TYPE-$(FILE_SOURCE_STY) $(DIR_PACKAGE)/$$FILENAME.sty;		\
														\
			$(ECHO) "Create file      : $(DIR_PACKAGE)/$$FILENAME.tex";                    		\
			$(CP) $(DIR_INCLUDE)/$$TYPE-$(FILE_SOURCE_TEX) $(DIR_PACKAGE)/$$FILENAME.tex;		\
														\
			$(ECHO) "Create directory : $(DIR_DOCUMENTS)/$$FILENAME";                    		\
			$(ECHO) "Create directory : $(DIR_DOCUMENTS)/$$FILENAME/common";              		\
			$(ECHO) "Create directory : $(DIR_DOCUMENTS)/$$FILENAME/$(DEFAULT_LANG)"; 		\
			$(MKDIR) $(DIR_DOCUMENTS)/$$FILENAME/common;						\
			$(MKDIR) $(DIR_DOCUMENTS)/$$FILENAME/$(DEFAULT_LANG);					\
														\
			$(ECHO) "Create file      : $(DIR_DOCUMENTS)/$$FILENAME/$(DEFAULT_LANG)/root.tex"; 	\
			$(TOUCH) $(DIR_DOCUMENTS)/$$FILENAME/$(DEFAULT_LANG)/root.tex

delete			:
			@											\
			declare -a FILES;									\
			declare -i CPT;         								\
			declare -i NUM_FILE;									\
			declare -i NUM_FILE_CONFIRM;								\
														\
			$(ECHO) "";                       			                		\
			$(ECHO) "Choose a document  : (0 by default)";		                		\
			$(ECHO) "0) ( none )";                               	                		\
														\
			CPT=1;                                      		                		\
			for files in $(ALL_FILES); do                                  				\
				files=`$(BASENAME) $$files`;                     				\
				$(ECHO) "$$CPT) $$files";           		                		\
				FILES[$$CPT]=$$files;				                		\
				CPT=$$(($$CPT+1));                  		                		\
			done;                                       		                		\
			$(READ) NUM_FILE;			    		                		\
														\
			if $(TEST) "$$NUM_FILE" -ge "$$CPT"; then                               		\
				NUM_FILE=0;                                  		        		\
			fi;											\
														\
			$(ECHO) "Please reenter the document id to confirm the delete";         		\
			$(READ) NUM_FILE_CONFIRM;		    		                		\
														\
			if $(TEST) "$$NUM_FILE" -eq "0" -o "$$NUM_FILE" -ne "$$NUM_FILE_CONFIRM"; then          \
				$(ECHO) "Delete cancelled";							\
				$(EXIT);                                       		        		\
			fi;											\
														\
			declare    FILENAME;									\
			FILENAME=$${FILES[$$NUM_FILE]};					      			\
														\
			$(ECHO) "Delete file      : $(DIR_TYPE)/$$FILENAME";                   			\
			$(RM) $(DIR_TYPE)/$$FILENAME;                 						\
														\
			$(ECHO) "Delete file      : $(DIR_LANGUAGE)/$$FILENAME";                   		\
			$(RM) $(DIR_LANGUAGE)/$$FILENAME;                 					\
														\
			$(ECHO) "Delete file      : $(DIR_PACKAGE)/$$FILENAME.sty";                    		\
			$(RM) $(DIR_PACKAGE)/$$FILENAME.sty;							\
														\
			$(ECHO) "Delete file      : $(DIR_PACKAGE)/$$FILENAME.tex";                    		\
			$(RM) $(DIR_PACKAGE)/$$FILENAME.tex;							\
														\
			$(ECHO) "Delete directory : $(DIR_DOCUMENTS)/$$FILENAME";                    		\
			$(RM) $(DIR_DOCUMENTS)/$$FILENAME;

rename			:
			@											\
			declare -a FILES;									\
			declare -i CPT;         								\
			declare -i NUM_FILE;									\
														\
			$(ECHO) "";                       			                		\
			$(ECHO) "Choose a document  : (0 by default)";		                		\
			$(ECHO) "0) ( none )";                               	                		\
														\
			CPT=1;                                      		                		\
			for files in $(ALL_FILES); do                                  				\
				files=`$(BASENAME) $$files`;                   					\
				$(ECHO) "$$CPT) $$files";           		                		\
				FILES[$$CPT]=$$files;				                		\
				CPT=$$(($$CPT+1));                  		                		\
			done;                                       		                		\
			$(READ) NUM_FILE;			    		                		\
														\
			if $(TEST) "$$NUM_FILE" -ge "$$CPT"; then                               		\
				NUM_FILE=0;                                  		        		\
			fi;											\
														\
			if $(TEST) "$$NUM_FILE" -eq "0"; then              					\
				$(ECHO) "Rename cancelled";							\
				$(EXIT);                                       		        		\
			fi;											\
														\
			declare    FILENAME_SRC;								\
			declare    FILENAME_DEST;								\
			FILENAME_SRC=$${FILES[$$NUM_FILE]};					      		\
														\
			$(ECHO) "Give the new name of the documents :";						\
			$(READ) FILENAME_DEST;									\
														\
			if $(TEST) -z "$$FILENAME_DEST" -o -d "$(DIR_TYPE)/$$FILENAME_DEST";			\
			then											\
				$(ECHO) "Invalid name : string is empty, or filename is already used";		\
				$(EXIT);									\
			fi;											\
														\
			$(ECHO) "Rename file      : $(DIR_LANGUAGE)/$$FILENAME_SRC";                 		\
			$(MV) $(DIR_LANGUAGE)/$$FILENAME_SRC    $(DIR_LANGUAGE)/$$FILENAME_DEST;               	\
														\
			$(ECHO) "Rename file      : $(DIR_TYPE)/$$FILENAME_SRC";                 		\
			$(MV) $(DIR_TYPE)/$$FILENAME_SRC        $(DIR_TYPE)/$$FILENAME_DEST;               	\
														\
			$(ECHO) "Rename file      : $(DIR_PACKAGE)/$$FILENAME_SRC.sty";                    	\
			$(MV) $(DIR_PACKAGE)/$$FILENAME_SRC.sty $(DIR_PACKAGE)/$$FILENAME_DEST.sty;		\
														\
			$(ECHO) "Rename directory : $(DIR_DOCUMENTS)/$$FILENAME_SRC";                    	\
			$(MV) $(DIR_DOCUMENTS)/$$FILENAME_SRC   $(DIR_DOCUMENTS)/$$FILENAME_DEST;

#--------------------------------------------------------------------------------
# View
#--------------------------------------------------------------------------------
view			: doc
			@                                           		                		\
			declare    SELECT_FILE;									\
			declare    SELECT_LANG;									\
			declare    SELECT_FORMAT;								\
			declare    SELECT_VIEWER;								\
														\
														\
			$(ECHO) "";                       			                		\
			$(ECHO) "Choose a document  : (0 by default)";		                		\
														\
			declare -i CPT;         								\
			declare -i NUM_FILE;									\
			declare -a FILES;									\
														\
			CPT=0;                                      		                		\
			for files in $(ALL_FILES); do                                  				\
				files=`$(BASENAME) $$files`;                   					\
				type=`$(CAT) $(DIR_TYPE)/$$files`;						\
				$(ECHO) "$$CPT) $$type - $$files";           		                	\
				FILES[$$CPT]=$$files;				                		\
				CPT=$$(($$CPT+1));                  		                		\
			done;                                       		                		\
			$(READ) NUM_FILE;			    		                		\
														\
			if $(TEST) "$$NUM_FILE" -ge "$$CPT"; then                               		\
				NUM_FILE=0;                                  		        		\
			fi;											\
														\
			SELECT_FILE=$${FILES[$$NUM_FILE]};							\
			$(ECHO) "Document selected  : $$SELECT_FILE";                           		\
			$(ECHO) "";                                 		                		\
														\
														\
														\
			$(ECHO) "Choose a language  : (0 by default)";	                        		\
														\
			declare -i NUM_LANG;									\
			declare -a LANG;      									\
														\
			CPT=0;						                        		\
			for lang in `$(CAT) $(DIR_LANGUAGE)/$$SELECT_FILE`; do					\
			    	$(ECHO) "$$CPT) $$lang";				                	\
				LANG[$$CPT]=$$lang;				                		\
				CPT=$$(($$CPT+1));				                        	\
			done;						                        		\
			$(READ) NUM_LANG;			    	                        		\
														\
			if $(TEST) "$$NUM_LANG" -ge "$$CPT"; then                             			\
				NUM_LANG=0;	 			                        		\
			fi;						                        		\
														\
			SELECT_LANG=$${LANG[$$NUM_LANG]};							\
			$(ECHO) "Language selected  : $$SELECT_LANG";                       			\
			$(ECHO) "";                                 		                		\
														\
														\
														\
			$(ECHO) "Choose a format    : (0 by default)";	                        		\
														\
			declare -i NUM_FORMAT;									\
			declare -a FORMAT;      								\
			declare -a VIEWER;      								\
														\
			FORMAT=("pdf"         "pdf"      "ps"     "dvi");		                        \
			VIEWER=($(VIEWPDF_FS) $(VIEWPDF) $(VIEWPS) $(VIEWDVI));	                        	\
														\
			CPT=0;						                        		\
			while [ $$CPT -lt $${#FORMAT[*]} ]; do		                        		\
			    $(ECHO) "$$CPT) $${FORMAT[$$CPT]} ($${VIEWER[$$CPT]})";		                \
			    CPT=$$(($$CPT+1));				                        		\
			done;						                        		\
			$(READ) NUM_FORMAT;			    	                        		\
														\
			if $(TEST) "$$NUM_FORMAT" -ge "$$CPT"; then                             		\
				NUM_FORMAT=0;	 			                        		\
			fi;						                        		\
														\
			SELECT_FORMAT=$${FORMAT[$$NUM_FORMAT]};							\
			SELECT_VIEWER=$${VIEWER[$$NUM_FORMAT]};							\
			$(ECHO) "Format selected    : $$SELECT_FORMAT";                         		\
			$(ECHO) "";                                 		                		\
														\
														\
														\
			$$SELECT_VIEWER "$$SELECT_FILE-$$SELECT_LANG.$$SELECT_FORMAT" &                		\
														\
			$(ECHO) "";

#--------------------------------------------------------------------------------
# The figures directory
#--------------------------------------------------------------------------------
$(DIR_SCHEMA_EPS)	:
			@$(ECHO) "Make directory       : $@"
			@$(MKDIR) $@

$(DIR_SCHEMA_JPG)	:
			@$(ECHO) "Make directory       : $@"
			@$(MKDIR) $@

$(DIR_TEX)       	:
			@$(ECHO) "Make directory       : $@"
			@$(MKDIR) $@

$(DIR_SCHEMA_EPS)/%.eps	: $(DIR_SCHEMA)/%.eps $(DIR_SCHEMA_EPS) $(DIR_SCHEMA_JPG)
			@$(ECHO) "Generate   files     : $*.eps"
			@$(CP) $(DIR_SCHEMA)/$*.eps $(DIR_SCHEMA_EPS)
			@$(EPS2JPG) $@ $(DIR_SCHEMA_JPG)/$*.jpg

$(DIR_SCHEMA_EPS)/%.eps	: $(DIR_SCHEMA)/%.fig $(DIR_SCHEMA_EPS) $(DIR_SCHEMA_JPG)
			@$(ECHO) "Generate   files     : $*.eps"
			@$(FIG2EPS) $< $@
			@$(EPS2JPG) $@ $(DIR_SCHEMA_JPG)/$*.jpg

$(DIR_SCHEMA_EPS)/%.eps	: $(DIR_GRAPH)/%.p    $(DIR_SCHEMA_EPS) $(DIR_SCHEMA_JPG)
			@$(ECHO) "Generate   files     : $*.eps"
			@$(CD) $(DIR_GRAPH); $(GNUPLOT) $*.p
			@$(EPS2JPG) $@ $(DIR_SCHEMA_JPG)/$*.jpg

#--------------------------------------------------------------------------------
# Cleaning
#--------------------------------------------------------------------------------

clean			:
			@$(ECHO) "Delete     temporary files              "$(PWD)
			@$(RM) $(DIR_SCHEMA_EPS) $(DIR_SCHEMA_JPG)  $(DIR_TEX) $(DIR_PACKAGE)/*.aux
			@$(RM) $(DVI_FILES) $(PS_FILES) $(PDF_FILES)
			@$(MAKE) clean_rec DIR_CLEAN=.  

#Clean recursive
clean_rec		:
			@$(ECHO) "Delete     temporary files in directory $(DIR_CLEAN)"
			@$(RM)	$(DIR_CLEAN)/*~ 		\
				$(DIR_CLEAN)/*.bak  		\
				$(DIR_CLEAN)/*.aux  		\
				$(DIR_CLEAN)/*.bbl  		\
				$(DIR_CLEAN)/*.blg  		\
				$(DIR_CLEAN)/*.bmt  		\
				$(DIR_CLEAN)/*.log  		\
				$(DIR_CLEAN)/*.mtc* 		\
				$(DIR_CLEAN)/*.toc  		\
				$(DIR_CLEAN)/*.idx  		\
				$(DIR_CLEAN)/*.ilg  		\
				$(DIR_CLEAN)/*.ind  		\
				$(DIR_CLEAN)/*.out  		\
				$(DIR_CLEAN)/*.nav  		\
				$(DIR_CLEAN)/*.snm  		\
				$(DIR_CLEAN)/missfont.log
			@# The .bbl file is to be removed if "bibtex" is used.
			@for files in `$(LS) $(DIR_CLEAN)`; do                              			\
				if $(TEST) -d $(DIR_CLEAN)/$$files;                         			\
				then                                                        			\
					$(MAKE) clean_rec DIR_CLEAN=$(DIR_CLEAN)/$$files;   			\
				fi;                                                         			\
			done;

clean_all		: clean
			@$(ECHO) "Delete     temporary files (all)"
			@$(RM) $(DIR_DOCUMENTATION)/*.pdf

#--------------------------------------------------------------------------------
# Help
#--------------------------------------------------------------------------------

help			:
			@$(ECHO) "Environnement to generate documents and presentations"
			@$(ECHO) "Version         : $(VERSION)"
			@$(ECHO) ""
			@$(ECHO) "List of rules   :"
			@$(ECHO) " * all          : Cf rules \"help\""
			@$(ECHO) " * clean        : Erase generate files"
			@$(ECHO) " * clean_all    : Erase all generate files"
			@$(ECHO) " * delete       : Erase all file and directory of a document"
			@$(ECHO) " * doc          : Generate all documents"
			@$(ECHO) " * help         : Print this message"
			@$(ECHO) " * new          : Create all file and directy for a new document"
			@$(ECHO) " * rename       : Rename a document"
			@$(ECHO) " * view         : List all document to view"
			@$(ECHO) ""
