#
# Makefile.cfg for SRB2 for the PlayStation 3 using PSL1GHT
#

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

ifeq ($(strip $(PS3DEV)),)
$(error "Please set PS3DEV in your environment. export PS3DEV=<path to>ps3dev-toolchain")
endif

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

# Set compiler flags

# Disable same warning flags
WFLAGS+=-Wno-shadow -Wno-char-subscripts -Wno-format

ifdef JAILBREAK
EXENAME?=SRB2PS3-jb.elf
PKGNAME?=SRB2PS3-jb.pkg
else
EXENAME?=SRB2PS3.elf
PKGNAME?=SRB2PS3.pkg
endif
DGBNAME?=$(EXENAME).debug

SRB2PS3DIR=sdl12/SRB2PS3
ICON0?=$(SRB2PS3DIR)/ICON0.png
SFOXML?=sfo.xml
SRB2TTF?=sdl12/srb2.ttf

TITLE=Sonic Robo Blast 2 v2.0.6
APPID=SRB2-PS3
CONTENTID=UP0001-$(APPID)_00-0000000000000000

FSELF=$(PS3DEV)/bin/fself.py
MAKE_SELF_NPDRM=$(PS3DEV)/ps3publictools/make_self_npdrm
FINALIZE=$(PS3DEV)/ps3publictools/package_finalize
SFO=$(PS3DEV)/bin/sfo.py
PKG=$(PS3DEV)/bin/pkg.py
PS3LOADEXE=$(PS3DEV)/ps3tools/ps3load
SED=sed
MV=mv
XARGS=xargs
FOR=for
SHXARGS:=$(XARGS)
SHSED:=$(SED)
SPRXLINKER=$(PS3DEV)/bin/sprxlinker

ifdef JAILBREAK
PKGDIR=$(BIN)/pkg-jb
else
PKGDIR=$(BIN)/pkg
endif
USRDIR=$(PKGDIR)/USRDIR
ETCDIR=$(USRDIR)/etc
WGET=wget -P $(ETCDIR) -c -nc

ifndef ECHO
	FSELF:=@$(FSELF)
	MAKE_SELF_NPDRM:=@$(MAKE_SELF_NPDRM)
	FINALIZE:=@$(FINALIZE)
	SFO:=@$(SFO)
	PKG:=@$(PKG)
	PS3LOADEXE:=@$(PS3LOADEXE)
	SED:=@$(SED)
	MV:=@$(MV)
	SPRXLINKER:=@$(SPRXLINKER)
	XARGS:=@$(XARGS)
	FOR:=@(FOR)
endif

# SRB2PS3 needs SDL_ttf to display any console text
SDL_TTF=1

# newlib has no support for networking
#NONET=1

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

# PS3DEV toolchain libdir and includedir
PS3DEV_INC := $(PS3DEV)/ppu/include
PS3DEV_LIB := $(PS3DEV)/ppu/lib

# PSL1GHT libdir and includedir
PSL1GHT_INC := $(PSL1GHT)/ppu/include
PSL1GHT_LIB := $(PSL1GHT)/ppu/lib

PS3PORTS := $(PS3DEV)/portlibs
PS3PORTS_BIN := $(PS3PORTS)/ppu/bin
PS3PORTS_INC := $(PS3PORTS)/ppu/include

PNG_CONFIG := $(PS3PORTS_BIN)/libpng-config
# static compilation
PNG_STATIC=1

SDL_CONFIG := $(PS3PORTS_BIN)/sdl-config

INCLUDE := -I$(PSL1GHT_INC) -I$(PS3DEV_INC) -I$(PS3PORTS_INC)

OPTS+=-D_PS3 -DUNIXCOMMON
CFLAGS+= -g $(INCLUDE) -L$(PSL1GHT_LIB) -L$(PS3DEV_LIB) -L$(PS3DEV)/lib
CXXFLAGS+=$(CFLAGS)
LDFLAGS+= -B$(PSL1GHT_LIB) -B$(PS3DEV_LIB) -B$(PS3DEV)/lib
LIBS+=-lrsx
ifndef NONET
LIBS+=-lnet -lsysmodule
endif

$(BIN)/$(PKGNAME): $(OBJS) $(BIN)/$(EXENAME)
	@echo Linking $(PKGNAME)...
	-$(MKDIR) $(ETCDIR)
	$(CP) $(ICON0) $(PKGDIR)
	$(CP) $(SRB2TTF) $(ETCDIR)
ifdef WITHDATA
	$(FOR) datafile in $(shell echo $(D_FILES) | $(SHSED) 's/\.srb/\.wad/' | $(SHXARGS) -n 1 basename); do \
		$(WGET) http://alam.srb2.org/SRB2/2.0.6-Final/Resources/$$datafile; \
	done
endif
	$(SPRXLINKER) $(BIN)/$(EXENAME)
ifdef JAILBREAK
	$(SED) 's/@@PS3_SYSTEM_VER@@/3.41/' $(SRB2PS3DIR)/$(SFOXML) > $(BIN)/$(SFOXML)
	$(FSELF) -n $(BIN)/$(EXENAME) $(USRDIR)/EBOOT.BIN
else
	$(SED) 's/@@PS3_SYSTEM_VER@@/3.55/' $(SRB2PS3DIR)/$(SFOXML) > $(BIN)/$(SFOXML)
	$(MAKE_SELF_NPDRM) $(BIN)/$(EXENAME) $(USRDIR)/EBOOT.BIN $(CONTENTID)
endif
	$(SFO) --title "$(TITLE)" --appid "$(APPID)" -f $(BIN)/$(SFOXML) $(PKGDIR)/PARAM.SFO
	$(PKG) --contentid $(CONTENTID) $(PKGDIR)/ $(BIN)/$(PKGNAME)
ifndef JAILBREAK
	$(FINALIZE) $(BIN)/$(PKGNAME)
endif

run: $(BIN)/$(EXENAME)
	$(PS3LOADEXE) $(USRDIR)/EBOOT.BIN
