
DIRS = $(shell ls)
COMMON_OBJ := common_obj
export EXE_DIR = $(shell pwd)/exedir
export COMMON_OBJ_DIR = $(shell pwd)/objdir

.PHONY: all clean setup compile_common $(DIRS)

all: setup

setup:
	@echo Setting up...
	-mkdir $(EXE_DIR)
	-mkdir $(COMMON_OBJ_DIR)

$(DIRS): compile_common
	@-if [ -d $@ ];\
	then\
		cd $@;\
		if [ -f Makefile ];\
		then\
			$(MAKE);\
		fi\
	fi

compile_common:
	echo Producing Common obj files...
	touch $(COMMON_OBJ_DIR)/$(COMMON_OBJ)

clean:
	@-for i in $(DIRS);\
	do\
		(if [ -d $$i ];\
		then\
			cd $$i;\
			if [ -f Makefile ];\
			then\
				$(MAKE) clean;\
			fi\
		fi)\
	done
	@-rm $(COMMON_OBJ_DIR)/$(COMMON_OBJ)
	@-rmdir $(EXE_DIR)
	@-rmdir $(COMMON_OBJ_DIR)
