#
#
#  Makefile for LaTeX  
#
# To build everything do
#    make tex
#    make web
#    make html
#    make dvipdf
#
# or simply
#
#    make
#
# for rapid development do:
#    make tex
#    make show
#
#
# If you are having problems getting "make" to work, debugging it is
#  easier if can see the output from latex, which is normally redirected
#  to /dev/null.  To see it, do the following:
#
#   cd docs/manual
#   make tex
#   latex bacula.tex
#
#  typically the latex command will stop indicating the error (e.g. a
#  missing \ in front of a _ or a missing { or ] ...
#
#  The following characters must be preceded by a backslash
#    to be entered as printable characters:
# 
#    # $ % & ~ _ ^ \ { }
#
DOC=utility
MAINDOC=Bacula_Utility_Programs.html
IMAGES=../../../images
DOCDIR=`pwd`
TEXFILES=$(wildcard *tex)
MAKEFORWEB=$(DOCDIR)/web.makefile
WEBFILESTOLINK=$(DOCDIR)/latex2html-init.pl $(DOCDIR)/bacula.sty $(DOCDIR)/translate_images.pl
#
# Main directory where to find all the documentation
DESTDIR=../pdf-and-html
#
# Location of documentation in PDF
PDF_DEST_DIR=$(DESTDIR)/$(DOC)
#
# Location of HTML documentation
HTML_DEST_DIR=$(DESTDIR)/$(DOC)
#
# Temporary directory to translate tex to HTML
WWW_BUILD_DIR=$(DESTDIR)/www-$(DOC)
#
# LATEX compiler
LATEX_TO_PDF=pdflatex --output-directory $(PDF_DEST_DIR)

first_rule: all

all: pdftex web mini-clean

.SUFFIXES:     .tex .html
.PHONY:
.DONTCARE:


destdir:
	@echo
	@echo "Making output directories..."
	@mkdir -p $(PDF_DEST_DIR) $(HTML_DEST_DIR)
	echo "Output directories done"

pdfimages: 
	@echo "Generating PDF images..."
	@(cd ${IMAGES}/svg ; make pdf)
	@echo "Done."

pngimages:
	@echo "Generating PNG images..."
	@(cd ${IMAGES}/svg ; make png)
	@echo "Done."

epsimages:
	@echo "Generating EPS images..."
	@(cd ${IMAGES}/svg ; make eps)
	@rm -rf ${IMAGES}/png
	@rm -rf ${IMAGES}/pdf
	@echo "Done."

epscovers:
	@echo -n "Linking coverpage and background EPS format..."
	@(cd $(SVGCOVERSDIR) ; make eps)
	@ln -sf `pwd`/${EPSCOVERSDIR}/${COVERNAME}.eps `pwd`/${BSYSMANUALDIR}/${BSYSMANNAME}.eps
	@rm -f `pwd`/${BSYSMANUALDIR}/${BSYSMANNAME}.pdf
	@echo "Done."

commonfiles:
	@echo -n "Making version `cat version.tex`"
	@echo -n "Linking shared files..."
	@(for L in $(LICENCES); do ln -sf $$L .; done)
	@echo "Done"

pdfcovers:
	@echo ""
	@echo "Making covers for $(DOC)"
	@(cd $(IMAGES)/svg ; make pdf)
	@echo "Covers done."
	@echo ""

external-references: destdir
	@echo " "
	@echo "Building external references for $(DOC)"
	@echo "\DeclareGraphicsExtensions{.pdf,.png,.jpg,.jpeg,.eps}" > graphicspolicy.tex
	@export TEXINPUTS=.:../../../latex//: ; $(LATEX_TO_PDF) $(DOC)
	@rm -f $(PDF_DEST_DIR)/$(DOC).pdf
	@echo "$(DOC) external references done."

pdftex: destdir
	@echo ""
	@echo "Making PDF manual with $(LATEX_TO_PDF)compile..."
	@echo "\DeclareGraphicsExtensions{.pdf,.png,.jpg,.jpeg,.eps}" > graphicspolicy.tex
	@export TEXINPUTS=.:../../../latex//: ; $(LATEX_TO_PDF) $(DOC); makeindex ${DOC}.idx -o ${DOC}.ind 2>/dev/null ; $(LATEX_TO_PDF) $(DOC); $(LATEX_TO_PDF) $(DOC)
	@echo "Done"


web: mini-clean
	@echo "Making web"
	@rm -rf $(WWW_BUILD_DIR)
	@mkdir -p $(WWW_BUILD_DIR)
	@echo "Adapting $(DOC) tex files for HTML documentation..."
	@(for F in $(TEXFILES) ;\
	do \
	cat $$F | sed -f ../pattern-to-handle > $(WWW_BUILD_DIR)/$$F ; \
	done)
	@ln -sf $(MAKEFORWEB) $(WWW_BUILD_DIR)/Makefile
	@(for F in $(WEBFILESTOLINK);\
	do \
	ln -sf $$F $(WWW_BUILD_DIR)/;\
	done)
	(cd $(WWW_BUILD_DIR) ; make )
	@echo "Done making web"

show:
	evince $(PDF_DEST_DIR)/${DOC}.pdf

texcheck:
	./check_tex.pl ${DOC}.tex

main_configs:
	pic2graph -density 100 <main_configs.pic >main_configs.png

mini-clean:
	@rm -f 1 2 3 *.tex~
	@rm -f *.gif *.jpg *.eps
	@rm -f *.aux *.cp *.fn *.ky *.log *.pg
	@rm -f *.backup *.ilg *.lof *.lot
	@rm -f *.cdx *.cnd *.ddx *.ddn *.fdx *.fnd *.ind *.sdx *.snd
	@rm -f *.dnd *.old *.out 
	@rm -f ${DOC}/*.gif ${DOC}/*.jpg ${DOC}/*.eps
	@rm -f ${DOC}/*.aux ${DOC}/*.cp ${DOC}/*.fn ${DOC}/*.ky ${DOC}/*.log ${DOC}/*.pg
	@rm -f ${DOC}/*.backup ${DOC}/*.ilg ${DOC}/*.lof ${DOC}/*.lot
	@rm -f ${DOC}/*.cdx ${DOC}/*.cnd ${DOC}/*.ddx ${DOC}/*.ddn ${DOC}/*.fdx ${DOC}/*.fnd ${DOC}/*.ind ${DOC}/*.sdx ${DOC}/*.snd
	@rm -f ${DOC}/*.dnd ${DOC}/*.old ${DOC}/*.out
	@rm -f ${DOC}/WARNINGS
	@rm -rf ../www-$(DOC)

clean:
	@rm -f graphicspolicy.tex
	@rm -f 1 2 3
	@rm -f *.png *.gif *.jpg *.eps
	@rm -f *.pdf *.aux *.cp *.fn *.ky *.log *.pg
	@rm -f *.html *.backup *.pdf *.ps *.dvi *.ilg *.lof *.lot
	@rm -f *.cdx *.cnd *.ddx *.ddn *.fdx *.fnd *.ind *.sdx *.snd
	@rm -f *.dnd imagename_translations
	@rm -f *.old WARNINGS *.out *.toc *.idx
	@rm -f images.pl labels.pl internals.pl
	@rm -f images.tex ${DOC}i.tex
	@rm -f ${DOC}i-*.tex
	@rm -rf $(WWW_BUILD_DIR)

distclean:  clean
	@rm -f ${DOC}.html ${DOC}.pdf
	@rm -rf $(WWW_BUILD_DIR)
	@rm -rf $(HTML_DEST_DIR)
