srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
top_builddir = .
my_dir = .
INSTALL = @INSTALL@

ifdef NATIVE_PREFIX
HOSTCC=$(NATIVE_PREFIX)-gcc
else
HOSTCC=@CC@
endif
export HOSTCC

@MCONFIG@

@RESIZER_CMT@RESIZE_DIR= resize
@DEBUGFS_CMT@DEBUGFS_DIR= debugfs

LIB_SUBDIRS=lib/et lib/ss lib/e2p lib/ext2fs lib/uuid # lib/finddev
PROG_SUBDIRS=e2fsck $(DEBUGFS_DIR) misc $(RESIZE_DIR) tests/progs 
SUBDIRS=util $(LIB_SUBDIRS) $(PROG_SUBDIRS) tests

SUBS= lib/ext2fs/ext2_types.h

TAR=tar

all:: $(SUBS) 
	$(MAKE) libs
	$(MAKE) progs
	$(MAKE) docs

progs: $(SUBS) all-progs-recursive
libs: $(SUBS) all-libs-recursive

docs:
	cd doc && $(MAKE) libext2fs.info

install-doc-libs:
	cd doc && $(MAKE) install-doc-libs

uninstall-doc-libs:
	cd doc && $(MAKE) uninstall-doc-libs

clean-doc:
	cd doc && $(MAKE) clean

distclean-doc:
	cd doc && $(MAKE) distclean

install: $(SUBS) all-libs-recursive install-progs-recursive \
	install-shlibs-libs-recursive install-doc-libs
#	export MANPATH=$(DESTDIR)$(mandir); $(srcdir)/install-utils/compile_manpages

uninstall: uninstall-progs-recursive uninstall-shlibs-libs-recursive uninstall-doc-libs

install-libs: install-libs-recursive

uninstall-libs: uninstall-libs-recursive

TAGS clean-recursive distclean-recursive depend-recursive check-recursive \
	    mostlyclean-recursive realclean-recursive install-recursive:
	for subdir in $(SUBDIRS); do \
	  if test -d $$subdir ; then \
	    target=`echo $@|$(SED) 's/-recursive//'`; \
	    echo making $$target in $$subdir; \
	    (cd $$subdir && $(MAKE) $$target) || exit 1; \
	  fi ; \
	done

all-progs-recursive install-progs-recursive uninstall-progs-recursive:
	for subdir in $(PROG_SUBDIRS); do \
	  if test -d $$subdir ; then \
	    target=`echo $@|$(SED) 's/-progs-recursive//'`; \
	    echo making $$target in $$subdir; \
	    (cd $$subdir && $(MAKE) $$target) || exit 1; \
	  fi ; \
	done

all-libs-recursive install-libs-recursive uninstall-libs-recursive install-shlibs-libs-recursive uninstall-shlibs-libs-recursive:
	for subdir in $(LIB_SUBDIRS); do \
	  if test -d $$subdir ; then \
	    target=`echo $@|$(SED) 's/-libs-recursive//'`; \
	    echo making $$target in $$subdir; \
	    (cd $$subdir && $(MAKE) $$target) || exit 1; \
	  fi ; \
	done

mostlyclean: mostlyclean-recursive mostlyclean-local

clean: clean-recursive clean-local clean-doc
	$(RM) -f $(SUBS)

distclean: distclean-doc distclean-recursive distclean-local 

realclean: realclean-recursive realclean-local

depend:: depend-recursive

lib/ext2fs/ext2_types.h: $(DEP_SUBSTITUTE) $(srcdir)/lib/ext2fs/ext2_types.h.in
	$(SUBSTITUTE) $(srcdir)/lib/ext2fs/ext2_types.h.in \
	> lib/ext2fs/ext2_types.h

mostlyclean-local:
	$(RM) -f \#* *~ *.orig core MAKELOG 

clean-local: mostlyclean-local

distclean-local: clean-local
	$(RM) -f $(SUBS) $(SUBST_CONF) \
		config.status config.log config.cache MCONFIG Makefile \
		$(srcdir)/TAGS $(srcdir)/Makefile.in.old

realclean-local: distclean-local
	$(RM) -f configure

check::	$(SUBS) check-recursive

distribution_tar_file:
	$(RM) -rf /tmp/dest
	$(MAKE) DESTDIR=/tmp/dest install
	cp -r $(srcdir)/README $(srcdir)/install-utils /tmp/dest
	$(RM) -rf /tmp/dest/install-utils/CVS /tmp/dest/install-utils/ChangeLog
	cp $(srcdir)/INSTALL.@BINARY_TYPE@ /tmp/dest/INSTALL
	(cd /tmp/dest && $(TAR) cf - . ) | gzip -9 \
		> e2fsprogs-@E2FSPROGS_VERSION@-@BINARY_TYPE@.tar.gz

SRCROOT = `echo e2fsprogs-@E2FSPROGS_VERSION@ | sed -e 's/-WIP//' \
			-e 's/pre-//' -e 's/-PLUS//'`

$(srcdir)/.exclude-file:
	a=$(SRCROOT); \
	(cd $(srcdir)/.. && find e2fsprogs \( -name \*~ -o -name \*.orig \
		-o -name CVS -o -name \*.rej -o -name Makefile.pq \
		-o -name TAGS -o -name \*.old -o -name \*.gmo -o -name SCCS \
		-o -name changed-files -o -name .#\* \) \
		-print) | sed -e "s/e2fsprogs/$$a/" > $(srcdir)/.exclude-file
	echo "$(SRCROOT)/BitKeeper" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/build" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/rpm.log" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/TODO" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/powerquest" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/.exclude-file" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/po/stamp-cat-id" >> $(srcdir)/.exclude-file
	echo "$(SRCROOT)/po/cat-id-tbl.c" >> $(srcdir)/.exclude-file
	echo $(SRCROOT)/e2fsprogs-@E2FSPROGS_VERSION@.tar.gz \
		>> $(srcdir)/.exclude-file

source_tar_file: $(srcdir)/.exclude-file
	cd $(srcdir)/.. && a=$(SRCROOT); rm -f $$a ; ln -sf e2fsprogs $$a ; \
		$(TAR) -c -h -v -f - \
			-X $$a/.exclude-file $$a | \
		gzip -9 > e2fsprogs-@E2FSPROGS_VERSION@.tar.gz
	rm -f $(srcdir)/.exclude-file
