#
# Makefile.cfg for SRB2Wii native using libogc
#

# Check if DEVKITPPC is set in the environment. If so, continue with compilation.
.SUFFIXES:

ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif

# Set compiler flags

SRB2NAME?=srb2wii
EXENAME?=$(SRB2NAME).elf
DBGNAME?=$(SRB2NAME).elf.debug
DOLNAME?=$(SRB2NAME).dol

ICONPNG?=sdl/SRB2WII/icon.png
METAXML?=sdl/SRB2WII/meta.xml

APPDIR=apps/$(SRB2NAME)
ZIPNAME=$(SRB2NAME).zip

ELF2DOL=$(DEVKITPPC)/bin/elf2dol
WIILOADEXE=$(DEVKITPPC)/bin/wiiload
ZIP=zip -r -9
WGET=wget -P srb2wii -c -nc
SED=sed
XARGS=xargs
SHXARGS:=$(XARGS)
SHSED:=$(SED)
FOR=for

ifndef ECHO
	ELF2DOL:=@$(ELF2DOL)
	WIILOADEXE:=@$(WIILOADEXE)
	ZIP:=@$(ZIP)
	SED:=@$(SED)
	XARGS:=@$(XARGS)
	FOR:=@$(FOR)
endif

# Disable same warning flags
WFLAGS+=-Wno-shadow -Wno-char-subscripts -Wno-old-style-definition -Wno-unsuffixed-float-constants

# newlib has no support for networking
NONET=1

# use pkgconfig for PKG
PNG_PKGCONFIG=libpng

# use absolute paths because changing PATH variable breaks distcc
PREFIX := $(DEVKITPPC)/bin/$(PREFIX)

# FIXME: DevkitPPC and ready-compiled SDL Wii require these things to be in a silly order
# libogc/DevkitPro required stuff
LIBOGC := $(DEVKITPRO)/libogc
LIBOGC_INC := $(LIBOGC)/include
LIBOGC_LIB := $(LIBOGC)/lib

PORTLIBS := $(DEVKITPRO)/portlibs/ppc
PORTLIBS_INC := $(PORTLIBS)/include
PORTLIBS_LIB := $(PORTLIBS)/lib

SDL_CPPFLAGS := -I$(LIBOGC_INC)/SDL
SDL_LIB := $(DEVKITPRO)/libogc/lib/wii
INCLUDE := -I$(LIBOGC_INC) $(SDL_CPPFLAGS) -I$(PORTLIBS_INC)

PKG_CONFIG_PATH := $(PORTLIBS)/lib/pkgconfig
PKG_BROKEN_SWTICH := --static --define-variable=DEVKITPRO=$(DEVKITPRO)
PNG_PKGCONFIG := $(PKG_CONFIG_PATH)/libpng.pc $(PKG_BROKEN_SWTICH)
ZLIB_PKGCONFIG := $(PKG_CONFIG_PATH)/zlib.pc $(PKG_BROKEN_SWTICH)

ZLIB_CFLAGS?=$(shell $(PKG_CONFIG) $(ZLIB_PKGCONFIG) --cflags)
ZLIB_LDFLAGS?=$(shell $(PKG_CONFIG) $(ZLIB_PKGCONFIG) --libs)

ifdef RDB
	LIBS+=-ldb
	OPTS+=-DREMOTE_DEBUGGING=$(RDB)
endif

LIBS+= -L$(SDL_LIB)
ifndef NOMIXER
	LD=$(CXX)
	LIBS+=-lSDL_mixer -lvorbisidec -lsmpeg
endif
LIBS+=-lSDL

LIBS+=$(ZLIB_LDFLAGS) -lfat -lwiiuse -lbte -logc -lm -lwiikeyboard -L$(LIBOGC_LIB)

MACHDEP = -DGEKKO -mrvl -mcpu=750 -meabi -mhard-float
OPTS+=-DWII -D_WII -DUNIXCOMMON
CFLAGS+=-D__BIG_ENDIAN__ -g -O3 -fsigned-char $(MACHDEP) $(INCLUDE)
CXXFLAGS+=$(CFLAGS)
LDFLAGS+=-g $(MACHDEP) -Wl,-Map,$(notdir $@).map

SDL_CONFIG=/bin/true
SDL_CFLAGS=
SDL_LDFLAGS=

$(BIN)/$(DOLNAME): $(BIN)/$(EXENAME)
	@echo Linking $(DOLNAME)...
	$(ELF2DOL) $(BIN)/$(EXENAME) $(BIN)/$(DOLNAME)
	@echo Creating /apps/$(SRB2NAME)...
	$(MKDIR) $(APPDIR)
	$(CP) $(BIN)/$(DOLNAME) $(APPDIR)/boot.dol
	$(CP) $(ICONPNG) $(APPDIR)
	$(CP) $(METAXML) $(APPDIR)
ifdef WITHDATA
	$(MKDIR) srb2wii
	$(FOR) datafile in $(shell echo $(D_FILES) | $(SHSED) -e 's/\.srb/\.wad/' -e 's/music.dta//' | $(SHXARGS) -n 1 basename); do \
		$(WGET) http://alam.srb2.org/SRB2/2.0.6-Final/Resources/$$datafile; \
	done
	# downsampled music.dta specially for SRB2Wii
	$(WGET) http://repos.srb2.org/srb2ports/music.dta
	$(ZIP) $(BIN)/$(ZIPNAME) $(APPDIR) srb2wii
else
	$(ZIP) $(BIN)/$(ZIPNAME) $(APPDIR)
endif
	$(REMOVE) -r $(APPDIR)

run: $(BIN)/$(EXENAME)
	$(WIILOADEXE) $(BIN)/$(DBGNAME)
