# SPDX-License-Identifier: CC0-1.0
#
# SPDX-FileContributor: Antonio Niño Díaz, 2023

BLOCKSDS	?= /opt/blocksds/core
BLOCKSDSEXT	?= /opt/blocksds/external

# User config
# ===========

NAME		?= template

GAME_TITLE	?= Template
GAME_SUBTITLE	?= Built with BlocksDS
GAME_AUTHOR	?= blocksds.skylyrac.net
GAME_ICON	?= $(BLOCKSDS)/sys/icon.gif

# DLDI and internal SD slot of DSi
# --------------------------------

# Root folder of the SD image
SDROOT		?= sdroot
# Name of the generated image it "DSi-1.sd" for no$gba in DSi mode
SDIMAGE		?= image.bin

# Source code paths
# -----------------

# List of folders to combine into the root of NitroFS:
NITROFSDIR	?=

# Libraries
# ---------

ARM7ELF		?= $(BLOCKSDS)/sys/arm7/main_core/arm7_dswifi_maxmod.elf

# Tools
# -----

MAKE		:= make
RM		:= rm -rf
CP		:= cp

# Verbose flag
# ------------

ifeq ($(VERBOSE),1)
V		:=
else
V		:= @
endif

# Build artfacts
# --------------

ROM		:= $(NAME).nds

# Targets
# -------

.PHONY: all clean arm9 dldipatch sdimage teak

all: $(ROM)

clean:
	@echo "  CLEAN"
	$(V)$(MAKE) -f Makefile.arm9 clean --no-print-directory
	$(V)$(MAKE) -f Makefile.teak clean --no-print-directory
	$(V)$(RM) $(ROM) build $(SDIMAGE)
	$(V)$(RM) arm9/data/teak_tlf.bin

arm9: teak
	$(V)$(CP) build/teak.tlf arm9/data/teak_tlf.bin
	$(V)$(MAKE) -f Makefile.arm9 --no-print-directory

teak:
	$(V)$(MAKE) -f Makefile.teak --no-print-directory

ifneq ($(strip $(NITROFSDIR)),)
# Additional arguments for ndstool
NDSTOOL_ARGS	:= -d $(NITROFSDIR)

# Make the NDS ROM depend on the filesystem only if it is needed
$(ROM): $(NITROFSDIR)
endif

# Combine the title strings
ifeq ($(strip $(GAME_SUBTITLE)),)
    GAME_FULL_TITLE := $(GAME_TITLE);$(GAME_AUTHOR)
else
    GAME_FULL_TITLE := $(GAME_TITLE);$(GAME_SUBTITLE);$(GAME_AUTHOR)
endif

$(ROM): arm9
	@echo "  NDSTOOL $@"
	$(V)$(BLOCKSDS)/tools/ndstool/ndstool -c $@ \
		-7 $(ARM7ELF) -9 build/arm9.elf \
		-b $(GAME_ICON) "$(GAME_FULL_TITLE)" \
		$(NDSTOOL_ARGS)

sdimage:
	@echo "  MKFATIMG $(SDIMAGE) $(SDROOT)"
	$(V)$(BLOCKSDS)/tools/mkfatimg/mkfatimg -t $(SDROOT) $(SDIMAGE)

dldipatch: $(ROM)
	@echo "  DLDIPATCH $(ROM)"
	$(V)$(BLOCKSDS)/tools/dldipatch/dldipatch patch \
		$(BLOCKSDS)/sys/dldi_r4/r4tf.dldi $(ROM)
