# Make sure the default target is 'all' by listing it first
all:

###################################################
##             ENVIRONMENT SETUP                 ##
###################################################


###########  ENVIRONMENT VARIABLES  ###############
# If you need to modify these, you should set them as environment
# variables instead of changing them here - that way other tools (such
# as mntmem and crashDebug) can use these as well.

# Directory the Tekkotsu framework is stored
TEKKOTSU_ROOT ?= /usr/local/Tekkotsu

# Location where the memstick will be mounted
MEMSTICK_ROOT ?= /mnt/memstick

# Directory where the OPEN-R SDK was installed
# See http://www.tekkotsu.org/SDKInstall.html
# or the OPEN-R website: http://openr.aibo.com/
OPENRSDK_ROOT ?= /usr/local/OPEN_R_SDK

# in case your memory stick drivers use uppercase, you'll need to
# set 'FILENAME_CASE' to 'upper'
FILENAME_CASE ?= lower

# this will delete files from the memory stick that aren't also in
# your build image - except files at the root level of the memstick,
# like memstick.ind
# default: OFF (empty string) - any non-empty string is ON
STRICT_MEMSTICK_IMAGE ?=


#############  MAKEFILE VARIABLES  ################
# Change these right here in the Makefile

# Want any other libraries passed to the compiler?  Put them here.
LIBS=-lObjectComm -lOPENR -lInternet -lantMCOOP

# What model AIBO are you targeting?  This will look at the
# $TEKKOTSU_ROOT/TARGET_MODEL file to find out.  If the file is not
# found, it is created with the default setting TGT_ERS2xx.
# change the target model, make will automatically recompile
# everything for you.
# Legal values: TGT_ERS210 TGT_ERS220 TGT_ERS2xx 
# If you want to change this, edit the TARGET_MODEL file, not this line!
TEKKOTSU_TARGET_MODEL:=$(shell if [ ! -r "$(TEKKOTSU_ROOT)/TARGET_MODEL" ] ; then echo "TGT_ERS2xx" > $(TEKKOTSU_ROOT)/TARGET_MODEL ; fi ; cat $(TEKKOTSU_ROOT)/TARGET_MODEL )

# Would you like some more compiler flags?  We like lots of warnings.
# There are some files with exceptions to these flags - MMCombo*.cc
# need to have optimizations turned off, and several TinyFTPD sources
# have -Weffc++ and -DOPENR_DEBUG turned off.  If you want to modify
# these exceptions, look in the middle of the 'Makefile Machinery'
# section. (grep/search for the file name)
CXXFLAGS= \
	-g -pipe -O2 -frename-registers -fomit-frame-pointer -ffast-math -fno-common \
	-Wall -W -Wshadow -Wlarger-than-8192 -Wpointer-arith -Wcast-qual \
	-Woverloaded-virtual -Weffc++ -Winline -Wdeprecated -Wnon-virtual-dtor \
	-I"`pwd`" -I$(TEKKOTSU_ROOT) \
	-isystem $(TEKKOTSU_ROOT)/Shared/jpeg-6b \
	-isystem $(OPENRSDK_ROOT)/OPEN_R/include/MCOOP \
	-isystem $(OPENRSDK_ROOT)/OPEN_R/include/R4000 \
	-isystem $(OPENRSDK_ROOT)/OPEN_R/include \
	-DPLATFORM_APERIOS -DDEBUG -DOPENR_DEBUG -D$(TEKKOTSU_TARGET_MODEL) $(GLOBAL_MAP) \

#Suggested additional flags
# -ffast-math


###################################################
##              SOURCE CODE LIST                 ##
###################################################
# Find all of the source files: (except temp files in build directory)
# You shouldn't need to change anything here unless you want to add
# external libraries

BUILDDIR=build
$(shell mkdir -p $(BUILDDIR))
$(shell mkdir -p $(TEKKOTSU_ROOT)/$(BUILDDIR))
SRCSUFFIX=.cc
PROJ_SRCS:=$(shell find . -name "*$(SRCSUFFIX)" \! -path "*/$(BUILDDIR)/*")
# We're assuming all of the source files we found are to be linked
# into MMCombo.
MAIN_SRCS:=$(PROJ_SRCS)

USERLIBS:=$(TEKKOTSU_ROOT)/Shared/jpeg-6b/libjpeg.a


###################################################
##               PROCESS SETUP                   ##
###################################################
# This part will set up the four processes:
# MMCombo is forked into Main and Motion
# TinyFTPD is the FTP server
# SoundPlay handles sound output

# It should be pretty easy to add a new process here if
# you're inclined to do so.

# These all are relative to TEKKOTSU_ROOT
PROCESS_OBJS=MMCombo TinyFTPD SoundPlay
MMCombo_COMP=MMCombo MMCombo/MMComboStub.o Behaviors Events Motion Shared SoundPlay/SoundManager.o SoundPlay/WAV.o Vision Wireless
TinyFTPD_COMP=TinyFTPD TinyFTPD/TinyFTPDStub.o
SoundPlay_COMP=SoundPlay SoundPlay/SoundPlayStub.o Shared/Config.o Shared/ProcessID.o Events/EventRouter.o Events/EventTranslator.o Events/EventBase.o Events/VisionObjectEvent.o Shared/LoadSave.o Shared/get_time.o
STUB_FILES:=$(addprefix $(TEKKOTSU_ROOT)/,$(addsuffix /stub.cfg,$(PROCESS_OBJS)))
STUB_CHECK_FILES:=$(addprefix $(TEKKOTSU_ROOT)/$(BUILDDIR)/,$(subst /,-,$(STUB_FILES)))

# It's important that the process names and variables are the same
# case as your memory stick reader - memsticks don't store case, and
# some drivers may convert all file names to uppercase, others to
# lower case.  Ours is lower case.  I think this only really matters
# if you want to update a stick instead of recopying it (make update
# vs. make install)

ifeq ($(FILENAME_CASE),lower)

PROCESSES=mmcombo tinyftpd sndplay
mmcombo_OBJS:=$(MAIN_SRCS:$(SRCSUFFIX)=.o) $(TEKKOTSU_ROOT)/$(BUILDDIR)/MMCombo.o
mmcombo_OCF:=$(TEKKOTSU_ROOT)/MMCombo/MMCombo.ocf
tinyftpd_OBJS:=$(TEKKOTSU_ROOT)/$(BUILDDIR)/TinyFTPD.o
tinyftpd_OCF:=$(TEKKOTSU_ROOT)/TinyFTPD/TinyFTPD.ocf
sndplay_OBJS:=$(TEKKOTSU_ROOT)/$(BUILDDIR)/SoundPlay.o
sndplay_OCF:=$(TEKKOTSU_ROOT)/SoundPlay/SoundPlay.ocf
CONVERTCASE=$(TEKKOTSU_ROOT)/tools/makelowercase

BINSUFFIX=.bin
MSIMGDIR=ms
INSTALLDIR=$(MSIMGDIR)/open-r/mw/objs
MMCOMBOBIN=mmcombo.bin
MAINFORK=mainobj.bin
MOTOFORK=motoobj.bin

else

PROCESSES=MMCOMBO TINYFTPD SNDPLAY
MMCOMBO_OBJS:=$(MAIN_SRCS:$(SRCSUFFIX)=.o) $(TEKKOTSU_ROOT)/$(BUILDDIR)/MMCombo.o
MMCOMBO_OCF:=$(TEKKOTSU_ROOT)/MMCombo/MMCombo.ocf
TINYFTPD_OBJS:=$(TEKKOTSU_ROOT)/$(BUILDDIR)/TinyFTPD.o
TINYFTPD_OCF:=$(TEKKOTSU_ROOT)/TinyFTPD/TinyFTPD.ocf
SNDPLAY_OBJS:=$(TEKKOTSU_ROOT)/$(BUILDDIR)/SoundPlay.o
SNDPLAY_OCF:=$(TEKKOTSU_ROOT)/SoundPlay/SoundPlay.ocf
CONVERTCASE=$(TEKKOTSU_ROOT)/tools/makeuppercase

BINSUFFIX=.BIN
MSIMGDIR=MS
INSTALLDIR=$(MSIMGDIR)/OPEN-R/MW/OBJS
MMCOMBOBIN=MMCOMBO.BIN
MAINFORK=MAINOBJ.BIN
MOTOFORK=MOTOOBJ.BIN

endif

# creates process build target names: MMCombo -> build/MMCombo.o
BUILDS:=$(foreach proc,$(PROCESS_OBJS),$(TEKKOTSU_ROOT)/$(BUILDDIR)/$(proc).o)

# Each process build target depends on the component object files that
# should be linked together.  These live in the
# $TEKKOTSU_ROOT/$BUILDDIR directory and should be fairly static
# unless you're editing the framework itself.  The automatic way of
# creating these dependancies automatically is too messy, just add a
# rule for each process by hand: (skip lines!)
$(word 1,$(BUILDS)): $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$($(word 1,$(PROCESS_OBJS))_COMP)), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc") )))

$(word 2,$(BUILDS)): $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$($(word 2,$(PROCESS_OBJS))_COMP)), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc") )))

$(word 3,$(BUILDS)): $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$($(word 3,$(PROCESS_OBJS))_COMP)), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc") )))

# Each of the executable binaries depends on the corresponding
# framework build target(s) plus any project files which were
# specified above. (by default, all project files are linked into
# MMCombo) These live in the project directory's $BUILDDIR and will
# need to be recompiled any time you change source that's part of that
# process (but not otherwise ;)
# Again, the automatic way of creating these dependancies
# automatically is too messy, just add a rule for each process by
# hand: (skip lines!)
PROC_BINS:=$(addprefix $(BUILDDIR)/,$(addsuffix $(BINSUFFIX),$(PROCESSES)))

$(word 1,$(PROC_BINS)): $($(word 1,$(PROCESSES))_OBJS) $($(word 1,$(PROCESSES))_OCF)

$(word 2,$(PROC_BINS)): $($(word 2,$(PROCESSES))_OBJS) $($(word 2,$(PROCESSES))_OCF)

$(word 3,$(PROC_BINS)): $($(word 3,$(PROCESSES))_OBJS) $($(word 3,$(PROCESSES))_OCF)


###################################################
##             MAKEFILE MACHINERY                ##
###################################################
# Hopefully, you shouldn't have to change anything down here,
# except one or two little things ;)

# List of all components for all processes, sorted to
# remove duplicates.
COMPONENTS:=$(sort $(foreach proc,$(PROCESS_OBJS),$($(proc)_COMP)))

# list of all source files of all components, sorted to remove
# duplicates.  This gives us all the source files which we care about,
# all in one place.
SRCS:=$(sort $(foreach comp,$(COMPONENTS), $(if $(suffix $(filter-out %.a,$(comp))),$(TEKKOTSU_ROOT)/$(basename $(comp)).cc,$(shell find $(TEKKOTSU_ROOT)/$(comp) -name "*.cc") ))) $(MAIN_SRCS)
OBJS:=$(SRCS:$(SRCSUFFIX)=.o)
DEPENDS:=$(SRCS:$(SRCSUFFIX)=.d)

# These are the file names of the final executable binaries, in the
# memstick image directory
INSTALL_BINS:=$(addprefix $(INSTALLDIR)/,$(addsuffix $(BINSUFFIX),$(PROCESSES)))

CXX=$(OPENRSDK_ROOT)/bin/mipsel-linux-g++
LD=$(OPENRSDK_ROOT)/bin/mipsel-linux-ld
STRIP=$(OPENRSDK_ROOT)/bin/mipsel-linux-strip
MKBIN=$(OPENRSDK_ROOT)/OPEN_R/bin/mkbin
STUBGEN=$(OPENRSDK_ROOT)/OPEN_R/bin/stubgen2
COLORFILT=${TEKKOTSU_ROOT}/tools/colorfilt
FILTERSYSWARN=$(TEKKOTSU_ROOT)/tools/filtersyswarn/filtersyswarn $(OPENRSDK_ROOT)
MKBINFLAGS=-p $(OPENRSDK_ROOT)

.PHONY: all compile install clean cleanDeps cleanProj cleanTemps Tekkotsu reportTarget newstick update $(INSTALLDIR)/$(MMCOMBOBIN) docs dox doc cleandoc updateTools

all: compile
	@echo "Build successful."
	@echo "Type: '$(MAKE) install' to copy all files to the memory stick"
	@echo "  or: '$(MAKE) update' to copy only changed files"

#the touch at the end is because memsticks seem to round time to even seconds, which screws up updates.  Grr.
reportTarget:
	@echo " ** Targeting $(TEKKOTSU_TARGET_MODEL) for build ** ";

updateTools:
	cd $(TEKKOTSU_ROOT)/tools && $(MAKE);

compile: reportTarget cleanTemps updateTools $(USERLIBS) $(INSTALL_BINS)
	@image="$(BUILDDIR)/$(notdir $(MEMSTICK_ROOT))" ; \
	if [ $(TEKKOTSU_ROOT)/TARGET_MODEL -nt $$image ] ; then \
		echo "Deleting old cached OPEN-R binaries"; \
		rm -rf $$image ; \
	fi; \
	if [ \! -d $$image ] ; then \
		if [ \! -d $(OPENRSDK_ROOT)/OPEN_R/MS/WCONSOLE/nomemprot ] ; then \
			echo "Could not find OPEN-R system binaries" ; \
			exit 1 ; \
		fi ; \
		echo "Copying system files..." ; \
		cp -r $(OPENRSDK_ROOT)/OPEN_R/MS/WCONSOLE/nomemprot $$image ; \
		chmod -R u+w $$image ; \
		(cd $$image; $(CONVERTCASE) -r . ); \
		rm -f $$image/open-r/mw/conf/connect.cfg $$image/open-r/mw/conf/object.cfg $$image/open-r/system/conf/wlandflt.txt ; \
		curt=`date +%Y%m%d%H%M`; \
		find $$image -exec touch -ft $$curt \{\} \; ; \
	fi


ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),cleanProj)
ifneq ($(MAKECMDGOALS),cleanTemps)
ifneq ($(MAKECMDGOALS),cleanDeps)
-include $(DEPENDS)
endif
endif
endif
endif

$(STUB_CHECK_FILES):
	@thedir=`echo $@ | sed 's/.*-\(.*\)-.*/\1/g'`; \
	cd $(TEKKOTSU_ROOT)/$$thedir ; \
	$(STUBGEN) stub.cfg ; \
	touch $@ ;

%.d : $(STUB_CHECK_FILES) %$(SRCSUFFIX)
	@echo Building $@...
	@$(CXX) $(CXXFLAGS) -MP -MG -MT $@ -MT $*.o -MM $*$(SRCSUFFIX) > $@

%/def.h %/entry.h: %/stub.cfg
	@echo "Doing $@"
	cd $* && $(STUBGEN) stub.cfg
	@echo "Done with $@"

#%/entry.h: %/stub.cfg
#	cd $(TEKKOTSU_ROOT)/$* && $(STUBGEN) stub.cfg

%Stub.h %Stub.cc: 
	@echo "Doing $@"
	cd $(dir $@) && $(STUBGEN) stub.cfg
	@echo "Done with $@"

#%Stub.cc: 
#	cd $(dir $@) && $(STUBGEN) stub.cfg
#	@echo "Done with $@"

%.h :
	@if [ "$(notdir $@)" = "def.h" -o "$(notdir $@)" = "entry.h" ] ; then \
		echo "WARNING: You shouldn't be seeing this message.  Report that you did." ; \
		echo "         Try a clean recompile." ; \
	fi;
	@echo "ERROR: Seems to be a missing header file '$@'...";
	@echo "       Someone probably forgot to check a file into CVS.";
	@echo "       I'll try to find where it's being included from:";
	@find . -name "*.h" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find . -name "*.cc" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find $(TEKKOTSU_ROOT) -name "*.h" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find $(TEKKOTSU_ROOT) -name "*.cc" -exec grep -H "$(notdir $@)" \{\} \; ;
	@echo "You might need to remove all the .d files (make cleanDeps) to get rid of this error.";
	@exit 1

#The "fork" we do of MMCombo into MainObj and MotoObj crashes with optimization... not a big loss, just turn it off for these files
$(TEKKOTSU_ROOT)/MMCombo/MMCombo.o $(TEKKOTSU_ROOT)/MMCombo/MMComboStub.o: %.o: %$(SRCSUFFIX) $(FILTERSYSWARN)  $(TEKKOTSU_ROOT)/TARGET_MODEL
	@if [ -r dist_hosts.txt ] ; then \
		echo "Adding $@ to job list"; \
		echo "$(CXX) $(filter-out -O2,$(CXXFLAGS)) -o $@ -c $< > $*.log 2>&1; retval=\$$?; cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); test \$$retval -eq 0;" >> $(BUILDDIR)/joblist.txt ; \
		touch $@; \
		echo "$@" >> $(BUILDDIR)/touched.txt ; \
	else \
		echo "Compiling $<... (no -O2)"; \
		$(CXX) $(filter-out -O2,$(CXXFLAGS)) -o $@ -c $< > $*.log 2>&1; \
		retval=$$?; \
		cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); \
		test $$retval -eq 0; \
	fi;

$(TEKKOTSU_ROOT)/TinyFTPD/TinyFTPD.o $(TEKKOTSU_ROOT)/TinyFTPD/FtpMethod.o $(TEKKOTSU_ROOT)/TinyFTPD/FtpRequest.o $(TEKKOTSU_ROOT)/TinyFTPD/FtpDTP.o $(TEKKOTSU_ROOT)/TinyFTPD/FtpPI.o: %.o: %$(SRCSUFFIX) $(FILTERSYSWARN) $(TEKKOTSU_ROOT)/TARGET_MODEL
	@if [ -r dist_hosts.txt ] ; then \
		echo "Adding $@ to job list"; \
		echo "$(CXX) $(filter-out -DOPENR_DEBUG,$(filter-out -Weffc++,$(CXXFLAGS))) -o $@ -c $< > $*.log 2>&1; retval=\$$?; cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); test \$$retval -eq 0;" >> $(BUILDDIR)/joblist.txt ; \
		touch $@; \
		echo "$@" >> $(BUILDDIR)/touched.txt ; \
	else \
		echo "Compiling $<... (reduced warnings, no OPENR_DEBUG)"; \
		$(CXX) $(filter-out -DOPENR_DEBUG,$(filter-out -Weffc++,$(CXXFLAGS))) -o $@ -c $< > $*.log 2>&1; \
		retval=$$?; \
		cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); \
		test $$retval -eq 0; \
	fi;

$(TEKKOTSU_ROOT)/SoundPlay/WAV.o $(TEKKOTSU_ROOT)/Vision/Vision.o: %.o: %$(SRCSUFFIX) $(FILTERSYSWARN) $(TEKKOTSU_ROOT)/TARGET_MODEL
	@if [ -r dist_hosts.txt ] ; then \
		echo "Adding $@ to job list"; \
		echo "$(CXX) $(filter-out -Weffc++,$(CXXFLAGS)) -o $@ -c $< > $*.log 2>&1; retval=\$$?; cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); test \$$retval -eq 0;" >> $(BUILDDIR)/joblist.txt ; \
		touch $@; \
		echo "$@" >> $(BUILDDIR)/touched.txt ; \
	else \
		echo "Compiling $<... (reduced warnings)"; \
		$(CXX) $(filter-out -Weffc++,$(CXXFLAGS)) -o $@ -c $< > $*.log 2>&1; \
		retval=$$?; \
		cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); \
		test $$retval -eq 0; \
	fi;

%.o: %$(SRCSUFFIX) $(FILTERSYSWARN) $(TEKKOTSU_ROOT)/TARGET_MODEL
	@if [ -r dist_hosts.txt ] ; then \
		echo "Adding $@ to job list"; \
		echo "$(CXX) $(CXXFLAGS) -o $@ -c $< > $*.log 2>&1; retval=\$$?; cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); test \$$retval -eq 0;" >> $(BUILDDIR)/joblist.txt ; \
		touch $@; \
		echo "$@" >> $(BUILDDIR)/touched.txt ; \
	else \
		echo "Compiling $<..."; \
		$(CXX) $(CXXFLAGS) -o $@ -c $< > $*.log 2>&1; \
		retval=$$?; \
		cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); \
		test $$retval -eq 0; \
	fi;

#This is for external libraries which we are including, such as libjpeg
%.a:
	$(MAKE) -C $(dir $@) $(notdir $@)

$(TEKKOTSU_ROOT)/TARGET_MODEL:
	$(error Could not find $(TEKKOTSU_ROOT)/TARGET_MODEL)

#BUILDS:   (framework object files, one per process - resides in framework)
$(BUILDS):
	@if [ -r dist_hosts.txt -a -r $(BUILDDIR)/joblist.txt ] ; then \
		echo "Distributing compiles..."; \
		../tools/pm.pl dist_hosts.txt $(BUILDDIR)/joblist.txt ; \
	fi
	@rm -f $(BUILDDIR)/joblist.txt; #this is so we don't rebuild multiple times
	@echo "Linking component object files..."
	@if [ $(words $(sort $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$($(basename $(notdir $@))_COMP)), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc")))))) -gt 20 ] ; then \
		echo "$@ <- [...]"; \
	else \
		echo "$@ <- $(sort $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$(filter-out %.a,$($(basename $(notdir $@))_COMP))), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc") ))))"; \
	fi;
	@$(LD) -i $(sort $(foreach comp,$(addprefix $(TEKKOTSU_ROOT)/,$(filter-out %.a,$($(basename $(notdir $@))_COMP))), $(if $(suffix $(comp)),$(comp),$(patsubst %.cc,%.o,$(shell find $(comp) -name "*.cc") )))) -o $@ ; \
		if [ $$? -ne 0 ] ; then exit 1; fi;


#PROC_BINS:    (executable binaries, uncompressed)
# we have to do a couple extra steps to cd into the builddir because
# mkbin doesn't support -o target in a different directory... drops an
# intermediate file in . and then complains (as of 1.1.3 anyway)
$(BUILDDIR)/%$(BINSUFFIX): $(USERLIBS)
	@if [ -r dist_hosts.txt -a -r $(BUILDDIR)/joblist.txt ] ; then \
		echo "Distributing compiles..."; \
		../tools/pm.pl dist_hosts.txt $(BUILDDIR)/joblist.txt ; \
	fi
	@rm -f $(BUILDDIR)/joblist.txt; #this is so we don't rebuild multiple times
	@echo "Creating executable binary..."
	@echo "$@ <- $($(basename $(notdir $@))_OBJS), $($(basename $(notdir $@))_OCF), $(LIBS) $(USERLIBS)"; #we'll need to do some filtering on USERLIBS below because now we're in builddir
	@$(LD) -i $($(basename $(notdir $@))_OBJS) -o $(BUILDDIR)/tmp.o
	@cp $($(basename $(notdir $@))_OCF) $(BUILDDIR)/tmp.ocf
	@cd $(BUILDDIR) ; \
	$(MKBIN) $(MKBINFLAGS) -o $(notdir $@) tmp.o -m tmp.ocf $(LIBS) $(filter /%,$(USERLIBS)) $(addprefix ../,$(filter-out /%,$(USERLIBS))) ; \
	if [ $$? -gt 0 ] ; then \
		echo "Build failed."; \
		exit 1; \
	fi; \
	$(STRIP) $(notdir $@) ; \
	rm tmp.o tmp.ocf;


#INSTALL_BINS:     (compressed executables, in proper location in image directory)
$(INSTALLDIR)/%$(BINSUFFIX): $(BUILDDIR)/%$(BINSUFFIX)
	@if [ \! -d "$(INSTALLDIR)" ] ; then \
		echo "I can't find $(INSTALLDIR).  You may need to run '$(TEKKOTSU_ROOT)/tools/makeuppercase -r ms'."; \
		exit 1; \
	fi;
	@echo "Compressing $< -> $@"
	@gzip -c $< > $@;

$(INSTALLDIR)/$(MMCOMBOBIN): $(INSTALLDIR)/$(MAINFORK) $(INSTALLDIR)/$(MOTOFORK)

$(INSTALLDIR)/$(MAINFORK): $(BUILDDIR)/$(MMCOMBOBIN) $(TEKKOTSU_ROOT)/tools/binstrswap/binstrswap
	@if [ \! -d "$(INSTALLDIR)" ] ; then \
		echo "I can't find $(INSTALLDIR).  You may need to run '$(TEKKOTSU_ROOT)/tools/makeuppercase -r ms'."; \
		exit 1; \
	fi;
	@echo "Compressing $< ~> $@"
	@$(TEKKOTSU_ROOT)/tools/binstrswap/binstrswap $< MMCombo MainObj | gzip -c > $@
#	@sed 's/MMCombo/MainObj/g;s/mmcombo/mainobj/g' $< | gzip -c > $@

$(INSTALLDIR)/$(MOTOFORK): $(BUILDDIR)/$(MMCOMBOBIN) $(TEKKOTSU_ROOT)/tools/binstrswap/binstrswap
	@if [ \! -d "$(INSTALLDIR)" ] ; then \
		echo "I can't find $(INSTALLDIR).  You may need to run '$(TEKKOTSU_ROOT)/tools/makeuppercase -r ms'."; \
		exit 1; \
	fi;
	@echo "Compressing $< ~> $@"
	@$(TEKKOTSU_ROOT)/tools/binstrswap/binstrswap $< MMCombo MotoObj | gzip -c > $@
#	@sed 's/MMCombo/MotoObj/g;s/mmcombo/motoobj/g' $< | gzip -c > $@

install: compile
	@echo "Installing files to memory stick at $(MEMSTICK_ROOT)"
	@$(TEKKOTSU_ROOT)/tools/cpymem --all --img $(MSIMGDIR) --tgt $(MEMSTICK_ROOT) --tools $(TEKKOTSU_ROOT)/tools

update: compile $(TEKKOTSU_ROOT)/tools/evenmodtime/evenmodtime
	@echo "Syncing $(MSIMGDIR) and $(MEMSTICK_ROOT)"
	@$(TEKKOTSU_ROOT)/tools/evenmodtime/evenmodtime `find $(MSIMGDIR)`
	@$(TEKKOTSU_ROOT)/tools/mntmem $(MEMSTICK_ROOT)
	@if [ $(STRICT_MEMSTICK_IMAGE) ] ; then \
		echo "Strict image copy is on." ; \
		rsync -rLtWCv --delete $(MSIMGDIR)/* $(BUILDDIR)/$(notdir $(MEMSTICK_ROOT))/* $(MEMSTICK_ROOT) ; \
	else \
		rsync -rLtWCv $(MSIMGDIR)/* $(BUILDDIR)/$(notdir $(MEMSTICK_ROOT))/* $(MEMSTICK_ROOT) ; \
	fi;
	@$(TEKKOTSU_ROOT)/tools/umntmem $(MEMSTICK_ROOT)

newstick:
	$(TEKKOTSU_ROOT)/tools/mntmem $(MEMSTICK_ROOT)
	rm -rf $(MEMSTICK_ROOT)/*
	cp -r $(OPENRSDK_ROOT)/OPEN_R/MS/WCONSOLE/nomemprot/* $(MEMSTICK_ROOT)
	$(TEKKOTSU_ROOT)/tools/umntmem $(MEMSTICK_ROOT)

$(FILTERSYSWARN):
	(cd $(TEKKOTSU_ROOT)/tools && $(MAKE));

cleanTemps:
	rm -f $(BUILDDIR)/joblist.txt $(BUILDDIR)/touched.txt

clean: cleanProj
	@printf "Cleaning all .o, .d, .log and ~ files corresponding to .cc files..."
	@rm -f $(OBJS) $(DEPENDS) $(SRCS:$(SRCSUFFIX)=.log) $(addsuffix ~,$(SRCS)) $(SRCS:$(SRCSUFFIX)=.h~)
	@printf "done.\n"
	rm -f $(foreach proc,$(PROCESS_OBJS),$(TEKKOTSU_ROOT)/$(proc)/$(proc)Stub.cc $(TEKKOTSU_ROOT)/$(proc)/$(proc)Stub.h $(TEKKOTSU_ROOT)/$(proc)/def.h $(TEKKOTSU_ROOT)/$(proc)/entry.h)
	rm -rf $(TEKKOTSU_ROOT)/$(BUILDDIR)
	cd $(TEKKOTSU_ROOT)/tools ; $(MAKE) clean
	$(foreach lib,$(USERLIBS),$(MAKE) -C $(dir $(lib)) clean)

cleanDeps:
	@printf "Cleaning all .d files corresponding to .cc files..."
	@rm -f $(DEPENDS)
	@printf "done.\n"

cleanProj:
	@printf "Cleaning all .o, .d, .log and ~ files corresponding to project .cc files..."
	@rm -f $(PROJ_SRCS:$(SRCSUFFIX)=.o)
	@rm -f $(PROJ_SRCS:$(SRCSUFFIX)=.d)
	@rm -f $(PROJ_SRCS:$(SRCSUFFIX)=.log)
	@rm -f $(addsuffix ~,$(PROJ_SRCS)) $(PROJ_SRCS:$(SRCSUFFIX)=.h~)
	@printf "done.\n"
	rm -rf $(BUILDDIR)
	rm -f $(INSTALL_BINS) $(INSTALLDIR)/$(MAINFORK) $(INSTALLDIR)/$(MOTOFORK)

dox doc docs:
	cd $(TEKKOTSU_ROOT) && $(MAKE) $@

cleandoc:
	docs/builddocs --clean



