# LaTeX Makefile for dvi, ps, and pdf file creation.
# MAGMA - Laboratoire Leibniz 13-12-2004

# The LATEX variable contains the latex command to use, using LATEXFLAGS
# you can specify additional flags to pass. 
TEXPATH = /usr/local/texlive/latest/bin/universal-darwin
LATEX = pdflatex
LATEXFLAGS= -interaction=scrollmode 
PDFLATEX = pdflatex
#PDFLATEXFLAGS= --jobname=$(OUTFILE)
PDFLATEXFLAGS= 
MAKEINDEX = makeindex

# Specifies the bibliography generation program.
BIBTEX = biber
BIBTEXFLAGS=

# These options are used for generating PostScript files from DVI files.
DVIPS = dvips
DVIPSFLAGSPDF = -P pdf
PS2PDF = ps2pdf
PS2PDFFLAGS =

# Commands for images convertion
FIG2DEV = /usr/bin/fig2dev
GIFTOPNM = giftopnm
PNMTOPNG = pnmtopng

# Useful commands
EGREP = egrep
ECHO = echo

# The variable USEPDFLATEX must equal 1 to use pdflatex for generating pdf file.
USEPDFLATEX = 1

# Tex directory 
TEX_DIR = .

# Figures directory for fig to eps or pdf convertion
FIGURE_DIR = figures

# Contain those LaTeX input files which will be passed directly to LaTeX, i.e.
# no include files. 
MAIN_TEX_FILES= Main.tex


# Contains include tex file
TEX_FILES= $(wildcard *.tex) \
$(wildcard ./$(TEX_DIR)/*.tex)  

# Contains the names of all DVI files to be generated when using the target<.dvi>
# target. Defaults to all files from $(MAIN_TEX_FILES) with their
# extensions changed to target<.dvi>.
DVI_FILES= $(MAIN_TEX_FILES:.tex=.dvi)

# Contains the names of all PostScript resp. PDF files to be generated when
# using the target<.ps> or target<.pdf>.
PS_FILES= $(DVI_FILES:.dvi=.ps)
CMR_FILES= $(DVI_FILES:.dvi=_CMR.pdf)
PDF_FILES= $(DVI_FILES:.dvi=.pdf)

# Contains the names of all eps resp. eps images to be generated when 
# using target<.dvi> or target<.ps> or target<.pdf>.
EPSFIGURES = $(patsubst %.fig,%.eps,$(wildcard ./$(FIGURE_DIR)/*.fig)) $(patsubst %.tex,%.eps,$(wildcard ./$(FIGURE_DIR)/*.tex))
PDFFIGURES = $(patsubst %.fig,%.pdf,$(wildcard ./$(FIGURE_DIR)/*.fig))

TODAY= $(shell date +"%Y%m%d_%H%M")
TODAY= $(shell date +"%Y%m%d")
OUTFILE=$(shell basename $(CURDIR))_$(TODAY)

# Contains the names of all configuration and styles to be linked 
# from the configuration directory
LINKS_FILES = bibliography.bib MySetting.tex latex8.bst latex8.sty latex8_unsrt.bst myColor.sty ntu_techrpt_cover.sty mycolor.cfg coverart.eps svn-multi.sty prelim2e.sty ieeeconf.cls IEEEtran.cls
#LINKS_FILES = mycolor.cfg myColor.sty
LN = ln -sf
HOME = /Users/cshih
# Target definition

#all: $(EPSFIGURES) $(PDFFIGURE)
all: link $(EPSFIGURES) $(PDFFIGURE)
	@$(ECHO) "Home directory is $(HOME)"
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "EPSFIGURES: $(EPSFIGURES)."
	@$(ECHO) "PDFFIGURES: $(PDFFIGURES)."
	@$(ECHO) "OUTFILE: $(OUTFILE)."
	@$(ECHO) "----------------------------------------------------------------"
#	$(MAKE) $(DVI_FILES) $(PS_FILES) 
#	./svnlog-gen.sh http://newslabx.csie.ntu.edu.tw/svn/R_Publications/SOCA12 log.tex
#	$(MAKE) $(DVI_FILES) $(PS_FILES) $(PDF_FILES)
	$(MAKE) $(DVI_FILES) $(PDF_FILES) 

%.dvi %.log %.aux %.toc :  $(EPSFIGURES) $(TEX_FILES)
	$(MAKE) $< 
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "Running $(LATEX) $* for the first time"
	@$(ECHO) "----------------------------------------------------------------"
	$(TEXPATH)/$(LATEX) $(LsATEXFLAGS) $* $(LATEXSTDOUT)
#  Path beamer to be sure to have navigation bar
	@if $(EGREP) -q "No file .*nav" $*.log && $(EGREP) -q "beamer" $*.log; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(LATEX) again to include navigation bar"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT); \
	fi; \
#  end beamer
	@if $(EGREP) -q "Rerun to get .*references right" $*.log; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(LATEX) again to get references right"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(LATEX) $(LATEXFLAGS) $* $(LATEXSTDOUT) ; \
	fi
	@if $(EGREP) -q '\\bib(data|cite)' $*.aux; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Making Bibliography using $(BIBTEX)"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(BIBTEX) $(BIBTEXFLAGS) $*; \
	if [ -f $*.bbl ]; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(LATEX) again to include bibliography"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(LATEX) $(LATEXFLAGS) $* $(LATEXSTDOUT); \
	fi; \
	fi
	@-count=5;\
	while $(EGREP) -q "Rerun to get .*(references|citations) (right|correct)" $*.log && [ $$count -gt 0 ]; do \
		$(ECHO) "----------------------------------------------------------------"; \
		$(ECHO) "Rerunning $(LATEX), max. $$count times left"; \
		$(ECHO) "----------------------------------------------------------------"; \
		$(TEXPATH)/$(LATEX) $(LATEXFLAGS) $* $(LATEXSTDOUT); \
		count=`expr $$count - 1`; \
	done

# Generate pdf file with pdflatex
ifdef USEPDFLATEX
%.pdf : $(TEX_FILES)
	$(MAKE) $<
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "Running $(PDFLATEX) for the first time..."
	@$(ECHO) "----------------------------------------------------------------"
	$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT)
	#  Path beamer to be sure to have navigation bar
	@if $(EGREP) -q "No file .*nav" $*.log && $(EGREP) -q "beamer" $*.log; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(PDFLATEX) again to include navigation bar"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT); \
	fi; \
	# end beamer
	@if $(EGREP) -q "Rerun to get .*references right" $*.log; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(PDFLATEX) again to get references right"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT) ; \
	fi
	@if $(EGREP) -q '\\bib(data|cite)' $*.aux; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Making Bibliography using $(BIBTEX)"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(BIBTEX) $(BIBTEXFLAGS) $*; \
	if [ -f $*.bbl ]; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Running $(PDFLATEX) again to include bibliography"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT); \
	fi; \
	fi
	@-count=5; \
	while $(EGREP) -q "Rerun to get .*(references|citations) (right|correct)" $*.log && [ $$count -gt 0 ]; do \
		$(ECHO) "----------------------------------------------------------------"; \
		$(ECHO) "Rerunning $(PDFLATEX), max. $$count times left"; \
		$(ECHO) "----------------------------------------------------------------"; \
		$(TEXPATH)/$(PDFLATEX) $(PDFLATEXFLAGS) $* $(LATEXSTDOUT); \
		count=`expr $$count - 1`; \
	done; \
	cp -f $(PDF_FILES) ~/Dropbox/2Reads/$(OUTFILE).pdf; \
	$(ECHO) "-------- DONE --------"
# Generate pdf file with dvi and dvips
else 
%.pdf : %.ps
	@if [ -s $< ] ; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Generating PDF file $@"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(PS2PDF) $(PS2PDFFLAGS) $< $@; \
	else \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Skipped creating $@: $< does not exist or is empty"; \
	$(ECHO) "----------------------------------------------------------------"; \
	fi
endif

%.ps : %.dvi
	@if [ -s $< ] ; then \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Generating PS file $@"; \
	$(ECHO) "----------------------------------------------------------------"; \
	$(TEXPATH)/$(DVIPS) $(DVIPSFLAGSPDF) $< -o  $*.ps ; \
else \
	$(ECHO) "----------------------------------------------------------------"; \
	$(ECHO) "Skipped creating $@: $< does not exist or is empty"; \
	$(ECHO) "----------------------------------------------------------------"; \
fi

cmr: $(PDF_FILES) 
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "Make CMR Ready for IEEE"
	@$(ECHO) "----------------------------------------------------------------"
	gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dEmbedAllFonts=true -dSubsetFonts=true -sOutputFile=$(CMR_FILES) -f $(PDF_FILES)
	# ps2pdf14 -dPDFSETTINGS=/prepress $(PDF_FILES) $(CMR_FILES)

cleanepsfig:
	-rm -f $(FIGURE_DIR)/*.eps $(FIGURE_DIR)/*.bak

cleanpdffig:
	-rm -f $(FIGURE_DIR)/*.pdf $(FIGURE_DIR)/*.bak

#cleanfig: cleanepsfig cleanpdffig
cleanfig:
	-rm -f $(PDFFIGURES) $(EPSFIGURES)

cleanbiber:
	rm -rf `biber --cache`

clean: cleanfig cleanbiber
	rsync -Cuavz $(PDF_FILES) ~/Dropbox/2Reads/$(OUTFILE).pdf
	-rm -f $(TEX_DIR)/*.aux
	-rm -f *.dvi *.ps *.pdf
	-rm -f *.aux *.log *.toc *.out
	-rm -f *.lof *.lot *.loa *.lol
	-rm -f *.nav *.snm *.vrb
	-rm -f *.idx *.ind *.ilg *.glo *.gls *.bbl *.blg
	-rm -f $(LINKS_FILES)

%.eps : %.fig
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "Convert $*.fig to $*.eps and $*.pdf"
	@$(ECHO) "----------------------------------------------------------------"
	$(FIG2DEV) -L eps $*.fig $*.eps
	@if [ -f $*.pdf ]; then \
	$(ECHO) "$*.pdf is there"; \
	rm -f $*.pdf; \
	fi
	epstopdf $*.eps

%.pdf : %.fig %.eps
	@$(ECHO) "----------------------------------------------------------------"
	@$(ECHO) "Convert $*.eps to $*.pdf"
	@$(ECHO) "----------------------------------------------------------------"
	if [ -f $*.eps ]; then \
	epstopdf $*.eps \
	else \
	$(FIG2DEV) -L pdf -P $*.fig  $*.pdf \
	fi

help:
	@$(ECHO) "Usage: make <target>[.dvi,.ps,.pdf]"
	@$(ECHO) "Options:"
	@$(ECHO) "  - make help display this message"
	@$(ECHO) "  - make clean remove all intermediate generated file"
	@$(ECHO) "  - make cleanfig remove all intermediate figures"

link: $(LINKS_FILES)

bibliography.bib:
	$(LN) $(HOME)/notes/bibDB/bibliography.bib .

MySetting.tex:
	$(LN) $(HOME)/notes/tex_config/MySetting.tex .

latex8.bst:
	$(LN) $(HOME)/notes/tex_config/latex8.bst .

latex8.sty:
	$(LN) $(HOME)/notes/tex_config/latex8.sty .

latex8_unsrt.bst: 
	$(LN) $(HOME)/notes/tex_config/latex8_unsrt.bst .

myColor.sty:
	$(LN) $(HOME)/notes/tex_config/myColor.sty .

svn-multi.sty:
	$(LN) $(HOME)/notes/tex_config/svn-multi.sty .

prelim2e.sty:
	$(LN) $(HOME)/notes/tex_config/prelim2e.sty .

mycolor.cfg:
	$(LN) $(HOME)/notes/tex_config/mycolor.cfg .

ntu_techrpt_cover.sty:
	$(LN) $(HOME)/notes/tex_config/ntu_techrpt_cover.sty .

coverart.eps:
	$(LN) $(HOME)/notes/tex_config/figures/coverart.eps ./figures/.

ieeeconf.cls:
	$(LN) $(HOME)/notes/tex_config/ieeeconf.cls .

IEEEtran.cls:
	$(LN) $(HOME)/notes/tex_config/IEEEtran.cls .

