#
# Wii Makefile
#

.PHONY: package

ifeq ($(strip $(DEVKITPRO)),)
$(error "DEVKITPRO must be set in your environment.")
endif

ifeq ($(strip $(DEVKITPPC)),)
$(error "DEVKITPPC must be set in your environment.")
endif

#
# Wii target rules
#

include $(DEVKITPPC)/wii_rules

ELF2DOL := elf2dol
STRIP   := /bin/true

#
# Override library paths
#

#
# If building with SDLWii, these need to be set manually--no sdl-config.
# libSDL has to be linked at a very specific place in the library list,
# but SDL_LDFLAGS gets appended to the end, so set it to something useless.
#
SDL_CFLAGS     = -isystem $(DEVKITPRO)/libogc/include/SDL
SDL_LDFLAGS    = $(SDL_CFLAGS)

#
# As of devkitPPC r34 $(PORTLIBS) has multiple paths for some reason...
#
PORTLIBS_INCLUDES := $(foreach dir, $(PORTLIBS), -isystem $(dir)/include)
PORTLIBS_LIBS     := $(foreach dir, $(PORTLIBS), -L$(dir)/lib)

EXTRA_INCLUDES := -isystem $(LIBOGC_INC) ${PORTLIBS_INCLUDES}
EXTRA_LIBS     := -L$(LIBOGC_LIB) ${PORTLIBS_LIBS} \
                 $(if $(BUILD_SDL), -lSDL,) \
                 -lfat -lasnd -lwiikeyboard -lwiiuse -lbte -logc -lstdc++ -lm

ARCH_CFLAGS   += ${EXTRA_INCLUDES} $(MACHDEP)
ARCH_CXXFLAGS += ${ARCH_CFLAGS}
ARCH_LDFLAGS  += ${EXTRA_LIBS} $(MACHDEP)

# Boot MegaZeux by default if the editor is enabled; otherwise, boot MZXRun.
package: mzx
ifneq ($(BUILD_EDITOR),)
	${ELF2DOL} ${mzx} boot.dol
	${ELF2DOL} ${mzxrun} mzxrun.dol
else
	${ELF2DOL} ${mzxrun} boot.dol
endif

clean:
	@rm -f boot.dol mzxrun.dol arch/wii/*.d arch/wii/*.o

#
# Vile hack, remove me ASAP
#
arch/wii/%.o: arch/wii/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

#
# Need to nest Wii binaries in a subdir
#
build := build/${SUBPLATFORM}/apps/megazeux
build: package ${build}
ifeq ($(BUILD_SDL),)
	${CP} arch/wii/pad.config ${build}
else
	${CP} arch/wii/pad.config.sdl ${build}/pad.config
endif
	${CP} arch/wii/icon.png ${build}
	${CP} boot.dol ${build}
ifneq ($(BUILD_EDITOR),)
	${CP} mzxrun.dol ${build}
endif
	@sed "s/%VERSION%/${VERSION}/g;s/%DATE%/`date -u +%Y%m%d%H%M`/g" \
	    arch/wii/meta.xml > ${build}/meta.xml
	${RM} ${build}/${mzxrun} ${build}/${mzxrun}.debug
	${RM} ${build}/${mzx} ${build}/${mzx}.debug

include arch/zip.inc
