MANFILES = asy.1 xasy.1x
ASYFILES = $(filter-out $(wildcard latexusage-*.asy),$(wildcard *.asy))
SOURCE = asymptote.texi version.texi options
ASY = ../asy -dir ../base -config "" -render=0
DOCFILES = asymptote.pdf asy-latex.pdf CAD.pdf TeXShopAndAsymptote.pdf \
           asyRefCard.pdf

docdir = $(DESTDIR)@docdir@
infodir = $(DESTDIR)@infodir@
datarootdir = @datarootdir@
INSTALL = @INSTALL@
TEXI2DVI = @TEXI2DVI@
PERL5LIB = ./

export docdir infodir INSTALL PERL5LIB

all:    doc

asy-latex.pdf:
	pdflatex asy-latex.dtx

asymptote.sty:
	pdflatex asy-latex.dtx

dvi:	doc asymptote.dvi

doc:	$(DOCFILES) asy.1 faq
	cd png && $(MAKE) all

manpage: $(MANFILES)

man:	$(DOCFILES) manpage
	# cd png && $(MAKE) asymptote.info

faq:
	cd FAQ && $(MAKE) faq

%.eps:	%.asy
	$(ASY) -f eps $<

%.pdf:	%.asy
	$(ASY) -f pdf -noprc $<

latexusage.pdf: latexusage.tex asymptote.sty
	rm -f latexusage-*
	rm -f latexusage.pre
	rm -f latexusage.aux
	pdflatex latexusage
	$(ASY) -noprc latexusage-*.asy
	pdflatex latexusage

options: ../settings.cc
	$(ASY) -h 2>&1 | tail -n +3 > options

asy.1: options asy.1.begin asy.1.end
	cat options | grep \^- | \
	sed -e "s/-\(.*\)  \([a-zA-Z0-9].*\)/.TP\n.B -\1\n\2\./" | \
	sed -e "/^.B/ s/-/\\\\-/g" | cat asy.1.begin - asy.1.end > asy.1

asymptote.dvi: $(SOURCE) $(ASYFILES:.asy=.eps) latexusage.pdf
	ln -sf asymptote.texi asymptote_.texi
	-$(TEXI2DVI) asymptote_.texi
	mv asymptote_.dvi asymptote.dvi

asymptote.pdf: $(SOURCE) $(ASYFILES:.asy=.pdf) latexusage.pdf
	-$(TEXI2DVI) --pdf asymptote.texi

CAD.pdf: CAD.tex CAD1.eps
	pdflatex CAD
	pdflatex CAD
	pdflatex CAD

TeXShopAndAsymptote.pdf: TeXShopAndAsymptote.tex
	pdflatex TeXShopAndAsymptote
	pdflatex TeXShopAndAsymptote

asyRefCard.pdf: asyRefCard.tex
	pdftex asyRefCard

clean: FORCE
	-rm -f asy-latex.{aux,idx,ins,log,toc,out,hd,ins}
	-rm -f *.eps latexusage.{dvi,eps,pdf,log,aux,out,*.eps} latexusage-* \
            latexusage.pre
	-rm -f \
            {asymptote,asymptote_}.{aux,cp,cps,dvi,fn,info,ky,log,pg,toc,tp,vr}
	-rm -f asymptote_.texi
	-rm -f {CAD,TeXShopAndAsymptote,asyRefCard}.{aux,dvi,log,toc}
	-cd png && $(MAKE) clean

cleaner0: FORCE clean
	-rm -f version.texi Makefile

cleaner: FORCE cleaner0
	-cd png && $(MAKE) cleaner
	-cd FAQ && $(MAKE) cleaner

cleanest: FORCE
	-rm -f $(DOCFILES)
	-rm -f options asy.1
	-rm -f $(ASYFILES:.asy=.pdf)
	-$(MAKE) cleaner0
	-cd png && $(MAKE) cleanest
	-cd FAQ && $(MAKE) cleanest

distclean: FORCE cleaner
maintainer-clean: FORCE cleanest

install-man:
	${INSTALL} -d -m 755 $(docdir) $(mandir)/man1
	${INSTALL} -p -m 644 $(DOCFILES) $(docdir)
	${INSTALL} -p -m 644 $(MANFILES) $(mandir)/man1

install: man faq install-man
	cd png && $(MAKE) install
	cd FAQ && $(MAKE) install

install-prebuilt: install-man options
	touch png/asymptote.info
	cd png && $(MAKE) install
	cd FAQ && $(MAKE) install-prebuilt

install-all: $(DOCFILES) $(MANFILES) faq install-man
	cd png && $(MAKE) install-all
	cd FAQ && $(MAKE) install-info

uninstall: uninstall-all

uninstall-all:
	cd png && $(MAKE) uninstall
	cd FAQ && $(MAKE) uninstall
	-cd $(mandir)/man1 && rm -f $(MANFILES)
	-rm -f $(addprefix $(docdir)/,$(DOCFILES))

FORCE:

Makefile: Makefile.in
	cd ..; config.status
