all:

PROCESSES=MMCombo TinyFTPD SoundPlay
MMCombo_COMP=MMCombo Behaviors Events Motion Shared SoundPlay/SoundManager.o SoundPlay/WAV.o Vision Wireless WorldModel2
TinyFTPD_COMP=TinyFTPD
SoundPlay_COMP=SoundPlay Shared/Config.o Shared/ProcessID.o Events/EventRouter.o Events/EventTranslator.o Events/EventBase.o Shared/LoadSave.o Shared/get_time.o
TARGETS=all clean
OPENRSDK_ROOT?=/usr/local/OPEN_R_SDK
LD=$(OPENRSDK_ROOT)/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:=$(foreach comp,$(COMPONENTS), $(if $(suffix $(comp)),$(comp),$(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)

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

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

all: reportTarget updateComponents $(BUILDS)

reportTarget:
	@echo " ** Targeting $(TEKKOTSU_TARGET_MODEL) for build ** ";

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

%.o:
	@if [ "$(dir $@)" != "./" -a -d "$(dir $@)" ] ; then \
		echo "cd $(dir $@) && $(MAKE) $(notdir $@)"; \
		cd $(dir $@) && $(MAKE) $(notdir $@); \
	else \
		echo "Unknown target"; \
		exit 1; \
	fi;

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

updateComponents:
	@for comp in tools/ $(LIB_COMPONENTS); do \
		dir=`echo "$$comp" | sed 's/\/[^/]*$$//'`; \
		name=`echo "$$comp" | sed 's/.*\///'`; \
		echo "** BUILDING: $$dir/$$name"; \
		(cd $$dir && $(MAKE) $$name); \
		if [ $$? -ne 0 ] ; then \
			exit 1; \
		fi; \
	done

clean:
	@for comp in tools $(LIB_COMPONENTS); do \
		dir=`echo "$$comp" | sed 's/\/[^/]*$$//'`; \
		name=`echo "$$comp" | sed 's/.*\///'`; \
		echo "** CLEANING: $$dir" ;\
		(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;

update:
	$(MAKE) all;
	(cd project && $(MAKE) update);
