#****************************************************************************
#** LaTeX Essentials -- HowTo Create Your LaTeX-based Documentation
#****************************************************************************

#****************************************************************************
#** Copyright 2002 by Lukas Ruf, ruf@topsy.net
#**
#** Copyright 2003 by Oliver Erdmann, 2003-08-28
#** Great additions
#**
#** Copyright 2003 by Lukas Ruf, ruf@topsy.net
#** Manicure
#**
#** Copyright 2004 by Lukas Ruf, ruf@topsy.net
#** Extension with 'helper applications'.
#**
#** Information is provided under the terms of the
#** GNU Free Documentation License <http://www.gnu.org/copyleft/fdl.html>
#** Fairness: Cite the source of information, visit <http://www.topsy.net>
#****************************************************************************


#****************************************************************************
#** DEBUG defines whether debugging output should be displayed
DEBUG =


#****************************************************************************
#** Please provide your file(s) etc. in the following
#****************************************************************************

#****************************************************************************
#** TARGET defines the main latex file and therefore the output file name.
#** NOTE, no file extension must be given.
#**
#** You can:
#**   (1) Give the file (without .tex suffix) to TARGET
#** or
#**   (2) Omit TARGET and let the target be autodetected in the current
#**       directory (recommended)

# give target here (leave it unset for autodetection)
TARGET = main

# give target language (for spell checking) here:
TARGETLANGUAGE = DE

# paper size to be used for the final document
PAPERSIZE = a4

#****************************************************************************
#** Listing of all document relevant files (WITHOUT the TARGET)

#** Add files to INPUT and/or give DIRS to let the files autodetect in the
#** given directories (in the latter case, do not forget to mention . to
#** include the current directory).
#** If omitted both, the autodetect uses the current directory.


# give additional tex files here:
TEXINPUT =
#TEXINPUT +=Abstract.tex
#TEXINPUT +=Bibliography.tex
#TEXINPUT +=Introduction.tex
# OR let it autodetect in TEXDIRS
TEXDIRS =

# give bib files here:
BIBINPUT =
# or let it autodetect in BIBDIRS
BIBDIRS =

# xfig input
XFIGINPUT =
# or let it autodetect in XFIGDIRS
XFIGDIRS =

# jpg input
JPGINPUT =
# or let it autodetect in JPGDIRS
JPGDIRS =

# gif input
GIFINPUT =
# or let it autodetect in GIFDIRS
GIFDIRS =

# any additional files:
COMINPUT =


#****************************************************************************
#****************************************************************************
#****************************************************************************
#** No edit is needed after this line
#****************************************************************************
#****************************************************************************
#****************************************************************************


#****************************************************************************
#** Collect dirs, files etc.

# default target, if no target set
TARGETDEFAULT = $(basename $(shell egrep -l 'document(class|style)' *.tex) )
REALTARGET = $(if $(TARGET),$(TARGET),$(TARGETDEFAULT))

# collect tex files
TEXINPUTBYDIRS = $(wildcard $(addsuffix /*.tex,$(TEXDIRS)))
TEXDEFAULTINPUT = $(if $(strip $(TEXINPUT) $(TEXDIRS)),,$(wildcard *.tex))
TEXREALINPUTPLUS = $(TEXINPUT) $(TEXINPUTBYDIRS) $(TEXDEFAULTINPUT)
TEXREALINPUT = $(filter-out $(addsuffix .tex,$(REALTARGET)),$(TEXREALINPUTPLUS))

# collect BIB files
BIBINPUTBYDIRS = $(wildcard $(addsuffix /*.bib,$(BIBDIRS)))
BIBDEFAULTINPUT = $(if $(strip $(BIBINPUT) $(BIBDIRS)),,$(wildcard *.bib))
BIBREALINPUT = $(BIBINPUT) $(BIBINPUTBYDIRS) $(BIBDEFAULTINPUT)

# collect XFIG files
XFIGINPUTBYDIRS = $(wildcard $(addsuffix /*.fig,$(XFIGDIRS)))
XFIGDEFAULTINPUT = $(if $(strip $(XFIGINPUT) $(XFIGDIRS)),,$(wildcard *.fig))
XFIGREALINPUT = $(XFIGINPUT) $(XFIGINPUTBYDIRS) $(XFIGDEFAULTINPUT)

# collect JPG files
JPGINPUTBYDIRS = $(wildcard $(addsuffix /*.jpg,$(JPGDIRS)))
JPGDEFAULTINPUT = $(if $(strip $(JPGINPUT) $(JPGDIRS)),,$(wildcard *.jpg))
JPGREALINPUT = $(JPGINPUT) $(JPGINPUTBYDIRS) $(JPGDEFAULTINPUT)

# collect GIF files
GIFINPUTBYDIRS = $(wildcard $(addsuffix /*.gif,$(GIFDIRS)))
GIFDEFAULTINPUT = $(if $(strip $(GIFINPUT) $(GIFDIRS)),,$(wildcard *.gif))
GIFREALINPUT = $(GIFINPUT) $(GIFINPUTBYDIRS) $(GIFDEFAULTINPUT)

# COM input
COMREALINPUT = $(COMINPUT)

# full input
REALINPUT =
REALINPUT += $(TEXREALINPUT)
REALINPUT += $(BIBREALINPUT)
REALINPUT += $(XFIGREALINPUT)
REALINPUT += $(JPGREALINPUT)
REALINPUT += $(GIFREALINPUT)
REALINPUT += $(COMREALINPUT)

# full intern input
COREINPUT = $(patsubst %.fig,%.pstex,\
						$(patsubst %.jpg,%.eps,\
						$(patsubst %.gif,%.eps,$(REALINPUT))))

#****************************************************************************
#** helper applications
#** NOTE: the options depend on your installed version of the tool
pdf_viewer 					= xpdf
pdf_viewer_options 	= -z page -paper $(PAPERSIZE)
ps_viewer  					= gv
ps_viewer_options 	= --media=$(PAPERSIZE) --resize --scale=0 --scalebase=2
dvi_viewer 					= xdvi
dvi_viewer_options 	= -paper $(PAPERSIZE)

spell_checker 			= aspell

#****************************************************************************
#** Handy functions

empty:=
space:= $(empty) $(empty)
intopath = $(subst $(space),:,$(strip $(1)))

#****************************************************************************
#** Main public targets & default goal

.PHONY: all clean proper check final usage help tex dvi ps pdf all-output
.PHONY: check_existence

.PHONY: vpdf vps vdvi view-pdf view-ps view-dvi html tag touch

# default goal, as it's the first
all: usage


#****************************************************************************
#** Impementing usage

help : usage

ps : check_existence $(REALTARGET).ps $(REALTARGET).2.ps

all-output: ps pdf

final : proper check all-output clean

vdvi : dvi view-dvi

vpdf : pdf view-pdf

vps : ps view-ps

tex : dvi

dvi : $(REALTARGET).dvi

pdf : $(REALTARGET).pdf

touch :
	touch $(REALTARGET).tex

# make sure dummy files do exist to make the example compile properly
check_existence:
	touch `egrep '(include|input)' $(REALTARGET).tex | \
		sed -e 's/\\include{//' -e 's/\\input{//' -e 's/}/.tex/'`

usage:
	@echo " "
	@echo "USAGE: "
	@echo " "
	@echo "all|usage|help - this usage information"
	@echo "(v)dvi|tex     - latex the file (and view it)"
	@echo "(v)ps          - latex until ps and 2.ps reached (and view it)"
	@echo "(v)pdf         - latex until pdf reached (and view it)"
	@echo "final          - everything: clean, create all again, check spelling"
	@echo "html           - latex2html the file"
	@echo "tag            - make all files and print html fragment for copy&paste"
	@echo "clean          - clean files"
	@echo "proper         - clean even output files"
	@echo "check          - spell check"
	@echo "touch          - treat main file as changed, i.e force re-make"
	@echo " "
	@echo " "
	@echo "CURRENT:"
	@echo " "
	@echo "Target is:" $(REALTARGET) "(so, main file is $(REALTARGET).tex)"
	@echo "Additional input files are:" $(REALINPUT)
	@echo "Target's language tag is:" $(TARGETLANGUAGE)
ifdef DEBUG
	@echo $(TARGETDEFAULT)
	@echo $(TEXINPUT) ":" $(TEXDIRS) ":" $(TEXINPUTBYDIRS) ":" $(TEXDEFAULTINPUT)
 	@echo ":" $(TEXREALINPUTPLUS) ":" $(TEXREALINPUT)
	@echo $(BIBINPUT) ":" $(BIBDIRS) ":" $(BIBINPUTBYDIRS) ":" $(BIBDEFAULTINPUT)
 	@echo ":" $(BIBREALINPUT)
	@echo "core:" $(COREINPUT)
endif


#****************************************************************************
#** Tex related rules


%.pstex: %.fig
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	fig2dev -L pstex $< $@
	fig2dev -L pstex_t -p $@ $< $(basename $@).pstex_t
	rm -f $(basename $@).pstex_t

%.eps: %.jpg
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	jpegtopnm $< | pnmtops -noturn > $@
	@echo "created: $@ (from $<)"

%.eps: %.gif
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	giftopnm $< | pnmtops -noturn > $@
	@echo "created: $@ (from $<)"

%.eps:  %.ps
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	ps2eps -f $<
	@echo \* created: $@ (from $<)

%.pdf: %.eps
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	epstopdf $<
	@echo "created: $@ (from $<)"

%.ps: %.dvi
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	dvips -t $(PAPERSIZE) -Pcmz -Pamz -o $@ $<
	@echo "created: $@ (from $<)"

%.2.ps: %.ps
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	psnup -p$(PAPERSIZE) -P$(PAPERSIZE) -n 2 $< $@
	@echo "created: $@ (from $<)"

$(REALTARGET).dvi : $(REALTARGET).tex $(COREINPUT)
	@echo
	@echo \*
	@echo \* Creating $@ from $<
	@echo \*
	@(test -f $(basename $@).make.log && rm $(basename $@).make.log) || true
	@(test -f $(basename $@).bibtex.log && rm $(basename $@).bibtex.log) || true
	@(test -f $(basename $@).warning.log && rm $(basename $@).warning.log) || true
	latex $< | tee $(basename $@).make.log
ifneq ($(strip $(BIBREALINPUT)),)
	env BIBINPUTS=$$BIBINPUTS:$(call intopath,$(BIBDIRS)) bibtex $(basename $@) \
		| tee $(basename $@).bibtex.log
	@(egrep 'Warning|error' $(basename $@).bibtex.log && \
		cat $(basename $@).bibtex.log >> $(basename $@).warning.log) || true
endif
	@(test -f $(basename $@).idx && (test -f $(basename $@).ist && \
				makeindex -s $(basename $@).ist $(basename $@).idx \
				|| makeindex $(basename $@).idx ) ) || true
	latex $< | tee -a $(basename $@).make.log
	@(test -f $(basename $@).idx && (test -f $(basename $@).ist && \
				makeindex -s $(basename $@).ist $(basename $@).idx\
				|| makeindex $(basename $@).idx ) ) || true
	latex $< | tee -a $(basename $@).make.log
	@(grep 'LaTeX Warning' $(basename $@).log && \
			grep 'LaTeX Warning' $(basename $@).log >> $(basename $@).warning.log) \
			|| true
# 	@(test -f $(basename $@).warning.log && \
# 			dialog --infobox "`cat $(basename $@).warning.log`" 0 0) || true
	@echo "created: $@ (from $<)"

%.pdf : %.dvi
	@echo " "
	@echo Creating $@ from $<
	@echo " "
	dvips -Ppdf -G0 -t $(PAPERSIZE) -o $(basename $<).pdfps $<
	ps2pdf -sPAPERSIZE=$(PAPERSIZE) 	 $(basename $<).pdfps $@
	rm -f $(basename $<).pdfps
	@echo "created: $@ (from $<)"

html :
	latex2html $(REALTARGET)

gziptargetps :
	gzip -c $(REALTARGET).ps > $(REALTARGET).ps.gz

FILESIZE_HUMANREADABLE = $(shell ls -h --si -l $(1) | awk '{ print $$5 }')

tag : all-output gziptargetps
	@echo " "
	@echo "$(REALTARGET)  \
	(<A HREF=\"$(REALTARGET).ps\">PS, \
	 $(call FILESIZE_HUMANREADABLE,$(REALTARGET).ps)</a>) \
	(<A HREF=\"$(REALTARGET).ps.gz\">PS.gz, \
	 $(call FILESIZE_HUMANREADABLE,$(REALTARGET).ps.gz)</a>) \
	(<A HREF=\"$(REALTARGET).pdf\">PDF, \
	 $(call FILESIZE_HUMANREADABLE,$(REALTARGET).pdf)</a>)" |  \
	tee "$(REALTARGET).html"
	@echo " "

clean :
	rm -f $(patsubst %.tex,%.aux,$(COREINPUT))
	rm -f $(patsubst %.tex,%.aux,$(wildcard *.tex))
	rm -f *~
	rm -f $(REALTARGET).log
	rm -f $(REALTARGET).warning.log
	rm -f $(REALTARGET).make.log
	rm -f $(REALTARGET).lof
	rm -f $(REALTARGET).lot
	rm -f $(REALTARGET).toc
	rm -f $(REALTARGET).bbl
	rm -f $(REALTARGET).blg
	rm -f $(REALTARGET).ps.2
	rm -f $(REALTARGET).0??
	rm -f *.bak
	rm -f *.pdfps
	rm -f *.pstex_t

proper : clean
	rm -f $(REALTARGET).dvi
	rm -f $(REALTARGET).pdf
	rm -f $(REALTARGET).ps.gz
	rm -f $(REALTARGET).2.ps.gz
	rm -f $(REALTARGET).ps
	rm -f $(REALTARGET).2.ps
	rm -f $(REALTARGET).html

check :
	for file_to_check in $(REALTARGET).tex $(TEXREALINPUT) $(BIBREALINPUT); do \
		$(spell_checker) -t --language-tag=$(TARGETLANGUAGE) \
		--lang=$(TARGETLANGUAGE) check $${file_to_check}; \
	done


view-pdf :
	$(pdf_viewer) $(pdf_viewer_options) $(REALTARGET).pdf

view-ps :
	$(ps_viewer)  $(ps_viewer_options)  $(REALTARGET).ps

view-dvi :
	$(dvi_viewer) $(dvi_viewer_options) $(REALTARGET).dvi

