@ECHO off
:console
ECHO                                    ------------------------------------------------
ECHO                                    ====== Create SATURN GAME NSP by Markus95 ======
ECHO                                    ------------------------------------------------
echo                                          ______________________________________________
echo                                         /         _______________________           /   \
echo                                        /         /______________________/          /     !
echo                                       /         _______________________           /      !
echo                                      /         /                       /         /       ! 
echo                                     /         /                       /         /        ; 
echo                                    /         /         ___________   /         /         / 
echo                                   /         /                       /         /         / 
echo                                  /          \_____________________           /         / 
echo                                 /        __        ________         __      /         / 
echo                                /       (__ )      /_______/       (__ )    /         /
echo                               /___________________________________________/         /   
echo                              ;                                              ;      /
echo                             !                      SEGA                      ;    /
echo                             !                ____        ____                 !  /
echo                             !               !____!      !____!                ! /  
echo                             !                                                 ;/ 
echo                              !_______________________________________________;/                                              
echo.
ECHO                             ---------------------------------------------------------
ECHO                             ======        1.  Create a Saturn Game NSP         ======
echo                             ======        2.  Convert .ISO to .BIN             ======
echo                             ======        3.  Compatibilty games list          ======
ECHO                             ---------------------------------------------------------
ECHO.

@set /p bs=

@if /i "%bs%"=="1" GOTO saturn
@if /i "%bs%"=="2" GOTO convert
@if /i "%bs%"=="3" GOTO list

:list

@start https://docs.google.com/spreadsheets/d/1X4iLL8J5sPMlxr_XbfZ-3f21IF9ni-otn2wwl-4hzsk/edit#gid=0
goto console

:convert
echo.
echo You will convert your .ISO game to .BIN
echo Make sure you select MODE1/2352 in the second drop-down
@.\tools\CDmage.exe
goto console

:saturn
if not exist "%cd%\keys.dat" (
@copy "%cd%\tools\prod.keys" "%cd%\"
@ren "%cd%\prod.keys" "keys.dat"
)
if not exist "%cd%\keys.ini" (
@copy "%cd%\tools\prod.keys" "%cd%\"
@ren "%cd%\prod.keys" "keys.ini"
)

if not exist "%cd%\tools\prod.keys" (
echo.
echo      Put a prod.keys in the tools folder and retry!!
echo.
pause
exit
)


if not exist .\Tools\*.nsp (
echo.
echo      Put a COTTOn Boomerang – Saturn Tribute Switch NSP in the Tools folder and retry!!
echo.
pause
exit
)

@if exist .\Tools\*.nsp (
@ren .\Tools\*.nsp source.nsp
)

@if not exist "%CD%\nsp" (
@mkdir "%CD%\base"
)

mkdir "%CD%\tools\control"

@.\Tools\hactool.exe "%CD%\Tools\source.nsp" -k keys.ini -x --intype=pfs0 --pfs0dir="%CD%\base"

@.\Tools\hactool.exe "%CD%\base\b662f687676c6dd89d14f570cceb6e01.nca" -k keys.ini --titlekey="75AAFB82950E3B17C368DAE840AF3DE3" --romfsdir="%CD%\tools\romfs" --exefsdir="%CD%\tools\exefs"
@.\Tools\hactool.exe "%CD%\base\fb28dcd68fb0f2cc21bc1b606c42a581.nca" -k keys.ini --titlekey="75AAFB82950E3B17C368DAE840AF3DE3" --romfsdir="%CD%\tools\control" 
echo.
echo.
ECHO                                             Your NSP is decrypted !!
echo.
Echo.
echo.
echo ==================== SATURN ====================
echo.
@set /p mk="Change ID of NSP (eg. 01 or AA or 1A):"
@move .\tools\exefs\main.npdm .\
@call .\tools\jrepl "\x02\xE0\x26\x15\x00\x77\x00\x01" "\x%mk%\xE0\x26\x15\x00\x77\x00\x01" /m /x /f .\main.npdm /o -
@move .\main.npdm .\tools\exefs
echo.
@set /p tittle="Insert name of your ROM: "
echo.
@set /p otor="Insert name of Developper: "

@move .\tools\control\control.nacp .\

.\tools\cmdlhex.exe ".\control.nacp" --address 0x00 --hex "00000000000000000000000000000000" 
.\tools\cmdlhex.exe ".\control.nacp" --address 0x010 --hex "00000000000000000000000000000000"
.\tools\cmdlhex.exe ".\control.nacp" --address 0x00 --text "%tittle%" 

.\tools\cmdlhex.exe ".\control.nacp" --address 0x0200 --hex "00000000000000000000000000000000" 
.\tools\cmdlhex.exe ".\control.nacp" --address 0x0200 --text "%otor%" 
.\tools\cmdlhex.exe ".\control.nacp" --address 0x0800 --hex "00000000000000000000000000000000" 
.\tools\cmdlhex.exe ".\control.nacp" --address 0x0800 --text "%otor%" 

@move .\control.nacp .\tools\control\
echo.
:cover
@set /p bz="Drag and drop your Cover (.png or .jpg) and press Enter:"

copy "%bz:"=%" .\tools\control\
@del .\tools\control\*.dat

if exist .\tools\control\*.png (
ren .\tools\control\*.png *.jpg
)

@.\Tools\magick mogrify -resize 256x256! .\tools\control\*.jpg
@ren .\tools\control\*.jpg icon_AmericanEnglish.dat
copy .\tools\control\icon_AmericanEnglish.dat .\tools\control\icon_Japanese.dat 

if not exist .\tools\control\*.dat (
echo.
echo                        ERROR your cover is no present in folder...!!
echo.
goto cover
)


@del .\tools\romfs\*.bin
@del .\tools\romfs\*.cue
@del .\tools\romfs\*.ini
@copy .\tools\Config_Switch.ini .\tools\romfs\CottonBoomerang_Switch.ini
@del .\tools\romfs\PlayingGuide\English\CtnB_00.tex
@del .\tools\romfs\PlayingGuide\English\CtnB_01.tex
@del .\tools\romfs\PlayingGuide\English\CtnB_02.tex
@del .\tools\romfs\PlayingGuide\English\CtnB_03.tex
@del .\tools\romfs\PlayingGuide\Japanese\CtnB_00.tex
@del .\tools\romfs\PlayingGuide\Japanese\CtnB_01.tex
@del .\tools\romfs\PlayingGuide\Japanese\CtnB_02.tex
@del .\tools\romfs\PlayingGuide\Japanese\CtnB_03.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\English\CtnB_00.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\English\CtnB_01.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\English\CtnB_02.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\English\CtnB_03.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\Japanese\CtnB_00.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\Japanese\CtnB_01.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\Japanese\CtnB_02.tex
@copy .\tools\langues\us\CtnB_00.tex .\tools\romfs\PlayingGuide\Japanese\CtnB_03.tex
@del .\tools\romfs\Credit\English\CtnB_00.tex
@del .\tools\romfs\Credit\English\CtnB_01.tex
@copy .\tools\CtnB_00.tex .\tools\romfs\Credit\CtnB_00.tex
@copy .\tools\CtnB_00.tex .\tools\romfs\Credit\CtnB_01.tex
echo.

:game
echo "Copy and paste your SATURN GAME (.bin + .cue) in game folder and press Enter:"

@copy ".\game\*.*" .\tools\romfs\
@ren .\tools\romfs\*.cue CottonBoomerang.cue
echo.
:options
echo --------------------------------------------------------
echo                        Options:
echo.
echo                 1. Actived Widescreen
echo                 2. Actived 4MB memory (save dont works)
echo                 3. Change boot-logo (500ko max)
echo                 4. Add your Wallpapers
echo                 5. Add your .INI custom
echo                 6. Desactive the menu screen
echo                 7. Compile your Saturn NSP
echo --------------------------------------------------------

@set /p ba=

@if /i "%ba%"=="1" GOTO wide
@if /i "%ba%"=="2" GOTO 4mb
@if /i "%ba%"=="3" GOTO logo
@if /i "%ba%"=="4" GOTO wall
@if /i "%ba%"=="5" GOTO ini
@if /i "%ba%"=="6" GOTO mennu
@if /i "%ba%"=="7" GOTO nsp

:ini
@del .\tools\romfs\*.ini
@set /p in="Drag and drop your INI custom (.ini) and press Enter:"

copy %in% .\tools\romfs
@ren .\tools\romfs\*.ini CottonBoomerang_Switch.ini

if exist ".\tools\romfs\*.ini" (
echo.
echo      Your INI custom is ready !!
echo.
goto options

:wide
.\tools\cmdlhex.exe ".\tools\romfs\CottonBoomerang_Switch.ini" --address 0x0112 --hex "31" 
if exist ".\tools\romfs\CottonBoomerang_Switch.ini" (
echo.
echo      The widescreen is ready !!
)
echo.
goto options

:4mb
.\tools\cmdlhex.exe ".\tools\romfs\CottonBoomerang_Switch.ini" --address 0x02A --hex "63" 
if exist ".\tools\romfs\*.ini" (
echo.
echo      The 4mb setting is ready !!
echo.
)
goto options

:logo
@del .\tools\romfs\PlayingGuide\English\CtnB_00.tex
@del .\tools\romfs\PlayingGuide\Japanese\CtnB_00.tex
echo.
@echo You will change boot-logo for your Saturn NSP...
echo.
@set /p lo="Drag and drop your boot-logo (.png) and press Enter:"

copy %lo% .\tools\romfs\PlayingGuide\English
@ren .\tools\romfs\PlayingGuide\English\*.png 1.png
.\Tools\magick mogrify -resize 1920x1080! .\tools\romfs\PlayingGuide\English\1.png
@call .\tools\jrepl "\x89\x50\x4E\x47" "\x00\x40\x00\x00\x01\x00\x10\x08\x56\xC5\x09\x00\x80\x07\x38\x04\x89\x50\x4E\x47" /m /x /f .\tools\romfs\PlayingGuide\English\1.png /o -
@ren .\tools\romfs\PlayingGuide\English\1.png CtnB_00.tex
@copy .\tools\romfs\PlayingGuide\English\CtnB_00.tex .\tools\romfs\PlayingGuide\Japanese
echo.
goto options

:wall
@del .\tools\romfs\Wallpaper\*.tex
@copy .\tools\WP_CB_001.tex .\tools\romfs
:wall1
@echo You will make 3 wallpapers for your Saturn NSP...
echo.
@set /p wa="Drag and drop your first Wallpaper (.jpg or .png) and press Enter:"

copy %wa% .\tools\romfs\Wallpaper\*.png
@ren .\tools\romfs\Wallpaper\*.png 1.png
.\Tools\magick mogrify -resize 1280x720! .\tools\romfs\Wallpaper\1.png
@call .\tools\jrepl "\x89\x50\x4E\x47" "\x00\x40\x00\x00\x01\x00\x10\x08\x2D\xC6\xC0\x00\x00\x05\xD0\x02\x89\x50\x4E\x47" /m /x /f .\tools\romfs\Wallpaper\1.png /o -
@ren .\tools\romfs\Wallpaper\1.png WP_CB_002.tex
echo.
if exist ".\tools\romfs\Wallpaper\WP_CB_002.tex" (
echo.
echo      Your first wallpaper is ready !!
echo.
)
@set /p wa="Drag and drop your second Wallpaper (.jpg or .png) and press Enter:"

copy %wa% .\tools\romfs\Wallpaper\*.png
@ren .\tools\romfs\Wallpaper\*.png 1.png
.\Tools\magick mogrify -resize 1280x720! .\tools\romfs\Wallpaper\1.png
@call .\tools\jrepl "\x89\x50\x4E\x47" "\x00\x40\x00\x00\x01\x00\x10\x08\x2D\xC6\xC0\x00\x00\x05\xD0\x02\x89\x50\x4E\x47" /m /x /f .\tools\romfs\Wallpaper\1.png /o -
@ren .\tools\romfs\Wallpaper\1.png WP_CB_003.tex
echo.
if exist ".\tools\romfs\Wallpaper\WP_CB_003.tex" (
echo.
echo      Your second wallpaper is ready !!
echo.
)
@set /p wa="Drag and drop your third Wallpaper (.jpg or .png) and press Enter:"

copy %wa% .\tools\romfs\Wallpaper\*.png
@ren .\tools\romfs\Wallpaper\*.png 1.png
.\Tools\magick mogrify -resize 1280x720! .\tools\romfs\Wallpaper\1.png
@call .\tools\jrepl "\x89\x50\x4E\x47" "\x00\x40\x00\x00\x01\x00\x10\x08\x2D\xC6\xC0\x00\x00\x05\xD0\x02\x89\x50\x4E\x47" /m /x /f .\tools\romfs\Wallpaper\1.png /o -
@ren .\tools\romfs\Wallpaper\1.png WP_CB_004.tex
echo.
if exist ".\tools\romfs\Wallpaper\WP_CB_004.tex" (
echo.
echo      Your third wallpaper is ready !!
echo.
)
goto options

:mennu
@del .\tools\romfs\Texture.tex
@copy .\tools\Texture.tex .\tools\romfs
if exist ".\tools\romfs\Texture.tex" (
echo.
echo      The menu screen is desactived !!
echo.
)
goto options

:nsp
@mkdir .\nca\out\program\
.\Tools\hacpack.exe -o .\nca\out\program\ --type nca --ncatype program --titleid=010077001526E0%mk% --exefsdir .\tools\exefs\ --romfsdir .\tools\romfs\ --logodir .\tools\logo\

@mkdir .\nca\out\control\
.\Tools\hacpack.exe -o .\nca\out\control\ --type nca --ncatype control --titleid=010077001526E0%mk% --romfsdir .\tools\control\

.\Tools\hacpack.exe -o .\nca\out\ --titletype application --type nca --ncatype meta --titleid=010077001526E0%mk% --programnca .\nca\out\program\*.nca --controlnca .\nca\out\control\*.nca 

del /q .\base\*.*
move .\nca\out\control\*.nca .\base
move .\nca\out\program\*.nca .\base
move .\nca\out\*.nca .\base


.\Tools\hacpack.exe -o .\temp\ --type nsp --ncadir .\base\ --titleid=010077001526E0%mk% 
if not exist ".\nsp" (
@mkdir .\nsp
)
@ren ".\temp\010077001526E0%mk%.nsp" "%tittle%_saturn.nsp
@move .\temp\*.nsp .\nsp
@rmdir /s /q "%cd%\temp"
@rmdir /s /q "%cd%\nca"
@rmdir /s /q "%cd%\base"
@rmdir /s /q "%cd%\hacpack_backup"
@rmdir /s /q "%cd%\tools\exefs"
@rmdir /s /q "%cd%\tools\romfs"
@rmdir /s /q "%cd%\tools\control"

echo.
ECHO                                            Your Saturn GAME NSP is ready...!!
echo.
echo.
echo.
echo -----------------------------------------------------------
echo.
echo           Do you want create an other SATURN nsp
echo.
echo                         1. YES
echo                         2. NO
echo.
ECHO -----------------------------------------------------------
echo.

@set /p bs=

@if /i "%bs%"=="1" goto console
@if /i "%bs%"=="2" exit