#
# Makefile.cfg for SRB2/PSP
#

#
#hmmm, the PSP
#

	PSPSDK=$(shell psp-config -p)
	PSPDEV=$(shell psp-config -d)
	PSPPREFIX=$(shell psp-config -P)
	STRIP=psp-strip
	MKSFO?=mksfoex -d MEMSIZE=1
	#MKSFO=mksfo
	PACK_PBP=pack-pbp
	FIXUP=psp-fixup-imports
	HOSTCC:=$(CC)
	CC=$(PSPDEV)/bin/psp-gcc
	OBJCOPY=psp-objcopy
	OBJDUMP=psp-objdump
ifdef FIXEDPRX
	PRXGEN=psp-prxgen
else
	PRXGEN=$(OBJCOPY)
endif
ifndef PRXSIGN
	SIGNER:=$(PSPDEV)/bin/$(OBJCOPY)
endif

ifndef ECHO
	MKSFO:=@$(MKSFO)
	PACK_PBP:=@$(PACK_PBP)
	FIXUP:=@$(FIXUP)
	PRXGEN:=@$(PRXGEN)
endif

	PSP_EBOOT_TITLE=SRB2-PSP vME
	PSP_EBOOT_SFO=$(BIN)/PARAM.SFO
	PSP_EBOOT_ICON=sdl/SRB2PSP/ICON0.png
	PSP_EBOOT_ICON1=NULL
	PSP_EBOOT_UNKPNG=NULL
	PSP_EBOOT_PIC1=sdl/SRB2PSP/PIC1.png
	PSP_EBOOT_SND0=NULL
	PSP_EBOOT_PSAR=NULL

	SIGNER?=sdl/SRB2PSP/psp-prxsign/psp-prxsign

	SDL=1
	PREFIX=psp
	NONX86=1
	#NOHW=1
	NOHS=1
	NOMD5=1
	NONET=1       #No TCPIP code
	NOPNG=1       #No Screenshot

	OPTS=-I$(PSPPREFIX)/include -I$(PSPSDK)/include
	OPTS+=-DUNIXCOMMON -DFORCESDLMAIN -G0
	WFLAGS+=-Wno-undef
	WFLAGS+=-O1
	LIBS=-lm
	SDL_CONFIG?=$(PSPPREFIX)/bin/sdl-config
	#SDL_CFLAGS?=-I$(PSPDEV)/psp/include/SDL
	#SDL_LDFLAGS?=-lSDLmain -lSDL -lglut -lGLU -lGL -lpspgu -lpspaudiolib -lpspaudio -lpsphprm -lpspvfpu -lpsprtc
ifndef NOMIXER
	LIBS:=-liberty -lvorbisfile -lvorbis -logg -lSDL $(LIBS)
endif
ifndef NOHW
	OPTS+=-DSTATIC_OPENGL -DMINI_GL_COMPATIBILITY
	LIBS+=-lGLU -lGL -lm
endif
	#PSPSDK_LIBS=-L$(PSPSDK)/lib -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk
	#LIBS+=$(PSPSDK_LIBS) -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel
ifdef FIXEDPRX
	LDFLAGS := -specs=$(PSPSDK)/lib/prxspecs -Wl,-q,-T$(PSPSDK)/lib/linkfile.prx $(LDFLAGS)
	LIBS+=$(PSPSDK)/lib/prxexports.o
endif

ifeq ($(PSP_FW_VERSION),)
PSP_FW_VERSION=150
endif

	CPPFLAGS:=-D_PSP_FW_VERSION=$(PSP_FW_VERSION) $(CPPFLAGS)


	# name of the exefile
	EXENAME?=SRB2PSP.elf
	PRXNAME?=SRB2PSP.prx
	DBGNAME?=SRB2PSP.debug

post-build: $(BIN)/EBOOT.PBP

kxploit: $(BIN)/$(EXENAME) $(PSP_EBOOT_SFO)
	-$(MKDIR) "$(BIN)/kxploit/srb2"
	@echo emitting kxploit/srb2/
	$(STRIP) $(BIN)/$(EXENAME) -o $(BIN)/kxploit/srb2/EBOOT.PBP
	@echo emitting kxploit/srb2%
	-$(MKDIR) "$(BIN)/kxploit/srb2%/"
	$(PACK_PBP) "$(BIN)/kxploit/srb2%/EBOOT.PBP" $(PSP_EBOOT_SFO) $(PSP_EBOOT_ICON)  \
		$(PSP_EBOOT_ICON1) $(PSP_EBOOT_UNKPNG) $(PSP_EBOOT_PIC1)  \
		$(PSP_EBOOT_SND0) NULL $(PSP_EBOOT_PSAR)

sdl/SRB2PSP/psp-prxsign/psp-prxsign:
	-$(MAKE) -C sdl/SRB2PSP/psp-prxsign CFLAGS=-pipe CC="$(HOSTCC)"

fix-up: $(BIN)/$(EXENAME)
	@echo Running psp-fixup-imports on $(EXENAME)
	$(FIXUP) $(BIN)/$(EXENAME)

$(BIN)/$(PRXNAME): $(BIN)/$(EXENAME) fix-up
	@echo Building $(PRXNAME) out of $(EXENAME)
	$(PRXGEN) $(BIN)/$(EXENAME) $@

$(BIN)/EBOOT.PBP: $(BIN)/$(PRXNAME) $(SIGNER) $(PSP_EBOOT_SFO)
	@echo Signing and running pack-pbp to make PBP
	$(SIGNER) $(BIN)/$(PRXNAME) $(BIN)/$(PRXNAME).sign
	$(PACK_PBP) $@ $(PSP_EBOOT_SFO) $(PSP_EBOOT_ICON)  \
		$(PSP_EBOOT_ICON1) $(PSP_EBOOT_UNKPNG) $(PSP_EBOOT_PIC1)  \
		$(PSP_EBOOT_SND0)  $(BIN)/$(PRXNAME).sign $(PSP_EBOOT_PSAR)
	$(REMOVE) $(BIN)/$(PRXNAME).sign

$(PSP_EBOOT_SFO):
	-$(MKDIR) $(BIN)
	$(MKSFO) '$(PSP_EBOOT_TITLE)' $@

#include $(PSPSDK)/lib/build.mak
