SHELL = /bin/sh

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

PACKAGE = @PACKAGE@
VERSION = @VERSION@
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

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

CC = @CC@
INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/include -I$(top_srcdir)/include \
	@INCLUDES@
DEFS = @DEFS@
CPPFLAGS = @CPPFLAGS@
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

LIBTOOL = ../libtool
MINST	= --mode=install
MLINK	= --mode=link
SANED	= @SANED@

COMPILE = $(CC) -c $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
LINK = $(CC) $(LDFLAGS) -o $@

DISTCLEAN_FILES = @DISTCLEAN_FILES@

@SET_MAKE@

BINPROGS = scanimage # tstbackend
SBINPROGS = $(SANED)
PROGRAMS = $(BINPROGS) $(SBINPROGS)
TESTPROGRAMS = test
LIBLIB = ../lib/liblib.a
LIBSANE = ../backend/libsane.la
LIBSANEI = ../sanei/libsanei.a

SCAN_OBJS = scanimage.o stiff.o

DISTFILES = Makefile.in saned.c scanimage.c stiff.c stiff.h test.c tstbackend.c

.PHONY: all clean depend dist distclean install uninstall

.c.o:
	$(COMPILE) $<

all: $(PROGRAMS)

install: $(PROGRAMS)
	$(MKDIR) $(DESTDIR)$(bindir) $(DESTDIR)$(sbindir) $(DESTDIR)$(datadir)
	@for program in $(BINPROGS); do \
	  $(LIBTOOL) $(MINST) $(INSTALL_PROGRAM) $${program} \
	    $(DESTDIR)$(bindir)/$${program}; \
	done
	@for program in $(SBINPROGS); do \
	  $(LIBTOOL) $(MINST) $(INSTALL_PROGRAM) $${program} \
	    $(DESTDIR)$(sbindir)/$${program}; \
	done

uninstall:
	@for program in $(BINPROGS); do \
	  rm -f $(bindir)/$${program}; \
	done
	@for program in $(SBINPROGS); do \
	  rm -f $(sbindir)/$${program}; \
	done

test: test.o $(LIBSANE) $(LIBLIB)
	@$(LIBTOOL) $(MLINK) $(LINK) test.o $(LIBLIB) $(LIBSANE) $(LIBS)

saned: saned.o $(LIBSANEI) $(LIBSANE) $(LIBLIB)
	@$(LIBTOOL) $(MLINK) $(LINK) saned.o $(LIBSANEI) $(LIBLIB) $(LIBSANE)  \
		$(LIBS)

scanimage: $(SCAN_OBJS) $(LIBSANE) $(LIBLIB)
	@$(LIBTOOL) $(MLINK) $(LINK) $(SCAN_OBJS) $(LIBLIB) $(LIBSANE)  $(LIBS)

# tstbackend is not compiled by default. 
# To compile it, add tstbackend to BINPROGS.
tstbackend: tstbackend.o $(LIBSANE) $(LIBLIB)
	@$(LIBTOOL) $(MLINK) $(LINK) tstbackend.o $(LIBLIB) $(LIBSANE) $(LIBS)

clean:
	rm -f *.o tstbackend
	rm -rf .libs

distclean: clean
	rm -f $(DISTCLEAN_FILES)
	rm -f Makefile $(PROGRAMS) $(TESTPROGRAMS)

depend:
	makedepend $(INCLUDES) *.c 2>/dev/null

dist: $(DISTFILES)
	for file in $(DISTFILES); do \
	  ln $$file $(distdir)/frontend 2> /dev/null \
	    || cp -p $$file $(distdir)/frontend ; \
	done

