DOC=$(top_srcdir)/doc
INSTDIR=$(top_srcdir)/install
TMFILES=$(wildcard *.tm)
EXFILES=$(wildcard *.cc)
TEXFILES=$(TMFILES:.tm=.tex)

DXG=$(patsubst %.tm, %.dxg, $(wildcard *.tm)) 

include $(top_srcdir)/install/makefile-tests

CLEANFILES+= $(DXG)

EXTRACT= $(top_srcdir)/install/extract.ex
TM2LATEX=$(top_srcdir)/install/tm2latex
TEX2HTML=$(top_srcdir)/install/tex2html
LATEX2DXG=$(top_srcdir)/install/latex2dxg

$(EXTRACT):
	cd $(INSTDIR); make extract.ex;
.tm.tex: $(EXTRACT)
	@$(INSTDIR)/tm2latex $*.tm
	@$(EXTRACT) $*.tex >.tmp
	@perl -pi -e "s/{\\\\quoteenv{/\\\\begin{quote}/g" .tmp
	@perl -pi -e "s/{verbatim}}}/{verbatim}\n\\\\end{quote}/g" .tmp
	@perl -pi -e "s/{verbatim} /{verbatim}\n/g" .tmp
	@perl -pi -e "s/includegraphics{/includegraphics[width=8cm]{/g" .tmp
	@perl -pi -e "s/ \\\\_ /_/g" .tmp
	@perl -pi -e "s/\\\\_ /_/g" .tmp
	@perl -pi -e "s/\\\\_ /_/g" .tmp
	@perl -pi -e "s/\\\\\"/\"/g" .tmp
	@perl -pi -e "s/ÿ//g" .tmp
	@cp .tmp $*.tex	
#	@-cp $*.tex  $(DOC)/manual
#	@echo "add $*.tex in $(DOC)/manual"

.tex.html:
	@$(TEX2HTML) $*.tex

.tex.dxg: $*.tm
	@echo "/**" > $*.dxg; 
	@$(LATEX2DXG) $*.tex >> $*.dxg; 
	@echo "**/" >> $*.dxg; 
	@echo "Generate $*.dxg"

$(top_srcdir)/expl:
	mkdir $(top_srcdir)/expl

tex: $(EXTRACT) $(TEXFILES)

pdf: tex

dxg: tex $(DXG) 
	@if test -f ./doxyfile; then \
	   export TEXINPUTS=../$(top_srcdir)/install: ;doxygen doxyfile; \
	else \
	   export TEXINPUTS=../$(top_srcdir)/install: ;doxygen $(top_srcdir)/install/doxypackage; \
	fi;
	@if test -f ./html/file.html; then grep -v "doc/" html/files.html > tmp;mv tmp html/files.html; fi;

html: tex $(DXG)
	@if test -f ./doxyfile; then \
	   export TEXINPUTS=../$(top_srcdir)/install: ;doxygen doxyfile; \
	   grep -v "doc/" html/files.html > tmp;mv tmp html/files.html; \
	fi;
	@if test -f ./html/file.html; then grep -v "doc/" html/files.html > tmp;mv tmp html/files.html; fi;
	@if test -d html; then \
	   cp $(top_srcdir)/install/logo.png html; \
	   perl -pi -e "s/div class=\"fragment\"/div class=\"fragment\" style=\"display: block;\"/g" html/*.html; \
	   cp $(top_srcdir)/install/doxygen.css html; \
	   cp $(top_srcdir)/doc/tutorial/*.cpp html; \
	fi;

