# folder containing sources is src if it's not already defined:
ifeq ($(origin SRC), undefined)
    SRC := src
endif

# to avoid some bugs if the PVSNESLIB_HOME is not well defined, we let a small check here
ifeq ($(findstring \,$(PVSNESLIB_HOME)),\)
$(error "PVSNESLIB_HOME environment variable is not defined correctly: the path must be in Unix style (on Windows operating system too!). For example, use /c/snesdev instead of c:\snesdev")
endif

ifeq ($(PVSNESLIB_DEBUG),1)
DEBUG = 1
$(info The debug mode is enabled)
else
DEBUG = 0
$(info The debug mode is NOT enabled, you can do it by executing "export PVSNESLIB_DEBUG=1")
endif

ifeq ($(HIROM),1)
HIROM = 1
$(info HiROM compilation is enabled)
ifeq ($(FASTROM),1)
LIBDIRSOBJS := $(PVSNESLIB_HOME)/pvsneslib/lib/HiROM_FastROM
else
LIBDIRSOBJS := $(PVSNESLIB_HOME)/pvsneslib/lib/HiROM_SlowROM
endif
else
HIROM = 0
$(info LoROM compilation is enabled)
ifeq ($(FASTROM),1)
LIBDIRSOBJS := $(PVSNESLIB_HOME)/pvsneslib/lib/LoROM_FastROM
else
LIBDIRSOBJS := $(PVSNESLIB_HOME)/pvsneslib/lib/LoROM_SlowROM
endif
endif

ifeq ($(FASTROM),1)
FASTROM = 1
$(info FastROM compilation is enabled)
else
FASTROM = 0
$(info SlowROM compilation is enabled)
endif

#---------------------------------------------------------------------------------
# on windows, linkfile can only manage path like E:\pvsneslib\lib\crt0_snes.obj
# this one doesn't work /e/pvsneslib/lib/crt0_snes.obj
#---------------------------------------------------------------------------------
ifeq ($(OS),Windows_NT)
DRIDIROBJW   := $(wordlist 1,1,$(subst /, ,${LIBDIRSOBJS}))
REPDIROBJSW  := $(subst /,\\,${LIBDIRSOBJS})
REPDIROBJSW1 := $$(echo "$(REPDIROBJSW)" | sed 's/^.\{3\}//')
LIBDIRSOBJSW := ${DRIDIROBJW}:\\${REPDIROBJSW1}
else
LIBDIRSOBJSW := ${LIBDIRSOBJS}
endif

#---------------------------------------------------------------------------------
# the compiler executables
#---------------------------------------------------------------------------------

export CC	:=	$(PVSNESLIB_HOME)/devkitsnes/bin/816-tcc
export AS	:=	$(PVSNESLIB_HOME)/devkitsnes/bin/wla-65816
export LD	:=	$(PVSNESLIB_HOME)/devkitsnes/bin/wlalink

export GFXCONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/gfx4snes
# gfx2snes is just here for old compatibity issues
export GFX2CONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/gfx2snes
export SMCONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/smconv
export BRCONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/snesbrr
export TXCONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/bin2txt
export SNTOOLS	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/snestools
export TMXCONV	:=	$(PVSNESLIB_HOME)/devkitsnes/tools/tmx2snes
export OPT		:=	$(PVSNESLIB_HOME)/devkitsnes/tools/816-opt
export CTF		:=	$(PVSNESLIB_HOME)/devkitsnes/tools/constify

#---------------------------------------------------------------------------------
# Add default flag for compiling
#---------------------------------------------------------------------------------
CFLAGS += -I$(PVSNESLIB_HOME)/pvsneslib/include -I$(PVSNESLIB_HOME)/devkitsnes/include -I$(CURDIR)


SFILES=
# if we found any .it file, we will need to build soundbank.asm first (generated by smconv tool)
# (strip function remove leading and trailing whitespaces to avoid issues...)
ifneq ($(strip $(AUDIOFILES)),)
	SFILES := $(SOUNDBANK).asm
endif

#---------------------------------------------------------------------------------
# Search all .asm and .c files until 3rd level in the tree
#---------------------------------------------------------------------------------
CFILES = $(wildcard *.c)
CFILES+= $(wildcard $(SRC)/*.c)
CFILES+= $(wildcard $(SRC)/*/*.c)
CFILES+= $(wildcard $(SRC)/*/*/*.c)

SFILES := $(SFILES) $(wildcard *.asm)
SFILES := $(SFILES) $(wildcard $(SRC)/*.asm)
SFILES := $(SFILES) $(wildcard $(SRC)/*/*.asm)
SFILES := $(SFILES) $(wildcard $(SRC)/*/*/*.asm)

DBGFILES = $(wildcard *.dbg)
DBGFILES+= $(wildcard $(SRC)/*.dbg)
DBGFILES+= $(wildcard $(SRC)/*/*.dbg)
DBGFILES+= $(wildcard $(SRC)/*/*/*.dbg)

export OFILES	:=	$(BINFILES:.bin=.obj) $(CFILES:.c=.obj) $(SFILES:.asm=.obj)

# The first rule available in makefile become the default one
# We want "all" as default rule, but we will declare it later !
.DEFAULT_GOAL := all

#---------------------------------------------------------------------------------
%.ps: %.c
	@echo Compiling to .ps ... $(notdir $<)
ifeq ($(HIROM),1)
ifeq ($(FASTROM),1)
	$(CC) $(CFLAGS) -Wall -c $< -H -F -o $@
else
	$(CC) $(CFLAGS) -Wall -c $< -H -o $@
endif
else
ifeq ($(FASTROM),1)
	$(CC) $(CFLAGS) -Wall -c $< -F -o $@
else
	$(CC) $(CFLAGS) -Wall -c $< -o $@
endif
endif

ifeq ($(DEBUG),1)
	cp $@ $@.01.dbg
endif

#---------------------------------------------------------------------------------
%.asm: %.ps
	@echo Assembling ... $(notdir $<)
	$(OPT) $< >$*.asp
ifeq ($(DEBUG),1)
	cp $*.asp $*.opt.02.dbg
endif
	@echo Moving constants ... $(notdir $<)
	$(CTF) $*.c $*.asp $@
ifeq ($(DEBUG),1)
	cp $*.asp $*.ctf.03.dbg
endif
	@rm $*.asp

%.obj: %.asm
	@echo Doing obj files ... $(fname)
	@echo "Building with -x flag: $(AS) -s -x -o $@ $<"
# -d switch disable WLA's ability to calculate A-B where A and B are labels.
# if you remove it, you will have some updates to do at least in crt0_snes.asm
	$(AS) -d -s -x -o $@ $<

#---------------------------------------------------------------------------------
%.sfc: $(OFILES)
	@echo Creating linkfile ...
	@echo [objects] > linkfile
	@for i in $(OFILES); do \
		echo $$i >> linkfile; \
	done

	@for i in $(shell ls $(LIBDIRSOBJSW)); do \
		echo $(LIBDIRSOBJSW)/$$i >> linkfile; \
	done

	@echo Linking ... $(notdir $@)
	@rm -f $(ROMNAME).sym
# -c should be removed ASAP ! It allow duplicate labels and definitions
	$(LD) -d -s -v -A -c -L ${LIBDIRSOBJS} linkfile $@

	@sed -i 's/://' $(ROMNAME).sym

	@echo
	@echo Build finished successfully !
	@echo

#---------------------------------------------------------------------------------
%.pic: %.bmp
	@echo convert bitmap ... $(notdir $<)
	$(GFXCONV) -s 8 -o 16 -u 16 -e 0 -p -m -t bmp -i $<

#---------------------------------------------------------------------------------
%.brr: %.wav
	@echo convert wav file ... $(notdir $<)
	$(BRCONV) -e $< $@

$(SOUNDBANK).asm : $(AUDIOFILES)
	@echo Compiling Soundbank ...
	$(SMCONV) $(SMCONVFLAGS) $^

.PHONY: cleanBuildRes cleanRom cleanGfx cleanAudio

cleanBuildRes: cleanDebug
	@echo clean build resources
	@rm -f $(OFILES) $(DBGFILES) linkfile

cleanRom:
	@echo clean rom
	@rm -f $(ROMNAME).sfc $(ROMNAME).sym

cleanGfx:
	@echo clean graphics data
	@rm -f *.pic *.map *.pal *.pc7 *.mp7 *.til *.m16 *.b16 *.o16 *.t16

cleanAudio:
	@echo clean audio data
	@rm -f $(SOUNDBANK).asm $(SOUNDBANK).h $(SOUNDBANK).bnk

cleanDebug:
ifeq ($(DEBUG),1)
	@echo clean debug files
	@find . -type f -name "*.dbg" -delete
endif
