# common part for sub directories Makefiles
#
# each submakefile should define SOURCES and TARGET
# optionnaly HEADRS and INCLUDES

DESTDIR =
srcdir = $(top_builddir)/src
prefix      = @prefix@
includedir  = @includedir@
SHELL       = /bin/sh
RM          = rm -fr
INSTALL     = @INSTALL@
LIBTOOL     = @LIBTOOL@
CC	    = @CC@
@SET_MAKE@

CFLAGS	    = -I$(top_builddir)/include -I$(top_builddir) -I. $(INCLUDES) @CFLAGS@ @CPPFLAGS@ @DEFS@

LDFLAGS     = @LDFLAGS@
OBJECTS	    = $(SOURCES:.c=.lo)

all: $(TARGET).la

.SUFFIXES: .lo .c

.c.lo:
	$(LIBTOOL) --mode=compile $(CC) -c $(CFLAGS) $<

$(TARGET).la: $(OBJECTS) $(SUBLIBS)
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(SUBLIBS)

$(SUBLIBS) no-sublibs: all-recursive

clean: clean-recursive
	$(RM) *.o *.lo $(TARGET).la .libs *~

# install headers here only
install: $(HEADERS) install-recursive
	for header in $(HEADERS) _listend; do \
	  if [ $$header != _listend ]; then \
            $(INSTALL) -m 644 $$header $(DESTDIR)$(includedir)/libetpan ; \
	  fi \
	done

Makefile: $(top_builddir)/Rules

$(top_builddir)/Rules: $(top_builddir)/Rules.in $(top_builddir)/config.status
	cd ${top_builddir} && CONFIG_FILES=`basename $@` \
	CONFIG_HEADERS= ./config.status

prepare: prepare-recursive
	mkdir -p ${top_builddir}/include/libetpan
	@for header in $(HEADERS) _listend; do \
	  [ "$$header" = _listend ] && break ; \
	  cmp -s "$$header" "${top_builddir}/include/libetpan/$$header" && continue ; \
	  cp -pf "$$header" ${top_builddir}/include/libetpan ; \
	  echo "Copied $$header -> include/libetpan/$$header" ; \
	done

# recursive
all-recursive install-recursive clean-recursive \
        distclean-recursive prepare-recursive:
		@set fnord $(MAKEFLAGS); amf=$$2; \
		list='$(SUBDIRS)'; for subdir in $$list; do \
			target=`echo $@ | sed s/-recursive//`; \
			echo "Making $$target in $$subdir"; \
			(cd $$subdir && $(MAKE) $$target) \
			|| case "$$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; \
		done && test -z "$$fail"

