#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif

include $(DEVKITPPC)/wii_rules

# Directories.
OBJ_DIR			:= obj
LIB_DIR			:= lib
BIN_DIR			:= .
SRC_DIR			:= src
INCLUDE_DIR		:= include

# Tools.
PIPE_TO_SED := 2>&1 | sed "s/:\([0-9]*\):/\(\1\) :/"

SYMLINKS=src/machdep src/osdep src/gfxdep src/joydep src/guidep src/sounddep src/threaddep


# Source files.
SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/caps/*.c $(SRC_DIR)/dms/*.c $(SRC_DIR)/keymap/*.c) \
	$(SRC_DIR)/gfxdep/rawkeys.c  \
	$(SRC_DIR)/gfxdep/sdlgfx.c \
	$(SRC_DIR)/gfxdep/sdlkeys.c \
	$(SRC_DIR)/guidep/gui-sdl.c \
	$(SRC_DIR)/guidep/menu.c \
	$(SRC_DIR)/guidep/VirtualKeyboard.c \
	$(SRC_DIR)/joydep/joystick.c \
	$(SRC_DIR)/machdep/support.c \
	$(SRC_DIR)/osdep/main.c \
	$(SRC_DIR)/osdep/memory.c\
	$(SRC_DIR)/osdep/fsdb_wii.c\
	$(SRC_DIR)/sounddep/sound.c \
	$(SRC_DIR)/threaddep/thread.c
	
SRCS := $(filter-out \
	src/akiko.c \
	src/ar.c \
	src/blitops.c \
	src/blkdev-libscg.c \
	src/blkdev.c \
	src/bsdsocket-posix-new.c \
	src/build68k.c \
	src/catweasel.c \
	src/cdrom.c \
	src/compemu.c \
	src/compemu_fpp.c \
	src/compemu_raw_x86.c \
	src/compemu_support.c \
	src/compstbl.c \
	src/debug.c \
	src/enforcer.c \
	src/fdi2raw.c \
	src/filesys_bootrom.c \
	src/fsdb_unix.c \
	src/genblitter.c \
	src/gencomp.c \
	src/gencpu.c \
	src/gengenblitter.c \
	src/genlinetoscr.c \
	src/linetoscr.c \
	src/make_hdf.c \
	src/readdisk.c \
	src/scsiemul.c \
	src/svgancui.c \
	src/tui.c \
	, $(SRCS))

genlinetoscr_args="-b"

# Library object files.
OBJS	:= $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS:.c=.o))

# Test source files.
# It can be useful to switch this variable around to select individual tests which are problematic.
TEST_SRCS	:= $(TEST_SRC_DIR)/showfont.c


# What's a full build?
all: uae.dol

src/tools/genlinetoscr.exe:
	cd src/tools/ && make -f Makefile.wii

# How to delete the intermediate files.
clean:
	@echo Cleaning $(OBJ_DIR)
	@rm -rf $(OBJS) $(OBJ_DIR) src/machdep src/target.h src/md-fpp.h src/sysconfig.h $(SYMLINKS)
	@rm -rf dist
	@rm -f src/blit.h src/blitfunc.c src/blitfunc.h src/blittable.c src/linetoscr.c
	@rm -f uae.dol uae.elf
	cd src/tools/ && make -f Makefile.wii clean

$(OBJ_DIR):
	install -d $@

src/sysconfig.h: src/sysconfig.h.wii
	cp $< $@

src/machdep:
	rm -f $@
	mkdir $@ && cd src/md-ppc-gcc && find . -maxdepth 1 -type f -exec ln '{}' ../machdep/'{}' ';' 

src/osdep:
	rm -f $@
	mkdir $@ && cd src/od-wii && find . -maxdepth 1 -type f -exec ln '{}' ../osdep/'{}' ';' 

src/gfxdep:
	rm -f $@
	mkdir $@ && cd src/gfx-sdl && find . -maxdepth 1 -type f -exec ln '{}' ../gfxdep/'{}' ';' 

src/joydep:
	rm -f $@
	mkdir $@ && cd src/jd-sdl && find . -maxdepth 1 -type f -exec ln '{}' ../joydep/'{}' ';'

src/guidep:
	rm -f $@
	mkdir $@ && cd src/gui-sdl && find . -maxdepth 1 -type f -exec ln '{}' ../guidep/'{}' ';'
	
src/sounddep:
	rm -f $@
	mkdir $@ && cd src/sd-sdl && find . -maxdepth 1 -type f -exec ln '{}' ../sounddep/'{}' ';'

src/threaddep:
	rm -f $@
	mkdir $@ && cd src/td-sdl && find . -maxdepth 1 -type f -exec ln '{}' ../threaddep/'{}' ';'

src/target.h:
	rm -f $@
	cd src && ln -s targets/t-wii.h target.h

src/md-fpp.h:
	rm -f $@
	cd src && ln -s include/fpp-ieee.h md-fpp.h

src/blit.h:	src/tools/genblitter.exe
		src/tools/genblitter.exe i >$@

src/blitfunc.c: src/tools/genblitter.exe src/blitfunc.h
		src/tools/genblitter.exe f >$@

src/blitfunc.h: src/tools/genblitter.exe
		src/tools/genblitter.exe h >$@

src/blittable.c:  src/tools/genblitter.exe src/blitfunc.h
		src/tools/genblitter.exe t >$@

src/linetoscr.c:    src/tools/genlinetoscr.exe
		src/tools/genlinetoscr.exe $(genlinetoscr_args) >$@

%.h: %.h.wii
	cp $< $@

%.c: %.c.wii
	cp $< $@


# How to build a DOL.
uae.dol: uae.elf
	@echo Creating DOL $@
	@-mkdir -p $(dir $@)
	#powerpc-eabi-objcopy -O binary $< $@
	elf2dol $< $@
	@echo ----

# Compilation flags.
COMMON_FLAGS	:= -g -O3 $(MACHDEP) -Wall -W -Wno-unused -Wno-format -Wno-sign-compare 
INCLUDES	:=  -Isrc/md-generic/ -Isrc/include -Isrc -I$(DEVKITPRO)/libogc/include -I$(DEVKITPRO)/libogc/include/SDL -I$(PORTLIBS)/include
DEFINES		:=  -DOS_WITHOUT_MEMORY_MANAGEMENT -DSAVESTATE -DUSE_SDL -DSUPPORT_THREADS -DCPUEMU_0 -DCPUEMU_5 -DCPUEMU_6 \
		    -DFPUEMU -DAGA -DAUTOCONFIG -DFILESYS \
		    -DTD_START_HEIGHT=16 -DDRIVESOUND -DREGPARAM=
CFLAGS		:= $(COMMON_FLAGS) $(INCLUDES) $(DEFINES)

#unused defines: -DCAPS (to enable ipf file) -DFDI2RAW (to enable DFI file)
#unused flags: -ftree-vectorize -ffast-math

# Test link flags.
LDFLAGS			:= $(COMMON_FLAGS) -L$(LIB_DIR) -L$(PORTLIBS)/lib -L$(DEVKITPRO)/libogc/lib -L$(DEVKITPRO)/libogc/lib/wii -lz -lSDL_ttf -lSDL -lSDL_image -lpng -ljpeg -lfreetype -lfat -lwiiuse -lbte -logc -lm -lwiikeyboard -ltinysmb


dist:uae.dol
	install -d $@/apps/uae
	install -d $@/apps/uae/docs
	install -d $@/apps/uae/images/
	install -d $@/uae
	install -d $@/uae/roms
	install -d $@/uae/floppies
	install -d $@/uae/HD
	install -d $@/uae/harddir
	install -d $@/uae/saves
	install -d $@/uae/wave
	echo "dummy" > $@/uae/roms/put-your-kick-dot-rom-here
	echo "dummy" > $@/uae/floppies/dummy
	echo "dummy" > $@/uae/HD/dummy
	echo "dummy" > $@/uae/harddir/dummy
	echo "dummy" > $@/uae/saves/dummy
	cp $< $@/apps/uae/boot.dol
	cp meta.xml $@/apps/uae/
	cp icon.png $@/apps/uae/
	cp font.ttf $@/apps/uae/
	cp font_small.ttf $@/apps/uae/
	cp README.Cloanto-Amiga_Forever $@/apps/uae/docs/
	cp images/kb_amiga.png $@/apps/uae/images/
	cp images/kb_amiga_small.png $@/apps/uae/images/
	cp images/window_bg.png $@/apps/uae/images/
	cp images/window_bg_small.png $@/apps/uae/images/
	cp sound/menu_navigation_BE.raw $@/uae/wave
	cp sound/select_BE.raw $@/uae/wave
	cp sound/unselect_BE.raw $@/uae/wave

	cp uaerc.wii $@/uae/uaerc
	cp uaerc.smb $@/uae/
	cp docs/configuration.txt $@/apps/uae/docs/
	cp docs/Joystick_mapping.txt $@/apps/uae/docs/
	cp src/resource/*.wav $@/uae/wave
	cd $@ && tar -czf ../uae-wii-vx-bin.tar.gz *
	
distsource:
	cd .. && cp -r uae-wii uae-wii-v
	cd ../uae-wii-v && find . -name ".svn" | xargs rm -rf
	cd .. && tar -czf uae-wii-v.tar.gz uae-wii-v
	cd .. && rm -fr uae-wii-v


#---------------------------------------------------------------------------------
run:
	wiiload uae.dol


# How to link an ELF.
uae.elf: src/tools/genlinetoscr.exe src/blit.h src/blitfunc.c src/blitfunc.h src/blittable.c src/linetoscr.c $(SYMLINKS) src/target.h src/md-fpp.h src/sysconfig.h $(OBJS)
	@echo Linking $@
	@-mkdir -p $(dir $@)
	$(CC) -o $@ $(OBJS) $(LDFLAGS)
	#keep elf for debugging
	#cp $@ /tmp/elf

# How to compile C file
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@echo Compiling $<
	@-mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) -c $< -o $@ $(PIPE_TO_SED)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	@echo Compiling $<
	@-mkdir -p $(dir $@)
	@$(CXX) $(CFLAGS) -c $< -o $@ $(PIPE_TO_SED)
