SHELL = /bin/sh

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

PACKAGE = @PACKAGE@
VERSION = @VERSION@
package_version = @PACKAGE_STRING@
distdir = $(top_srcdir)/$(PACKAGE)-$(VERSION)

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

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

DISTCLEAN_FILES = @DISTCLEAN_FILES@

@SET_MAKE@

SECT1	= scanimage.1 sane-config.1 sane-find-scanner.1 gamma4scanimage.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-gphoto2.5 \
          sane-leo.5 sane-lexmark.5 sane-matsushita.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-umax1220u.5 sane-sm3600.5 sane-usb.5 \
          sane-mustek_usb.5 sane-sceptre.5 sane-canon_pp.5 sane-canon630u.5 \
          sane-teco1.5 sane-teco2.5 sane-teco3.5 sane-test.5 sane-sp15c.5 \
          sane-coolscan2.5 sane-hpsj5s.5 sane-gt68xx.5 sane-artec_eplus48u.5 \
          sane-ma1509.5 sane-ibm.5 sane-hp5400.5 sane-plustek_pp.5 sane-u12.5 \
	  sane-niash.5 sane-sm3840.5 sane-genesys.5 sane-hp4200.5 \
	  sane-mustek_usb2.5 sane-hp3500.5 sane-pixma.5 sane-stv680.5 \
	  sane-hp5590.5 sane-hpljm1005.5 sane-cardscan.5 sane-hp3900.5 \
	  sane-epjitsu.5 sane-hs2p.5
SECT7   = sane.7
SECT8	= saned.8
MANPAGES = $(SECT1) $(SECT5) $(SECT7) $(SECT8)
READMES = README AUTHORS COPYING ChangeLog LICENSE NEWS PROBLEMS \
          PROJECTS README README.aix README.beos README.darwin README.freebsd \
	  README.djpeg \
          README.hp-ux README.linux README.netbsd README.openbsd README.os2 \
          README.solaris README.unixware2 README.unixware7 README.windows README.zeta
DOCS    = backend-writing.txt sane.ps sane.dvi
BACKDIRS= canon leo matsushita mustek mustek_usb plustek u12 umax sceptre teco \
	  gt68xx niash mustek_usb2
LATEX	= TEXINPUTS=$(srcdir):$$TEXINPUTS @LATEX@
DVIPS   = @DVIPS@
MAKEINDEX = @MAKEINDEX@
DLH	= TEXINPUTS=$(srcdir):$$TEXINPUTS dlh
MAN2HTML= nroff -man |\
	  man2html -compress -topm 6 -botm 6 -nodepage -title $${page} \
          -cgiurl '$$title.$$section.html'|\
	  sed 's,<BODY>,<BODY BGCOLOR=\#FFFFFF TEXT=\#000000><H1 ALIGN=CENTER><IMG SRC="/images/sane.png" HEIGHT=117 WIDTH=346></H1>,'

ifeq (@CROSS_COMPILING@,no)
HTML_PAGES = sane-backends.html sane-backends-external.html \
             sane-mfgs.html sane-mfgs-external.html
else
HTML_PAGES =
endif

ifeq (@USE_LATEX@,no)
STANDARD =
else
STANDARD = sane.ps
endif

SUBDIRS = $(BACKDIRS) icons figs descriptions descriptions-external

DISTFILES = Makefile.in backend-writing.txt descriptions.txt \
  doxygen-sanei.conf.in html.sty net.tex \
  releases.txt sane-abaton.man sane-agfafocus.man sane-apple.man \
  sane-artec.man sane-as6e.man sane-avision.man sane-bh.man \
  sane-canon.man sane-canon630u.man sane-config.man sane-coolscan.man \
  sane-coolscan2.man sane-dc210.man sane-dc240.man \
  sane-dc25.man sane-dll.man sane-dmc.man sane-epson.man \
  sane-find-scanner.man sane-fujitsu.man sane-gphoto2.man sane-hp.man \
  sane-logo.png sane-logo2.jpg sane-matsushita.man sane-microtek.man \
  sane-leo.man sane-lexmark.man sane-microtek2.man \
  sane-mustek.man sane-mustek_pp.man sane-mustek_usb.man sane-nec.man \
  sane-net.man sane-pie.man sane-pint.man sane-plustek.man \
  sane-pnm.man sane-qcam.man sane-ricoh.man sane-s9036.man \
  sane-scsi.man sane-sharp.man sane-sm3600.man sane-snapscan.man \
  sane-st400.man sane-tamarack.man sane-umax.man sane-umax1220u.man \
  sane-umax_pp.man sane-usb.man sane-v4l.man sane.man sane.png \
  sane.tex saned.man scanimage.man sane-sceptre.man sane-canon_pp.man \
  sane-teco1.man sane-teco2.man sane-teco3.man sane-test.man sane-sp15c.man \
  sane-hpsj5s.man gamma4scanimage.man sane-gt68xx.man sane-artec_eplus48u.man \
  sane-ma1509.man sane-ibm.man sane-hp5400.man sane-plustek_pp.man \
  sane-u12.man sane-niash.man sane-sm3840.man sane-genesys.man sane-hp4200.man \
  sane-mustek_usb2.man sane-hp3500.man sane-pixma.man sane-stv680.man \
  sane-hp5590.man sane-hpljm1005.man sane-cardscan.man sane-hp3900.man \
  sane-epjitsu.man sane-hs2p.man

.PHONY: all clean depend dist distclean html html-man install \
  sane-html uninstall

all: $(STANDARD) $(MANPAGES) html-pages

%.1 %.5 %.7 %.8: %.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' \
	     -e 's|@PACKAGEVERSION@|$(package_version)|g' $^ >$@
	@echo Generating manpage $@...

install: all
	$(MKDIR) $(DESTDIR)$(mandir)/man1 $(DESTDIR)$(mandir)/man5 \
           $(DESTDIR)$(mandir)/man7 $(DESTDIR)$(mandir)/man8
	@for page in $(SECT1); do \
	  echo installing $${page} in $(mandir)/man1/$${page}...; \
	  $(INSTALL_DATA) $${page} $(DESTDIR)$(mandir)/man1/$${page} || exit 1; \
	done
	@for page in $(SECT5); do \
	  echo installing $${page} in $(mandir)/man5/$${page}...; \
	  $(INSTALL_DATA) $${page} $(DESTDIR)$(mandir)/man5/$${page} || exit 1; \
	done
	@for page in $(SECT7); do \
	  echo installing $${page} in $(mandir)/man7/$${page}...; \
	  $(INSTALL_DATA) $${page} $(DESTDIR)$(mandir)/man7/$${page} || exit 1; \
	done
	@for page in $(SECT8); do \
	  echo installing $${page} in $(mandir)/man8/$${page}...; \
	  $(INSTALL_DATA) $${page} $(DESTDIR)$(mandir)/man8/$${page} || exit 1; \
	done
	$(MKDIR) $(DESTDIR)$(docdir)
	@for readme in $(READMES); do \
	  echo installing $${readme} in $(docdir)/$${readme}...; \
	  $(INSTALL_DATA) $(top_srcdir)/$${readme} $(DESTDIR)$(docdir)/$${readme} \
	    || exit 1; \
	done
	@for backdir in $(BACKDIRS); do \
	  $(MKDIR) $(DESTDIR)$(docdir)/$${backdir} ; \
	  for doc in $${backdir}/* ; do \
	    if test -f $${doc} ; then \
	      echo installing $${doc} in $(docdir)/$${doc}...; \
	      $(INSTALL_DATA) $${doc} $(DESTDIR)$(docdir)/$${doc} \
                || exit 1; \
	    fi \
	  done \
	done
	@-for doc in $(DOCS) $(HTML_PAGES); do \
	    echo installing $${doc} in $(docdir)/$${doc}...; \
	    $(INSTALL_DATA) $${doc} $(DESTDIR)$(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
	@for page in $(SECT8); do \
	  echo removing $${page} in $(mandir)/man8/$${page}...; \
	  rm -f $(mandir)/man8/$${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

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

../tools/sane-desc:
	cd ../tools ; $(MAKE) sane-desc

sane-backends.html: $(wildcard descriptions/*.desc) ../tools/sane-desc
	@../tools/sane-desc -m html-backends-split -s ${srcdir}/descriptions \
	 -i "The following table summarizes the backends/drivers distributed \
	     with $(PACKAGE)-$(VERSION), and the hardware or software they \
             support." -d 0 > sane-backends.html

sane-backends-external.html: $(wildcard descriptions-external/*.desc) \
                             ../tools/sane-desc
	@../tools/sane-desc -m html-backends-split -s ${srcdir}/descriptions-external \
	   -i "The following table summarizes the backends/drivers that \
	       have not yet been included in the SANE distribution, and the \
	       hardware or software they support." \
	   -t "SANE: External Backends (Drivers)" -d 0 \
	   > sane-backends-external.html

sane-mfgs.html: $(wildcard descriptions/*.desc) ../tools/sane-desc
	@../tools/sane-desc -m html-mfgs -s ${srcdir}/descriptions \
	   -i "The following table summarizes the devices supported \
	       by $(PACKAGE)-$(VERSION)." \
	   -d 0 > sane-mfgs.html

sane-mfgs-external.html: $(wildcard descriptions-external/*.desc) \
                             ../tools/sane-desc
	@../tools/sane-desc -m html-mfgs -s ${srcdir}/descriptions-external \
	   -i "The following table summarizes the devices that \
	       are supported by external backends." \
	   -t "SANE: Devices Supported by External Backends" -d 0 \
	   > sane-mfgs-external.html

html-pages: $(HTML_PAGES)

html: html-pages html-man sane-html

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

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

distclean: clean
	rm -f $(DISTCLEAN_FILES)
	for subdir in $(SUBDIRS) ; do \
	  ( cd $$subdir ; rm -f $(DISTCLEAN_FILES) ); \
	done
	rm -f $(MANPAGES)
	rm -f Makefile
	rm -f *.lot *.lof *.ind *.gz
	rm -f sane.dvi sane.ps sane-backends.html sane-backends-external.html
	rm -f sane-mfgs.html sane-mfgs-external.html
	rm -f sane/*.html sane/*.gif
	rm -f doxygen-sanei.conf
	-rm -rf sane sanei-html
	for manpage in $(MANPAGES) ; do \
	  rm -f $${manpage}.html ; \
	done

depend:

dist: $(DISTFILES)
	for subdir in $(SUBDIRS) ; do \
	  $(MKDIR) $(distdir)/doc/$${subdir} ; \
	  for file in $${subdir}/* ; do \
	    ln $$file $(distdir)/doc/$${subdir} 2> /dev/null \
	      || cp -p $$file $(distdir)/doc/$${subdir} \
	      || echo "ignoring: $$file" ; \
	  done ; \
	done
	for file in $(DISTFILES); do \
	  ln $$file $(distdir)/doc 2> /dev/null \
	    || cp -p $$file $(distdir)/doc ; \
	done

