# Directory for external version
EXDIR := /home/httpd/html/informatik
# Directory for internal version
INDIR := /home/httpd/html/informatik/intern

# How to produce the one-page-per-sheet pdf version:
# by latex; dvips; ps2pdf
TEX2PDF := dvips
#PS2PDFOPTIONS := -sPAPERSIZE=a4 -dAutoRotatePages=/None -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true
PS2PDFOPTIONS := -sPAPERSIZE=a4 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true
# by pdflatex
#TEX2PDF := pdflatex

# Fonts to be used by "dvips"
# True type fonts (?)
PSFONTS := TT
# Bitmap fonts with a resolution of 600dpi
#PSFONTS := 600

# Mark the origin of PS/PDF files? "yes" is only for test purposes
MPSPDF := no
#MPSPDF := yes

##########################################################################
# IT SHOULD NOT BE NECESSARY TO EDIT ANYTHING BELOW
##########################################################################

VER :=	version.tex

TEX :=	studplan.tex $(VER) studplan.sty lvadat.tex Vorwort.tex \
	BakkIntro.tex BakkDS.tex BakkMI.tex BakkSI.tex BakkTI.tex BakkZI.tex \
	MagCG.tex MagCI.tex MagIK.tex MagMI.tex MagSE.tex \
	MagTI.tex MagWI.tex MagZI.tex Contents.tex Abbrev.tex Zusatz.tex

DAT :=	dat/kategorie.dat dat/lva.dat dat/pruefach.dat dat/studium.dat \
	dat/titel.dat dat/typ.dat

INH :=	$(wildcard inhalt/*)

V :=	$(shell sed '/\\def\\version/!d;s/.*{\(.*\)\}.*/\1/' $(VER))
D :=	$(shell sed '/\\def\\datum/!d;s/.*{\(.*\)\}.*/\1/' $(VER))
A :=	$(shell sed '/^\\Anhaengetrue/!d;s/.*/a/' $(VER))
I :=	$(shell sed '/^\\InterneVersiontrue/!d;s/.*/i/' $(VER))
Z :=	$(shell sed '/^\\Zweiseitigtrue/!d;s/.*/z/' $(VER))
E :=    $(shell sed '/^\\Entwurftrue/!d;s/.*/e/' $(VER))

ifeq "$(I)" "i"
PUBLIC := $(INDIR)
else
PUBLIC := $(EXDIR)
endif

SP   :=	studplan

SPV  :=	$(SP)-$(V)$(A)$(I)$(Z)
SPV1 :=	$(SPV)-1
SPV2 :=	$(SPV)-2
SPV4 :=	$(SPV)-4
DVI1 :=	$(SPV1).dvi
ifeq "$(MPSPDF)" "no"				# ifeq  1
PS1  :=	$(SPV1).ps
PDF1 :=	$(SPV1).pdf
PS2  :=	$(SPV2).ps
PDF2 :=	$(SPV2).pdf
PS4  :=	$(SPV4).ps
PDF4 :=	$(SPV4).pdf
else						# else  1
PS1  :=	$(SPV1).$(PSFONTS).ps
ifeq "$(TEX2PDF)" "dvips"			# ifeq  2
PDF1 :=	$(SPV1).dvips$(PSFONTS).pdf
else						# else  2
PDF1 :=	$(SPV1).pdflatex.pdf
endif						# endif 2
PS2  :=	$(SPV2).$(PSFONTS).ps
PDF2 :=	$(SPV2).dvips$(PSFONTS).pdf
PS4  :=	$(SPV4).$(PSFONTS).ps
PDF4 :=	$(SPV4).dvips$(PSFONTS).pdf
endif						# endif 1

sp   := spinf
spv  :=	$(sp)$(A)$(I)$(Z)
spv1 :=	$(spv)1
spv2 :=	$(spv)2
spv4 :=	$(spv)4
dvi1 :=	$(spv1).dvi
ps1  :=	$(spv1).ps
pdf1 :=	$(spv1).pdf
ps2  :=	$(spv2).ps
pdf2 :=	$(spv2).pdf
ps4  :=	$(spv4).ps
pdf4 :=	$(spv4).pdf


help:
	@echo "Usage: make <target>"
	@echo "       where <target> is one of:"
	@echo "       dvi ......... make DVI version"
	@echo "       ps .......... make PS  version"
	@echo "       pdf ......... make PDF version"
	@echo "       2 ........... make PS&PDF with 2 pages per sheet"
	@echo "       4 ........... make PS&PDF with 4 pages per sheet"
	@echo "       all ......... make all of the above"
	@echo "       bedarf ...... compute staff requirements"
	@echo "       clean ....... delete auxiliary files"
	@echo "       realclean ... delete all generated files"
	@echo "       showconf .... show configuration"
	@echo "       intern ...... configure for internal version"
	@echo "       extern ...... configure for external version"
	@echo "       inapps ...... configure for including appendices"
	@echo "       exapps ...... configure for excluding appendices"
	@echo "       onesided .... configure for onesided page layout"
	@echo "       twosided .... configure for twosided page layout"
	@echo "       public ...... copy the document to the public places"
	@echo "       csv ......... generate CSV version"
	@echo "       draft ....... draft version"
	@echo "       final ....... final version"


clean:
	-rm *~ *.BAK *.aux *.log *.toc inhalt/*~ inhalt/*.BAK dat/*~ dat/*.BAK



realclean:	clean
	-rm *.dvi *.ps *.pdf *.csv lvadat.tex bedarf.txt



bedarf:
	@perl bedarf.pl


showconf:
	@echo
	@echo "Studienplan Version $(V) ($(D))"
ifeq "$(A)" "a"					# ifeq  3
	@echo "   mit Anhngen"
else						# else  3
	@echo "   ohne Anhnge"
endif						# endif 3
	@echo -n "   in der "
ifeq "$(I)" "i"					# ifeq  4
	@echo -n "internen"
else						# else  4
	@echo -n "externen"
endif						# endif 4
	@echo " Fassung"
	@echo -n "   fr "
ifeq "$(Z)" "z"					# ifeq  5
	@echo -n "zwei"
else						# else  5
	@echo -n "ein"
endif						# endif 5
	@echo "seitigen Druck"
ifeq "$(E)" "e"					# ifeq  6
	@echo "   im Entwurf"
else						# else  6
	@echo "   in der Endversion"
endif						# endif 6
	@echo
	@echo "Documents:"
	@echo "   $(DVI1)"
	@echo "   $(PS1)"
	@echo "   $(PDF1)"
	@echo "   $(PS2)"
	@echo "   $(PDF2)"
	@echo "   $(PS4)"
	@echo "   $(PDF4)"
	@echo
	@echo "Options:"
	@echo -n "   All PS files use "
ifeq "$(PSFONTS)" "TT"
	@echo -n "TT"
else
	@echo -n "$(PSFONTS) dpi bitmap"
endif
	@echo " fonts."
	@echo -n "   All PDF files are generated from PS files using ps2pdf"
ifeq "$(TEX2PDF)" "dvips"
	@echo "."
else
	@echo
	@echo "      except $(PDF1) which is"
	@echo "      generated from the TEX source using pdflatex."
endif
	@echo



dvi:	csvhackoff $(DVI1)
ps:	csvhackoff $(PS1)
pdf:	csvhackoff $(PDF1)
2:	csvhackoff $(PS2) $(PDF2)
4:	csvhackoff $(PS4) $(PDF4)
all:	dvi ps pdf 2 4

public:	all
	cp $(DVI1) $(PS1) $(PDF1) $(PS2) $(PDF2) $(PS4) $(PDF4) $(PUBLIC)
	cd $(PUBLIC); \
	rm $(dvi1) $(ps1) $(pdf1) $(ps2) $(pdf2) $(ps4) $(pdf4); \
	ln -s $(DVI1) $(dvi1); \
	ln -s $(PS1) $(ps1); \
	ln -s $(PDF1) $(pdf1); \
	ln -s $(PS2) $(ps2); \
	ln -s $(PDF2) $(pdf2); \
	ln -s $(PS4) $(ps4); \
	ln -s $(PDF4) $(pdf4)

intern:
	sed 's/.*\\InterneVersiontrue.*/\\InterneVersiontrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

extern:
	sed 's/.*\\InterneVersiontrue.*/%\\InterneVersiontrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

inapps:
	sed 's/.*\\Anhaengetrue.*/\\Anhaengetrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

exapps:
	sed 's/.*\\Anhaengetrue.*/%\\Anhaengetrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

onesided:
	sed 's/.*\\Zweiseitigtrue.*/%\\Zweiseitigtrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

twosided:
	sed 's/.*\\Zweiseitigtrue.*/\\Zweiseitigtrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

draft:
	sed 's/.*\\Entwurftrue.*/\\Entwurftrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

final:
	sed 's/.*\\Entwurftrue.*/%\\Entwurftrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

csvhackon:
	sed 's/.*\\CSVtrue.*/\\CSVtrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

csvhackoff:
	sed 's/.*\\CSVtrue.*/%\\CSVtrue/' $(VER) > $(VER).tmp
	mv $(VER).tmp $(VER)

lvadat.tex: lvatex.pl $(DAT)
	perl lvatex.pl


$(DVI1):	$(TEX) $(INH)
	latex $(SP)
	latex $(SP)
	latex $(SP)
	mv $(SP).dvi $(DVI1)

# Same as $(DVI1), but dvi file is not moved; only for work
x:	$(TEX) $(INH)
	latex $(SP)
	latex $(SP)
	latex $(SP)

$(PS1):	$(DVI1)
ifeq "$(PSFONTS)" "TT"
#	dvips -Ppdf $(DVI1) -o $(PS1)
	dvips -Pwww $(DVI1) -o $(PS1)
else
	dvips -D$(PSFONTS) $(DVI1) -o $(PS1)
endif


$(PS2):	$(PS1)
	psnup -pa4 -2 $(PS1) $(PS2)


$(PS4):	$(PS1)
	psnup -pa4 -4 $(PS1) $(PS4)

ifeq "$(TEX2PDF)" "dvips"
$(PDF1):	$(PS1)
	ps2pdf $(PS2PDFOPTIONS) $(PS1) $(PDF1)
else
$(PDF1):	$(TEX) $(INH)
	pdflatex $(SP)
	pdflatex $(SP)
	pdflatex $(SP)
	mv $(SP).pdf $(PDF1)
endif


$(PDF2):	$(PS2)
	ps2pdf $(PS2PDFOPTIONS) $(PS2) $(PDF2)


$(PDF4):	$(PS4)
	ps2pdf $(PS2PDFOPTIONS) $(PS4) $(PDF4)

csv:	$(TEX) $(INH)
	make csvhackon onesided inapps extern
	latex $(SP)
	sh etc/csv.sh
	make csvhackoff clean
