# Makefile for building ps and pdf output from latex documents 
# using the latexmk program.
# 
# Copyright 2003 Elmar Zander, Inst. of Scientific Computing, 
# TU Braunschweig
# $Id: Makefile 291 2005-10-03 22:22:59Z neocool $

BASENAMES=$(patsubst %.tex,%,$(wildcard *.tex))
SOURCES=$(patsubst %,%.tex,$(BASENAMES))
EDIT_SOURCE=$(SOURCES)
PREVIEW=$(word 1, $(SOURCES))
SUBDIRS=
INSTALLDIR=
MAIN_EXTENSIONS=.ps .pdf
#EXTRA_EXTENSIONS=.ps.gz -2p.ps -2p.ps.gz

MAIN_TARGETS=$(foreach ext,$(MAIN_EXTENSIONS),$(BASENAMES:%=%$(ext)))
EXTRA_TARGETS=$(foreach ext,$(EXTRA_EXTENSIONS),$(BASENAMES:%=%$(ext)))

TEXINPUTS:=.//:$(TEXINPUTS)
BSTINPUTS:=.//:$(BSTINPUTS)
BIBINPUTS:=.//:$(BIBINPUTS)
TEX_EXPORTS=TEXINPUTS=$(TEXINPUTS) BSTINPUTS=$(BSTINPUTS) BIBINPUTS=$(BIBINPUTS)
LATEXMK=./latexmk
EDIT=emacs

include Makefile.local

.PHONY: default # invoke the targets: all
default: all

.PHONY: all # create all output files (including subdirs and additional files)
all: subdirs ps pdf extra

.PHONY: extra # create gzipped and 2-page files
extra:  $(EXTRA_TARGETS)

# we need the main targets somewhere as a prerequisite
# in order that the extra targets work
.INTERMEDIATE: $(MAIN_TARGETS)

%-2p.ps: %.ps
	psnup -2 $< $@

%.ps.gz: %.ps
	gzip -c $< > $@

.PHONY: install # copy the main targets to the installation directory
install:
	@if [ -n \'$(INSTALLDIR)\' ]; then \
		if [ ! -d  $(INSTALLDIR) ]; then \
			mkdir -p $(INSTALLDIR) || \
				echo Could not create install dir $(INSTALLDIR);\
		fi;\
		echo Copying targets to installation directory;\
		cp -v $(MAIN_TARGETS) $(EXTRA_TARGETS) $(INSTALLDIR);\
	else\
		echo No install dir specified;\
	fi;\


.PHONY: subdirs # create all targets in the subdirectories
subdirs: 
	@for i in $(SUBDIRS) XXX; do\
		if test $$i != XXX; then \
			pushd .; \
	    cd $$i && $(MAKE) all; \
			popd; \
		fi; \
	done; 

.PHONY: pdf # create the pdf files only
pdf:
	@export $(TEX_EXPORTS); $(LATEXMK) -pdf $(SOURCES)

.PHONY: ps # create the ps files only
ps:
	@export $(TEX_EXPORTS); $(LATEXMK) -ps $(SOURCES)

.PHONY: dvi # create the dvi files only
dvi:
	@export $(TEX_EXPORTS); $(LATEXMK) $(SOURCES)

.PHONY: session # start an edit session (edit & preview)
session: edit preview

.PHONY: preview # invoke a continuously running  previewer
preview:
	@export $(TEX_EXPORTS);\
	xterm -e $(LATEXMK) -ps -pvc -f $(EDIT_SOURCE) &

.PHONY: edit # invoke and editor for source files
edit: 
	@export $(TEX_EXPORTS);\
	$(EDIT) $(EDIT_SOURCE) -geometry 80x60+0+0&

.PHONY: mostlyclean # clean up only intermediate and backup files
mostlyclean:
	$(LATEXMK) -c $(SOURCES)
	rm -rf *~ *.bak *.aux auto/

.PHONY: clean # clean up final, internediate and backup files
clean: clean-subdirs
	$(LATEXMK) -C $(SOURCES)
	rm -rf *~ *.lol *.bak *.aux auto/ $(EXTRA_TARGETS)

.PHONY: clean-subdirs # clean up subdirectories
clean-subdirs:
	@for dir in $(SUBDIRS) ; do \
		(cd $$dir && $(MAKE) clean) || exit 1; \
	done

.PHONY: help # show this help message
help:
	@echo Usage:
	@echo "  make              --   invoke the default target"
	@cat Makefile | grep ^.PHONY | cut -d: -f2-\
		| cut -d\# -f1-2 --output-delimiter="`echo -e \\\\t--\\\\t`" \
	  | expand -t 14,18 | sed "s/^/  make/g"
