@echo off
Title Welcome to CTGP-7.

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
	echo.
	echo Welcome to CTGP-7.
	echo.
	echo To prevent some errors, admin rights must be requested.
	echo.
	echo If you can't or don't want to get admin rights, type noadmin,
	echo else press Enter.
	set /P adm=" "
	if /I "%adm%"=="noadmin" goto prepare
	cls
	echo.
	echo Requesting administrative privileges...
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
	
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
	
:prepare	
mkdir log
del log\errorlog.txt
cls
echo.
echo Cleaning up folders, please wait.
rd /s /q output
rd /s /q tempdata
mkdir tempdata
mkdir output
echo This is the error log file, if you have any troubles, send me the error code. > log\errorlog.txt
echo ------------------------------------------------------------ >> log\errorlog.txt
echo. >> log\errorlog.txt
cls

:start
if not exist %SYSTEMROOT%\system32\robocopy.exe goto notrobo
echo.
echo.Enter your GAME region:
echo.
echo NOTE: If you are having trouble, check out the official tutorial/support
echo page typing tutorial.
echo.
echo 1. EUR
echo 2. USA
echo 3. JAP
echo.
set /P ver="Type (1/2/3): "
if /I "%ver%"=="tutorial" (
	start https://gbatemp.net/threads/mario-kart-7-custom-track-grand-prix-7-ctgp-7.396936/
	cls
	goto start
	)
if /I "%ver%"=="1" set version=EUR
if /I "%ver%"=="2" set version=USA
if /I "%ver%"=="3" set version=JPN
if [%version%]==[] cls & echo Press 1, 2 or 3. & goto start
cls
echo.
echo Can you install unlegit cias?
echo.
echo 1. Yes
echo 2. No
echo.
set /P sysver="Type (1/2): "
if /I "%sysver%"=="1" cls & goto cfw
if /I "%sysver%"=="2" cls & goto sayhans
if /I "%sysver%"=="yes" cls & goto cfw
if /I "%sysver%"=="no" cls & goto sayhans
cls
echo.
echo Press 1 or 2.
echo.
pause
exit

:cfw
echo You have 3 options.
echo.
echo 1. HANS (launch from homebrew launcher, NO updater/course manager/200cc)
echo 2. NTR CFW (launch from home menu, has an updater/course manager/200cc)
echo 3. CIA (launch from home menu, NO updater/course manager/200cc)
set /P sysver="Type 1 to 3: "
if /I "%sysver%"=="1" cls & goto hans
if /I "%sysver%"=="2" cls & goto ntr
if /I "%sysver%"=="3" cls & goto cia
cls
echo.
echo Type 1, 2 or 3 only.
goto cfw

:sayhans
echo.
echo You can only use HANS.
echo.
echo Press a key to continue.
pause > nul
cls
goto hans

:hans
echo.
set /P filename="Enter your MK7 decrypted 3ds/cia/cxi filename: "
for %%i in ("%filename%") do (
	if "%%~xi"==".cia" (
		tools\ctrtool.exe --contents=tempdata\MK7 "%filename%" 2>> log\errorlog.txt
		ren tempdata\MK7.0000* MK7.cxi 2>> log\errorlog.txt
		del tempdata\MK7.0001* 2>> log\errorlog.txt
		del tempdata\MK7.0002* 2>> log\errorlog.txt
		goto romfs
	)
	if "%%~xi"==".3ds" (
		tools\3dstool.exe -xvtf cci "%filename%" -0 tempdata\MK7.cxi
		goto romfs
	)
	if "%%~xi"==".cci" (
		tools\3dstool.exe -xvtf cci "%filename%" -0 tempdata\MK7.cxi
		goto romfs
	)
	if "%%~xi"==".cxi" (
		tools\ctrtool --romfs=tempdata\romfs.bin "%filename%" 2>> log\errorlog.txt
		tools\ctrtool.exe -t romfs --romfsdir=tempdata\romfs tempdata\romfs.bin 2>> log\errorlog.txt
		if not exist tempdata\romfs cls & echo Error unpacking romfs & goto start
		cls
		echo.
		echo Romfs unpacked succefully.
		goto build
	)
)
if not exist "%filename%" cls & echo File does not exist. & goto hans
cls
echo Invalid input file, make sure the file extension is ".3ds", ".cxi" or ".cia"
goto hans

:romfs
tools\ctrtool.exe --romfs=tempdata\romfs.bin tempdata\MK7.cxi 2>> log\errorlog.txt
tools\ctrtool.exe -t romfs --romfsdir=tempdata\romfs tempdata\romfs.bin 2>> log\errorlog.txt
if not exist tempdata\romfs cls & echo Error unpacking romfs & goto start
cls
echo.
echo Romfs unpacked succefully.
goto build

:build
%SYSTEMROOT%\system32\robocopy.exe files\CTGP-7romfs tempdata\romfs /s 2>> log\errorlog.txt
mkdir output
mkdir output\hans
mkdir output\3ds
cls
echo.
echo Building romfs
tools\3dstool.exe -cvtf romfs output\hans\ctgp-7.romfs --romfs-dir tempdata\romfs 2>> log\errorlog.txt
cls
echo. 
echo Copying files.
%SYSTEMROOT%\system32\robocopy.exe files\CTGP-7shortcut\%version% output\3ds /s 2>> log\errorlog.txt
echo YOU DON'T NEED TO RENAME ctgp-7.romfs, JUST FOLLOW THE TUTORIAL^^! > output\hans\readme.txt
goto end

:ntr
cls
echo.
echo Only use this tool if the instructions in the official tutorial don't work for you.
echo.
pause
cls
echo.
echo NTR uses layeredfs plugin. To use it you will need DevKitPro and Python 2.7, do you want to proceed?
echo.
echo (It will download the intaller for you)
set /P installntr="(Yes/No): "
if /I "%installntr%"=="Yes" cls & goto preparentr
if /I "%installntr%"=="No" cls & goto cfw
goto ntr

:preparentr
cls
echo.
if exist "C:\devkitPro\devkitARM\bin" (
	set devkitpro=1
	echo Detected DevKitARM
) else (
	set devkitpro=0
	echo DevKitARM not detected
)
echo.
if exist "C:\Python27\python.exe" (
	set python27=1
	echo Detected Python 2.7
) else (
	set python27=0
	echo Python 2.7 not detected
)
if /I "%devkitpro%"=="1" (
	if /I "%python27%"=="1" (
		goto layeredfs
	)
)
echo.
echo Press a key to install missing programs.
pause > nul
if /I "%devkitpro%"=="0" (
	bitsadmin /transfer "Downloading DevKitPro installer" http://tenet.dl.sourceforge.net/project/devkitpro/Automated%%20Installer/devkitProUpdater-1.6.0.exe "%__CD__%tempdata\devkitpro.exe"
	cls
	echo.
	echo Running the installer, do not change the installation path nor close this window.
	tempdata\devkitpro.exe
)
if /I "%python27%"=="0" (
	bitsadmin /transfer "Downloading Python27 installer" https://www.python.org/ftp/python/2.7.11/python-2.7.11.msi "%__CD__%tempdata\python27.msi"
	cls
	echo.
	echo Running the installer, do not change the installation path nor close this window.
	tempdata\python27.msi
)
goto preparentr

:layeredfs
echo.
set /P filename="Enter your MK7 decrypted 3ds/cia/cxi filename: "
for %%a in ("%filename%") do (
	if "%%~xa"==".cia" (
		tools\ctrtool.exe --contents=tempdata\MK7 "%filename%" 2>> log\errorlog.txt
		ren tempdata\MK7.0000* MK7.cxi 2>> log\errorlog.txt
		del tempdata\MK7.0001* 2>> log\errorlog.txt
		del tempdata\MK7.0002* 2>> log\errorlog.txt
		goto extractexefs
	)
	if "%%~xa"==".3ds" (
		tools\3dstool.exe -xvtf cci "%filename%" -0 tempdata\MK7.cxi
		goto extractexefs
	)
	if "%%~xa"==".cci" (
		tools\3dstool.exe -xvtf cci "%filename%" -0 tempdata\MK7.cxi
		goto extractexefs
	)
	if "%%~xa"==".cxi" (
		echo.
		echo Starting to build layeredfs plugin.
		del tools\layeredfs\plugin\source\autogen.h
		tools\ctrtool --exefs=tempdata\exefs.bin "%filename%" 2>> log\errorlog.txt
		goto buildlayerefs
	)
)
if not exist "%filename%" cls & echo File does not exist. & goto layeredfs
cls
echo Invalid input file, make sure the file extension is ".3ds", ".cxi" or ".cia"
goto layeredfs

:extractexefs
mkdir output\plugin
mkdir output\CTGP-7
mkdir output\CTGP-7\gamefs
cls
echo.
echo Starting to build layeredfs plugin.
del tools\layeredfs\plugin\source\autogen.h
tools\ctrtool --exefs=tempdata\exefs.bin tempdata\MK7.cxi 2>> log\errorlog.txt
goto buildlayerefs

:buildlayerefs
mkdir output\manager_cia
tools\ctrtool -t exefs --decompresscode --exefsdir=tools\layeredfs\workdir\exefs tempdata\exefs.bin 2>> log\errorlog.txt
copy tools\layeredfs\workdir\exefs\code.bin tools\layeredfs\workdir\dump.bin
cd tools\layeredfs
cls
echo.
echo Generating layeredfs.
C:\Python27\python.exe "%__CD__%locate.py" workdir\dump.bin
cd plugin
C:\Python27\python.exe "%__CD__%build.py"
cd ..
cd ..
cd ..
echo.
pause
cls
echo.
echo copying files.
echo.
if /I "%version%"=="JPN" (
	mkdir output\plugin\0004000000030600 
	copy tools\layeredfs\plugin\payload.bin output\plugin\0004000000030600\CTGP-7OnionFS.plg
)
if /I "%version%"=="EUR" (
	mkdir output\plugin\0004000000030700
	copy tools\layeredfs\plugin\payload.bin output\plugin\0004000000030700\CTGP-7OnionFS.plg
)
if /I "%version%"=="USA" (
	mkdir output\plugin\0004000000030800 
	copy tools\layeredfs\plugin\payload.bin output\plugin\0004000000030800\CTGP-7OnionFS.plg
)
%SYSTEMROOT%\system32\robocopy.exe files\CTGP-7romfs output\CTGP-7\gamefs /s 2>> log\errorlog.txt
%SYSTEMROOT%\system32\robocopy.exe files\lua output\CTGP-7 /s 2>> log\errorlog.txt
%SYSTEMROOT%\system32\robocopy.exe files output\manager_cia CTGP-7.cia /s 2>> log\errorlog.txt
ren output\manager_cia\CTGP-7.cia CTGP-7mngr.cia
goto end

:cia
echo.
set /P filename="Enter your MK7 decrypted cia filename: "
for %%i in ("%filename%") do (
	if "%%~xi"==".cia" goto ciacia
	if "%%~xi"==".cxi" cls & echo braindump CXI files aren't supported, only CIA files. & goto cia
)
if not exist "%filename%" cls & echo File does not exist. & goto cia
cls
echo Invalid input file, make sure the file extension is ".cia"
goto cia

:ciacia
cls
echo.
echo Starting conversion.
tools\ctrtool.exe --contents=tempdata\MK7 "%filename%" 2>> log\errorlog.txt
ren tempdata\MK7.0000* MK7.cxi 2>> log\errorlog.txt
ren tempdata\MK7.0001* manual.cfa 2>> log\errorlog.txt
ren tempdata\MK7.0002* dlp.cfa 2>> log\errorlog.txt
goto ciadone

:notrobo
cls
echo.
echo %SYSTEMROOT%\system32\robocopy.exe isn't detected in your system.
echo.
pause
exit

:ciadone
tools\3dstool.exe -xvtf cxi tempdata\MK7.cxi --header tempdata\header.bin --exefs tempdata\exefs.bin --romfs tempdata\romfs.bin --logo tempdata\logo.bin --extendedheader tempdata\exthdr.bin 2>> log\errorlog.txt
tools\3dstool.exe -xvtf romfs tempdata\romfs.bin --romfs-dir tempdata\romfs 2>> log\errorlog.txt
if not exist tempdata\romfs\RaceCommon.szs cls & echo File is encrypted or not Mario Kart 7 game & goto cia 
tools\3dstool.exe -xvtf exefs tempdata\exefs.bin --header tempdata\exefshdr.bin --exefs-dir tempdata\exefs 2>> log\errorlog.txt
%SYSTEMROOT%\system32\robocopy.exe files\CTGP-7exefs tempdata\exefs /s 2>> log\errorlog.txt
%SYSTEMROOT%\system32\robocopy.exe files\CTGP-7romfs tempdata\romfs /s 2>> log\errorlog.txt
del tempdata\romfs.bin 2>> log\errorlog.txt
del tempdata\exefs.bin 2>> log\errorlog.txt
cls
echo.
echo Change CTGP-7 title ID?
echo.
echo Yes (Allows installing CTGP-7 ALONG with existing MK7 cia, but CANNOT go online.)
echo No (Will OVERWRITE existing MK7 cia, but CAN go online)
echo.
set /P installntr="(Yes/No): "
if /I "%installntr%"=="Yes" C:\Python27\python.exe tools\changeID.py tempdata\header.bin tempdata\exthdr.bin 2>> log\errorlog.txt
tools\3dstool.exe -cvtf exefs tempdata\exefs.bin --header tempdata\exefshdr.bin --exefs-dir tempdata\exefs 2>> log\errorlog.txt
tools\3dstool.exe -cvtf romfs tempdata\romfs.bin --romfs-dir tempdata\romfs 2>> log\errorlog.txt
del tempdata\MK7.cxi 2>> log\errorlog.txt
if exist tempdata\logo.bin (
	if not exist tempdata\exefs\logo.bin.lz (
		tools\3dstool.exe -cvtf cxi tempdata\MK7.cxi --header tempdata\header.bin --exefs tempdata\exefs.bin --extendedheader tempdata\exthdr.bin --logo tempdata\logo.bin --romfs tempdata\romfs.bin 2>> log\errorlog.txt
	) else (
		tools\3dstool.exe -cvtf cxi tempdata\MK7.cxi --header tempdata\header.bin --exefs tempdata\exefs.bin --extendedheader tempdata\exthdr.bin --romfs tempdata\romfs.bin 2>> log\errorlog.txt
	)
) else (
	tools\3dstool.exe -cvtf cxi tempdata\MK7.cxi --header tempdata\header.bin --exefs tempdata\exefs.bin --extendedheader tempdata\exthdr.bin --romfs tempdata\romfs.bin 2>> log\errorlog.txt
)
if not exist tempdata\manual.cfa (
	if exist tempdata\dlp.cfa (
		echo WARNING: Manual not found, building without it.
		echo WARNING: Manual not found, building without it. Ingame manual is disabled. 2>> log\errorlog.txt
		tools\makerom.exe -f cia -content tempdata\MK7.cxi:0:0 -content tempdata\dlp.cfa:2:2 -o output\CTGP-7.cia 2>> log\errorlog.txt
		goto end
	)
	echo WARNING: Manual and Download Play not found, building without them.
	echo WARNING: Manual and Download Play not found, building without them. 2>> log\errorlog.txt
	tools\makerom.exe -f cia -content tempdata\MK7.cxi:0:0 -o output\CTGP-7.cia 2>> log\errorlog.txt
	goto end
)
if not exist tempdata\dlp.cfa (
	echo WARNING: Download Play not found, building without it.
	echo WARNING: Download Play not found, building without it. Local multiplayer is disabled. 2>> log\errorlog.txt
	tools\makerom.exe -f cia -content tempdata\MK7.cxi:0:0 -content tempdata\dlp.cfa:2:2 -o output\CTGP-7.cia 2>> log\errorlog.txt
	goto end
)
tools\makerom.exe -f cia -content tempdata\MK7.cxi:0:0 -content tempdata\manual.cfa:1:1 -content tempdata\dlp.cfa:2:2 -o output\CTGP-7.cia 2>> log\errorlog.txt
goto end

:end
cls
echo.
echo Generation done, now copy the contents of output folder to your SD root.
echo Press any key to exit.
pause > nul