SHELL = /bin/sh

VPATH = @srcdir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = ..

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
libexecdir = @libexecdir@
datadir = @datadir@
sysconfdir = @sysconfdir@
sharedstatedir = @sharedstatedir@
localstatedir = @localstatedir@
libdir = @libdir@
infodir = @infodir@
mandir = @mandir@
includedir = @includedir@
oldincludedir = /usr/include
configdir = ${sysconfdir}/sane.d
docdir=$(prefix)/doc/sane-@VERSION@

MKDIR = $(top_srcdir)/mkinstalldirs
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
LN_S = @LN_S@

@SET_MAKE@

SECT1	= saned.1 scanimage.1 sane-find-scanner.1
SECT5	= sane-abaton.5 sane-agfafocus.5 sane-apple.5 sane-as6e.5 sane-dll.5 \
          sane-dc25.5 sane-dmc.5 sane-epson.5 sane-hp.5 sane-microtek.5 \
          sane-microtek2.5 sane-mustek.5 sane-nec.5 sane-net.5 sane-pie.5 \
          sane-pint.5 sane-pnm.5 sane-umax.5 sane-qcam.5 sane-scsi.5 \
          sane-artec.5 sane-fujitsu.5 sane-sharp.5 sane-s9036.5 \
          sane-tamarack.5 sane-ricoh.5 sane-avision.5 sane-plustek.5 \
          sane-st400.5 sane-mustek_pp.5 sane-dc210.5 sane-v4l.5 \
          sane-snapscan.5 sane-canon.5 sane-coolscan.5 sane-bh.5 sane-dc240.5 \
	  sane-umax_pp.5 sane-sm3600.5 sane-usb.5
SECT7   = sane.7
MANPAGES = $(SECT1) $(SECT5) $(SECT7)
READMES = README AUTHORS COPYING ChangeLog LEVEL2 LICENSE NEWS PROBLEMS \
          PROJECTS README README.aix README.hp-ux README.linux README.os2 \
          README.solaris README.unixware2 README.unixware7 TODO
DOCS    = backend-writing.txt sane.ps sane.dvi
BACKDIRS= canon mustek umax
LATEX	= TEXINPUTS=$(srcdir):$$TEXINPUTS latex
DLH	= TEXINPUTS=$(srcdir):$$TEXINPUTS dlh
MAN2HTML= nroff -man |\
	  man2html -compress  -topm 6 -botm 6 -title $${page} \
          -cgiurl '$$title.$$section.html'|\
	  sed 's,<BODY>,<BODY BGCOLOR=\#FFFFFF TEXT=\#000000><H1 ALIGN=CENTER><IMG SRC="../sane.png" HEIGHT=117 WIDTH=346></H1>,'
EMACS   = xemacs
WWW_PATH= /home/httpd/html/sane/

all: sane.ps $(MANPAGES) 

%.1 %.5 %.7: %.man
	@sed -e 's|@DATADIR@|$(datadir)|g' \
	     -e 's|@CONFIGDIR@|$(configdir)|g' \
	     -e 's|@DOCDIR@|$(docdir)|g' \
	     -e 's|@LIBDIR@|$(libdir)/sane|g' \
	     -e 's|@BINDIR@|$(bindir)|g' \
	     -e 's|@SBINDIR@|$(sbindir)|g' $^ >$@
	@echo Generating manpage $@...

install: all
	$(MKDIR) $(mandir)/man1 $(mandir)/man5 $(mandir)/man7
	@for page in $(SECT1); do \
	  echo installing $${page} in $(mandir)/man1/$${page}...; \
	  $(INSTALL_DATA) $${page} $(mandir)/man1/$${page} || exit 1; \
	done
	@for page in $(SECT5); do \
	  echo installing $${page} in $(mandir)/man5/$${page}...; \
	  $(INSTALL_DATA) $${page} $(mandir)/man5/$${page} || exit 1; \
	done
	@for page in $(SECT7); do \
	  echo installing $${page} in $(mandir)/man7/$${page}...; \
	  $(INSTALL_DATA) $${page} $(mandir)/man7/$${page} || exit 1; \
	done
	$(MKDIR) $(docdir)
	@for readme in $(READMES); do \
	  echo installing $${readme} in $(docdir)/$${readme}...; \
	  $(INSTALL_DATA) $(top_srcdir)/$${readme} $(docdir)/$${readme} \
	    || exit 1; \
	done
	@for backdir in $(BACKDIRS); do \
	  echo installing $${backdir} in $(docdir)/$${backdir}...; \
	  $(MKDIR) $(docdir)/$${backdir} ; \
	  for doc in $${backdir}/* ; do \
	    if test -f $${doc} ; then \
	      $(INSTALL_DATA) $${doc} $(docdir)/$${backdir} || exit 1; \
	    fi \
	  done \
	done
	@-for doc in $(DOCS); do \
	    echo installing $${doc} in $(docdir)/$${doc}...; \
	    $(INSTALL_DATA) $${doc} $(docdir)/$${doc}; \
	  done

uninstall:
	@for page in $(SECT1); do \
	  echo removing $${page} in $(mandir)/man1/$${page}...; \
	  rm -f $(mandir)/man1/$${page}; \
	done
	@for page in $(SECT5); do \
	  echo removing $${page} in $(mandir)/man5/$${page}...; \
	  rm -f $(mandir)/man5/$${page}; \
	done
	@for page in $(SECT7); do \
	  echo removing $${page} in $(mandir)/man7/$${page}...; \
	  rm -f $(mandir)/man7/$${page}; \
	done
	rm -rf $(docdir)

sane.ind: sane.tex net.tex
	@echo Generating index for sane.ps...
	@touch sane.ind
	@-$(LATEX) $(srcdir)/sane </dev/null >/dev/null && \
	  makeindex sane.idx 2>/dev/null

sane.ps: sane.ind
	@echo Generating sane.ps...
	@-$(LATEX) $(srcdir)/sane </dev/null >/dev/null && \
	  $(LATEX) $(srcdir)/sane </dev/null >/dev/null && \
	  dvips sane.dvi -o sane.ps 2>/dev/null

sane-html: sane.ind
	$(DLH) $(srcdir)/sane.tex

sane-backends.html:
	cd $(top_srcdir)/backend \
	 && $(EMACS) -batch --load $(top_srcdir)/tools/sane-desc.el \
	      -f sane-desc-doit

html-man: $(MANPAGES)
	@for page in $(MANPAGES); do \
	  echo "translating $${page} to $${page}.html..."; \
	  cat $${page} | $(MAN2HTML) > $${page}.html; \
	done

html: sane-backends.html html-man sane-html

%.gz: %
	gzip -f -c $^ >$@

install-mostang: html sane.ps sane.ps.gz
	@echo Installing html manpages in $(WWW_PATH)/man...
	$(MKDIR) $(WWW_PATH)/man
	for manpage in $(MANPAGES) ; do \
	  $(INSTALL_DATA) $${manpage}.html $(WWW_PATH)/man/$${manpage}.html; \
	done
	@echo Installing sane-backends.html in $(WWW_PATH)...
	$(MKDIR) $(WWW_PATH)
	$(INSTALL_DATA) sane-backends.html $(WWW_PATH)/sane-backends.html
	@echo Installing SANE standard \(html\) in $(WWW_PATH)/html/...
	$(MKDIR) $(WWW_PATH)/html/
	for html in sane/* ; do \
	  $(INSTALL_DATA) $${html} $(WWW_PATH)/html/ || exit 1; \
	done
	@echo Installing SANE standard \(postscript\) in $(WWW_PATH)/...
	$(INSTALL_DATA) sane.ps $(WWW_PATH)/sane.ps
	@echo Installing SANE standard \(ps.gz\) in $(WWW_PATH)/...
	$(INSTALL_DATA) sane.ps.gz $(WWW_PATH)/sane.ps.gz

clean:
	rm -f *.toc *.aux *.log *.cp *.fn *.tp *.vr *.pg *.ky *.blg *.idx *.cb
	rm -f *.ilg

distclean: clean
	rm -f $(MANPAGES)
	rm -f Makefile *~
	rm -f *.lot *.lof *.ind *.gz
	rm -f sane.dvi sane.ps sane-backends.html
	rm -f sane/*.html sane/*.gif
	-rm -rf sane
	for manpage in $(MANPAGES) ; do \
	  rm -f $${manpage}.html ; \
	done

depend:

.PHONY: all install depend clean html html-man sane-html install-mostang uninstall
