all:

TEKKOTSU_ROOT?=/usr/local/Tekkotsu
MEMSTICK_ROOT?=/mnt/memstick
TEKKOTSU_TARGET_MODEL?=TGT_ERS210

# Find all of the source files: (outside of build directory)

BUILDDIR=build
$(shell mkdir -p $(BUILDDIR))
SRCSUFFIX=.cc
SRCS:=$(shell find . -name "*$(SRCSUFFIX)" \! -path "*/$(BUILDDIR)/*")
OBJS:=$(foreach file,$(SRCS),$(addsuffix .o,$(basename $(file))))
DEPENDS:=$(foreach file,$(SRCS),$(addsuffix .d,$(basename $(file))))

# We're assuming all of the source files we found are to be linked into MMCombo
# It should be pretty easy to add a new process here if you're inclined to do so.

# 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.
# This only really matters if you want to update a stick instead of recopying it
# (make update vs. make install)

# in case your is uppercase, you'll need to change as 'FILENAME_CASE = upper'
FILENAME_CASE ?= lower

ifeq ($(FILENAME_CASE),lower)

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

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

else

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

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

endif

#this will delete files from the memory stick that aren't also in your build image
#except files at the root level of the memory stick
#STRICT_MEMSTICK_IMAGE=true

#the automatic way of doing this part is messy, just add a rule for each processes: (skip lines!)
$(BUILDDIR)/$(word 1,$(PROCESSES))$(BINSUFFIX): $($(word 1,$(PROCESSES))_OBJS) $($(word 1,$(PROCESSES))_OCF)

$(BUILDDIR)/$(word 2,$(PROCESSES))$(BINSUFFIX): $($(word 2,$(PROCESSES))_OBJS) $($(word 2,$(PROCESSES))_OCF)

$(BUILDDIR)/$(word 3,$(PROCESSES))$(BINSUFFIX): $($(word 3,$(PROCESSES))_OBJS) $($(word 3,$(PROCESSES))_OCF)


######################
# You shouldn't have to change anything after this point, hopefully....
#####################

PROC_BINS:=$(addprefix $(BUILDDIR)/,$(addsuffix $(BINSUFFIX),$(PROCESSES)))
INSTALL_BINS:=$(addprefix $(INSTALLDIR)/,$(addsuffix $(BINSUFFIX),$(PROCESSES)))

PREFIX=/usr/local/OPEN_R_SDK
CXX=$(PREFIX)/bin/mipsel-linux-g++
LD=$(PREFIX)/bin/mipsel-linux-ld
STRIP=$(PREFIX)/bin/mipsel-linux-strip
MKBIN=$(PREFIX)/OPEN_R/bin/mkbin
STUBGEN=$(PREFIX)/OPEN_R/bin/stubgen2
FILTERSYSWARN=$(TEKKOTSU_ROOT)/tools/filtersyswarn/filtersyswarn
MKBINFLAGS=-p $(PREFIX)
LIBS=-lObjectComm -lOPENR -lInternet -lantMCOOP
CXXFLAGS= \
	-g -O2 \
	-Wall -W -Wshadow -Wlarger-than-8192 -Wpointer-arith -Wcast-qual \
	-Woverloaded-virtual -Weffc++ -Winline -Wdeprecated \
	-I. -I$(TEKKOTSU_ROOT) \
	-isystem $(PREFIX)/OPEN_R/include/MCOOP \
	-isystem $(PREFIX)/OPEN_R/include/R4000 -isystem $(PREFIX)/OPEN_R/include \
	-DPLATFORM_APERIOS -DDEBUG -DOPENR_DEBUG -DCONSOLE_VT100 -D$(TEKKOTSU_TARGET_MODEL) $(GLOBAL_MAP) \

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

.PHONY: all compile install clean Tekkotsu newstick update newstick $(INSTALLDIR)/$(MMCOMBOBIN) docs

#the touch at the end is because memsticks seem to round time to even seconds, which screws up updates.  Grr.
compile: Tekkotsu $(OBJS) $(PROC_BINS) $(INSTALL_BINS)
	@image="$(BUILDDIR)/$(notdir $(MEMSTICK_ROOT))" ; \
	if [ \! -d $$image ] ; then \
		if [ \! -d $(PREFIX)/OPEN_R/MS/WCONSOLE/nomemprot ] ; then \
			echo "Could not find OPEN-R system binaries" ; \
			exit 1 ; \
		fi ; \
		echo "Copying system files..." ; \
		cp -r $(PREFIX)/OPEN_R/MS/WCONSOLE/nomemprot $$image ; \
		chmod -R u+w $$image ; \
		$(CONVERTCASE) -r $$image ; \
		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

ifdef BUILD_TEKKOTSU
Tekkotsu:
	(cd $(TEKKOTSU_ROOT) && $(MAKE));
endif

#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
$(BUILDDIR)/%$(BINSUFFIX):
	@echo "Creating executable binary..."
	@echo "$@ <- $($(basename $(notdir $@))_OBJS), $($(basename $(notdir $@))_OCF)"
	@$(LD) -i $($(basename $(notdir $@))_OBJS) -o $(BUILDDIR)/tmp.o
	@cp $($(basename $(notdir $@))_OCF) $(BUILDDIR)/tmp.ocf
	@cd $(BUILDDIR) ; \
	$(MKBIN) $(MKBINFLAGS) -o $(notdir $@) tmp.o tmp.ocf $(LIBS) ; \
	$(STRIP) $(notdir $@) ; \
	rm tmp.o tmp.ocf;


#INSTALL_BINS:     (compressed executables, in proper location in image directory)
$(INSTALLDIR)/%$(BINSUFFIX): $(BUILDDIR)/%$(BINSUFFIX)
	@echo "Compressing $< -> $@"
	@gzip -c $< > $@;

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

$(INSTALLDIR)/$(MAINFORK): $(BUILDDIR)/$(MMCOMBOBIN)
	@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)
	@echo "Compressing $< ~> $@"
	@$(TEKKOTSU_ROOT)/tools/binstrswap/binstrswap $< MMCombo MotoObj | gzip -c > $@
#	@sed 's/MMCombo/MotoObj/g;s/mmcombo/motoobj/g' $< | gzip -c > $@

#OBJS:    (for user project files)

%.o: %$(SRCSUFFIX) %.d $(FILTERSYSWARN)
	@echo Compiling $< into $@...
	@$(CXX) $(CXXFLAGS) -o $@ -c $< > $*.log 2>&1; \
	retval=$$?; \
	cat $*.log | $(FILTERSYSWARN); \
	test $$retval -eq 0;

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

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

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

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

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

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPENDS)
endif


clean:
	rm -f $(OBJS) $(DEPENDS) ${SRCS:$(SRCSUFFIX)=.log} *~
	rm -rf build
	rm -f $(INSTALL_BINS) $(INSTALLDIR)/$(MAINFORK) $(INSTALLDIR)/$(MOTOFORK)
	if [ -n "$(BUILD_TEKKOTSU)" ] ; then (cd $(TEKKOTSU_ROOT) && $(MAKE) clean); fi;

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

