all:

PROCESSES=MMCombo TinyFTPD
MMCombo_COMP=MMCombo Behaviors Events Motion Shared Vision Wireless
TinyFTPD_COMP=TinyFTPD
TARGETS=all clean
PREFIX=/usr/local/OPEN_R_SDK
LD=$(PREFIX)/bin/mipsel-linux-ld

# list of components for all processes
# NOT SORTED, process directories need to be made first (generated stub header files)
COMPONENTS:=$(foreach proc,$(PROCESSES),$($(proc)_COMP))

# for all components of all processes:
# converts directory component name to expected library name: MMCombo -> MMCombo/MMComboLib.o
# sorted to remove duplicates
LIB_COMPONENTS:=$(sort $(foreach comp,$(COMPONENTS), $(comp)/$(comp)Lib.o))

# for all processes:
# creates 'target' names: MMCombo -> build/MMCombo.o
BUILDS:=$(foreach proc,$(PROCESSES),build/$(proc).o)

#the automatic way of doing this part is too messy, just add a rule for each processes by hand: (skip lines!)
$(word 1,$(BUILDS)): $($(word 1,$(PROCESSES))_COMP)

$(word 2,$(BUILDS)): $($(word 2,$(PROCESSES))_COMP)

.PHONY: $(TARGETS) docs doc cleandoc tools updateComponents testbuild

all: updateComponents $(BUILDS)

tools:
	cd tools && $(MAKE) $@; \

%.o:
	cd $(dir $@) && $(MAKE) $(notdir $@)

$(BUILDS):
	@if [ ! -d build ] ; then mkdir build; fi;
	@echo "Linking component object files..."
	@echo "$@ <- $(foreach comp,$($(basename $(notdir $@))_COMP),$(comp)/$(comp)Lib.o)"
	@$(LD)	-i $(foreach comp,$($(basename $(notdir $@))_COMP),$(comp)/$(comp)Lib.o) -o $@
	@echo "Copying .ocf files..."
	@cp $(basename $(notdir $@))/$(notdir $@)cf $(dir $@)

updateComponents:
	@for dir in tools $(COMPONENTS); do \
		echo "** BUILDING: $$dir"; \
		(cd $$dir && $(MAKE) ); \
		if [ $$? -ne 0 ] ; then \
			exit 1; \
		fi; \
	done

clean:
	@for dir in tools $(COMPONENTS); do \
		(cd $$dir && $(MAKE) $@); \
		if [ $$? -ne 0 ] ; then \
			exit 1; \
		fi; \
	done
	rm -f docs/doxygenbuildcfg;
	rm -rf build;

memstick: install
	tools/cpymem;
	@echo "Copy completed; memory stick unmounted"
#	tools/mntmem;
#	cp -r ms/* /mnt/memstick
#	tools/umntmem;

ftp: install
	tools/mstreeput 192.168.0.5 ms;

docs doc:
	docs/builddocs --update

cleandoc:
	docs/builddocs --clean

fullcopy:
	tools/mntmem;
	cp -r ms/* /mnt/memstick
	tools/umntmem;
