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@
TEXI2ANY = @TEXI2ANY@
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

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
	@if test "$(TEXI2ANY)" != ""; then \
	  $(TEXI2ANY) --dvi asymptote_.texi; \
	fi
	mv asymptote_.dvi asymptote.dvi

asymptote.pdf: $(SOURCE) $(ASYFILES:.asy=.pdf) latexusage.pdf
	@if test "$(TEXI2ANY)" != ""; then \
	  $(TEXI2ANY) --pdf asymptote.texi; \
	fi

asymptote.info: $(SOURCE) $(ASYFILES:.asy=.pdf) latexusage.pdf
	@if test "$(TEXI2ANY)" != ""; then \
	  $(TEXI2ANY) --no-warn --no-split --info asymptote.texi; \
	fi

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 asymptote.info
	cd png && $(MAKE) install
	cd FAQ && $(MAKE) install-prebuilt

# Build and install the info page (doc/asymptote.info). Kept separate from
# `install` so a plain install does not require texinfo support; call this
# explicitly (e.g. from the RPM spec) to ship the info pages.
install-info: asymptote.info
	cd png && $(MAKE) install

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
