include $(SRCTOP)/Makefile.vars

.PHONY:  all clean depend lib subdirs headers bin test install subdirs-install clean-install

all: lib bin test

install:
	$(MAKE) install-lib install-shlib install-etc install-include install-bin install-test install-resources

install-lib:
	$(RM) -r $(LIBDIR.DEST)
	mkdir $(LIBDIR.DEST)
	(cd install/lib/$(PROJECT.NAME); tar cf - *) | (cd $(LIBDIR.DEST); tar xf -)

install-shlib:
	$(RM) -r $(SHLIBDIR.DEST)
	mkdir $(SHLIBDIR.DEST)
	(cd install/shlib/$(PROJECT.NAME); tar cf - *) | (cd $(SHLIBDIR.DEST); tar xf -)

install-include:
	$(RM) -r $(INCDIR.DEST)
	mkdir $(INCDIR.DEST)
	(cd install/include; tar cf - *) | (cd $(INCDIR.DEST); tar xf -)

install-bin:
	$(RM) -r $(BINDIR.DEST)
	mkdir $(BINDIR.DEST)
	(cd install/bin; tar cf - *) | (cd $(BINDIR.DEST); tar xf -)

install-test:
	$(RM) -r $(TESTDIR.DEST)
	mkdir $(TESTDIR.DEST)
	(cd install/test; tar cf - *) | (cd $(TESTDIR.DEST); tar xf -)

install-etc:
	$(RM) -r $(ETCDIR.DEST)
	mkdir $(ETCDIR.DEST)
	(cd install/etc; tar cf - *) | (cd $(ETCDIR.DEST); tar xf -)

install-resources:
	$(RM) -r $(RESDIR.DEST)
	mkdir $(RESDIR.DEST)
	(cd install/resources; tar cf - *) | (cd $(RESDIR.DEST); tar xf -)

install-clean:
	$(RM) -r $(BINDIR.DEST) $(INCDIR.DEST) $(LIBDIR.DEST) $(RESDIR.DEST) $(SHLIBDIR.DEST)

linstall-clean:
	$(RM) -r install
	$(MAKE) install-subdirs

linstall-subdirs::
	mkdir install
	mkdir install/lib
	mkdir install/lib/$(PROJECT.NAME)
	mkdir install/shlib
	mkdir install/shlib/$(PROJECT.NAME)
	mkdir install/etc
	mkdir install/bin
	mkdir install/test
	mkdir install/include
	mkdir install/include/$(PROJECT.NAME)
	mkdir $(addprefix install/include/$(PROJECT.NAME)/,$(PROJECT.LIBS))
	mkdir install/resources

lib:
	if [ "$(PROJECT.LIBS)" != "" ]; then $(MAKE) -C lib all; fi

bin:
	if [ "$(PROJECT.BINDIRS)" != "" ]; then $(MAKE) -C bin all; fi

test:
	if [ "$(PROJECT.TESTDIRS)" != "" ]; then $(MAKE) -C test all; fi

depend:
	if [ "$(PROJECT.LIBS)" != "" ]; then $(MAKE) -C lib depend; fi
	if [ "$(PROJECT.BINDIRS)" != "" ]; then $(MAKE) -C bin depend; fi
	if [ "$(PROJECT.TESTDIRS)" != "" ]; then $(MAKE) -C test depend; fi

headers:
	if [ "$(PROJECT.LIBS)" != "" ]; then $(MAKE) -C lib headers; fi

clean:
	 find $(SRCTOP) \( -name \*~ -o -name core \) -print | xargs rm

reset:
	@pwd | grep -v MASTER > /dev/null || (echo "make: can't make reset in source directory" && exit 1)
	$(RM) -r *
	echo "SRCTOP = $(SRCTOP)" > Makefile.vars.config
	echo "CONFIG = $(CONFIG)" >> Makefile.vars.config
	ln -s $(SRCTOP)/Makefile.src Makefile
	$(MAKE) subdirs
	$(MAKE) linstall-subdirs

subdirs:
	if [ "$(PROJECT.LIBS)" != "" ]; then mkdir lib; ln -s $(SRCTOP)/lib/Makefile.src lib/Makefile; $(MAKE) -C lib subdirs; fi
	if [ "$(PROJECT.BINDIRS)" != "" ]; then mkdir bin; ln -s $(SRCTOP)/bin/Makefile.src bin/Makefile; $(MAKE) -C bin subdirs; fi
	if [ "$(PROJECT.TESTDIRS)" != "" ]; then mkdir test; ln -s $(SRCTOP)/test/Makefile.src test/Makefile; $(MAKE) -C test subdirs; fi

Makefile.config.vars:
	echo "SRCTOP = $(SRCTOP)" > Makefile.vars.config
	echo "CONFIG = $(CONFIG)" >> Makefile.vars.config
