PKGDIR  ?= ../..
L4DIR   ?= $(PKGDIR)/../..
include $(L4DIR)/mk/Makeconf

prefix = $(DROPS_STDDIR)

TXTFILES	:= README.style README
BIBFILES	:= own.bib

GOSHDIR		:= $(dir $(shell PATH=$(L4DIR)/tool/gosh:$$PATH which gosh))
GOSH		:= $(GOSHDIR)/gosh
TXT2TEX_OPT	:= --style manual.gosh
TXT2MAN_OPT	:= --style $(GOSHDIR)/man.gosh

TEXFILES	:= style.tex build.tex
PDFFILES	:= $(TEXFILES:.tex=.pdf)
DVIFILES	:= $(TEXFILES:.tex=.dvi)

MANPAGES	:= $(patsubst %.tex, fiasco%.7, $(TEXFILES))

all:: $(PDFFILES) $(MANPAGES)

# Make sure the corresponding txt files are the first dependency of
# each document -- the rules to create the documents depend on this.
style.tex: README.style
fiascostyle.7: README.style
build.tex: README
fiascobuild.7: README

$(MANPAGES): $(GOSHDIR)/man.gosh $(GOSH) 

$(TEXFILES): manual.gosh $(GOSH)
$(PDFFILES) $(DVIFILES): $(BIBFILES) 

install:: $(MANPAGES)
	install -d $(prefix)/man/man7
	install -c -m 644 $(MANPAGES) $(prefix)/man/man7

clean::
	rm -f *.aux *.bbl *.blg *.log *.ps *.toc *.pdf *.dvi *.idx *.new \
	  $(TXTFILES) $(TEXFILES) $(MANPAGES) 

cleanall:: clean
	rm -f *~

$(TXTFILES): % : ../../src/%
	cp $< $@

$(MANPAGES):
	$(GOSH) $(TXT2MAN_OPT) $< > $@.new
	mv $@.new $@

.PRECIOUS: .tex
%.tex:
	$(GOSH) $(TXT2TEX_OPT) $< > $@.new
	mv $@.new $@

%.pdf: %.tex 
	pdflatex $<
	bibtex $(basename $<) || true
	pdflatex $<
	pdflatex $<

.PRECIOUS: .dvi
%.dvi: %.tex 
	latex $<
	bibtex $(basename $<) || true
	latex $<
	latex $<

%.ps: %.dvi
	dvips -o $@ $<
