
:: INFO:

:: GRUB2 FILE MANAGER SETUP & DOWNLOAD SUITE
:: Copyright (C) 2020  mephistooo2 | TNCTR.com
:: Grub2 File Manager USB Installer Github: https://github.com/abdullah-erturk/SecureBoot-Grub2FM-Suite

:: Ventoy & Grub2FM Multiboot (With Original Files)
:: Copyright (C) 2020  mephistooo2 | TNCTR.com
:: Ventoy & Grub2FM Multiboot (With Original Files) Github: https://github.com/abdullah-erturk/Ventoy-Grub2FM-Multiboot-With-Original-Files

:: Grub2 File Manager Github: https://github.com/a1ive/grub2-filemanager
:: credit: Alive

:: Ventoy Github: https://github.com/ventoy/Ventoy
:: credit: longpanda
::===============================================================================================================
@echo off
cd /d "%~dp0"
if NOT "%cd%"=="%cd: =%" (
    echo.
    echo    ..:: WARNING ::..
    echo    There are spaces in the current folder name.
    echo    Please rename it to a name that does not contain the folder space character.
    echo.
    echo    Press any key to EXIT...
    echo.
	pause >nul
    goto :EOF
)

if "[%1]" == "[49127c4b-02dc-482e-ac4f-ec4d659b7547]" goto :MAINMENU
REG QUERY HKU\S-1-5-19\Environment >NUL 2>&1 && goto :MAINMENU

set command="""%~f0""" 49127c4b-02dc-482e-ac4f-ec4d659b7547
SETLOCAL ENABLEDELAYEDEXPANSION
set "command=!command:'=''!"

echo.
echo          ENABLING ADMINISTRATOR RIGHTS...
echo.
echo			  please wait...

powershell -NoProfile Start-Process -FilePath '%COMSPEC%' ^
-ArgumentList '/c """!command!"""' -Verb RunAs 2>NUL

IF %ERRORLEVEL% GTR 0 (
    echo.
    echo =============================================================
    echo This script needs to be run as administrator.
    echo =============================================================
    echo.
pause
)
SETLOCAL DISABLEDELAYEDEXPANSION
goto :EOF
::===============================================================================================================
:MAINMENU
setlocal enableextensions disabledelayedexpansion
for /f "tokens=6 delims=[]. " %%# in ('ver') do set winbuild=%%#
del %temp%\msg.vbs /f /q >nul 2>&1
echo Set WshShell = CreateObject("WScript.Shell"^) >> %temp%\msg.vbs
echo x = WshShell.Popup ("The Operating System is not Windows 10. The script file only runs on Windows 10. The process will be terminated in 5 seconds.",5, "..:: WARNING ::..") >> %temp%\msg.vbs
if %winbuild% LSS 10240 (
call %temp%\msg.vbs
del %temp%\msg.vbs /f /q >nul 2>&1
exit
)
::===============================================================================================================
mode con:cols=94 lines=38
title SecureBoot (MokManager EFI) Grub2FM ^& Ventoy Suite - mephistooo2 ^| TNCTR.com
:Main
cls
echo.
echo ==============================================================================================
echo.
echo	  GRUB2 FILE MANAGER ^& VENTOY SETUP Revision 7 by mephistooo2 ^| TNCTR.com
echo.
echo	 	  1 - Ventoy ^& Grub2FM INSTALL USB/HDD DISK
echo.
echo	 	  2 - GRUB2 FILE MANAGER ^& VENTOY UPDATE
echo.
echo	 	  3 - GRUB2 FILE MANAGER ^& VENTOY VISIT WEBSITE (TNCTR - GITHUB)
echo.
echo	 	  4 - EXIT
echo.
echo ==============================================================================================
echo.
choice /c 1234 /cs /n /m "YOUR CHOICE : "
echo.
if errorlevel 4 Exit
if errorlevel 3 goto :TNCTR
if errorlevel 2 goto :Update
if errorlevel 1 goto :Ventoy
echo.
::===============================================================================================================
:Ventoy
	echo.   [N]on-Destructive Installation       [F]ormatted Installation
	echo.
	set dest=0
	choice /C:NF /N /M "How would you like the Ventoy setup to be?:
	if errorlevel 2 goto :Destructive
	if errorlevel 1 set dest=1
	if errorlevel 1 goto :Non-Destructive 

:Non-Destructive
	call :MsgBox "In the Non-Destructive Installation option, the disk to be installed Ventoy must be in NTFS format."  "VBInformation" "..:: WARNING ::.."
	call :MsgBox "In the Ventoy installation, you must select \n \n'Partition Configuration' from the 'Option' \n \nsection and reserve the desired amount of space for Grub2FM, at least 40 MB with the \n \n'Preserve some space at the end of the disk' \n \noption. \n \nYou can do the Non-Destructive Installation from the option menu."  "VBInformation" "..:: ATTENTION ::.."
	cd /d %~dp0Ventoy
	Ventoy2Disk.exe
	goto :Non-DestructiveInstall

:Destructive
	echo.
	set diskstr=
	echo.   [1] NTFS       [2] FAT32       [3] exFAT       [4] UDF
	echo.
	choice /C:4321 /N /M "Select partition structure for disk storage after Ventoy installation : "
	if errorlevel 1 set diskstr=UDF
	if errorlevel 2 set diskstr=exFAT
	if errorlevel 3 set diskstr=FAT32
	if errorlevel 4 set diskstr=NTFS
	
:ask
	echo.
	echo After Ventoy installation, write the desired size in MB for the Grub2FM 
	set /p "disksize=partition, provided that it is at least 40 MB: " || goto :ask
	setlocal enabledelayedexpansion 
	for /f "delims=0123456789" %%a in ("!disksize!") do set "disksize="
	endlocal & set "disksize=%disksize%"

	if not defined disksize (
	echo.
	echo Please use numbers only...
	timeout /t 3 > nul
	goto :ask
    )
	set value=40
	If %disksize% GEQ %value% (
	GOTO :VentoyInstall 
	) ELSE (
	echo.
	echo Input FAT32 size as MINIMUM 40 MB...
	timeout /t 3 > nul
	goto :ask
	)
	
:VentoyInstall
	cd /d %~dp0Ventoy
	call :showDiskTable
    set /p "   diskNumber=Ventoy & Grub2FM type the disk number to install: "
	echo.	
	echo	 Please wait...
	Ventoy2Disk.exe VTOYCLI /I /PhyDrive:%diskNumber% /NOUSBCheck /R:%disksize% >nul 2>&1

:Non-DestructiveInstall	
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo RESCAN
    )
    type "%scriptFile%" >nul
	diskpart /s "%scriptFile%" >nul 
    del /q "%scriptFile%" >nul

	call :NTFSLetter
	set "label=VTOYEFI"
	set q1= $Q1=(gwmi Win32_Volume -filter $('Label='+[char]34+$env:label+[char]34)).DeviceID
	set q2= $Q2=(gwmi MSFT_Partition -Namespace root\Microsoft\Windows\Storage ^|where {$_.AccessPaths -contains $Q1}).DiskNumber
	for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; $Q2}"') do set diskfind=%%s
	
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK
		echo SELECT DISK %diskfind% 
		echo SELECT PART 1 
		echo ASSIGN LETTER=%ntfsdrive%
    )
    type "%scriptFile%" >nul
	diskpart /s "%scriptFile%" >nul 
    del /q "%scriptFile%" >nul
	label %ntfsdrive%: Ventoy
	
	cd %~dp0 > nul
	for /f "tokens=2 delims={}" %%# in ('powershell -nop -c "(Get-Volume | ? FileSystemLabel -eq 'VTOYEFI').Path"') do set "usb=%%#"
	SET a=\\?\Volume{
	SET b=}\
	SET c=%a%%usb%%b%
	md fix >nul
	mountvol %~dp0fix %c% >nul 
	echo.
	echo	 Fixing mbr boot file from Grub2FM to Ventoy...
	robocopy files fix\grub\i386-pc core.img >nul 
	mountvol %~dp0fix /d >nul
	rmdir fix >nul

	call :FAT32Letter
	set "label=VTOYEFI"
	set q1= $Q1=(gwmi Win32_Volume -filter $('Label='+[char]34+$env:label+[char]34)).DeviceID
	set q2= $Q2=(gwmi MSFT_Partition -Namespace root\Microsoft\Windows\Storage ^|where {$_.AccessPaths -contains $Q1}).DiskNumber
	for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; $Q2}"') do set disk=%%s

	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %disk%
		echo CREATE PARTITION PRIMARY
		echo FORMAT QUICK FS=FAT32 LABEL="GRB2FM_BOOT"		
		echo ASSIGN LETTER=%fat32drive%
    )
    type "%scriptFile%" >nul
	echo.
	echo	 Formatting the free space of the disk...
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	echo.	
	echo	 Copying Grub2FM files...
	robocopy Grub2FM %fat32drive%:\ /E >nul

	call :NTFSLetter
	set "drive=%fat32drive%:\"
	set rel=__RELPATH LIKE '%%%drive%\%%'
	for /f %%s in ('powershell -nop -c "((gwmi win32_logicaldisktopartition -Filter $env:rel).Antecedent -split '<?Disk #(\d+)')[1]"') do set finddisk=%%s

	if %dest% equ 1 (
	goto :continue
	)

	if %diskstr% equ exFAT (
	goto :continue
	)
	
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %finddisk%
		echo LIST PART
		echo SEL PART 1	
		echo FORMAT QUICK FS=%diskstr% LABEL="Ventoy"		
		echo ASSIGN LETTER=%ntfsdrive%
    )
    type "%scriptFile%" >nul
	echo.
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul

:continue
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %finddisk%
		echo LIST PART
		echo SEL PART 1		
		echo ASSIGN LETTER=%ntfsdrive%
    )
    type "%scriptFile%" >nul
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	
	mkdir %ntfsdrive%:\ventoy >nul  
	cd /d %~dp0 >nul
	echo.
	echo	 Copying ventoy_grub.cfg, ventoy_wimboot.img and ventoy_vhdboot.img to disk storage  
	robocopy files %ntfsdrive%:\ventoy ventoy_wimboot.img >nul
	robocopy files %ntfsdrive%:\ventoy ventoy_vhdboot.img >nul
	robocopy files %ntfsdrive%:\ventoy ventoy_grub.cfg >nul
	TIMEOUT /T 2 > nul

	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo SELECT VOLUME %fat32drive% 
		echo REMOVE LETTER %fat32drive%
		echo SET ID = 27 OVERRIDE 
		echo SET ID = EF OVERRIDE 
    )
    type "%scriptFile%" >nul
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	
	set label=VTOYEFI
	for /f %%a in ('fsutil volume list') do for /f "tokens=4" %%V in ('fsutil volume queryLabel %%a ^| findstr /c:%label%') do set "vefi=%%~V"
	set str=%vefi% 
	set str=%str::\=% 

	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo SELECT VOLUME %str%
		echo REMOVE LETTER %str%
    )
    type "%scriptFile%" >nul
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	goto :end
::===============================================================================================================
:end
	echo.
	echo.
    echo	 OK
	echo.
	call :MsgBox "Ventoy & Grub2FM MultiBoot Installation completed."  "VBInformation" ".:: Ventoy & Grub2FM MultiBoot ::."
	choice /C:MX /N /M "Press M button for MAIN MENU -- Press the X button for EXIT : "
	if errorlevel 2 Exit
	if errorlevel 1 goto :Main
::===============================================================================================================
:NTFSLetter
	set ntfsdrive=
	for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set ntfsdrive=%%a
	exit /b 0
::===============================================================================================================
:FAT32Letter
    set "fat32drive="
    for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set fat32drive=%%a
	exit /b 0
::===============================================================================================================
:MsgBox
	Rem 64=vbInformation, 48=vbExclamation, 16=vbCritical 32=vbQuestion
    setlocal enableextensions
    set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
	>"%tempFile%" echo(WScript.Quit MsgBox(Replace("%~1","\n",vbcrlf),%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
    set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
    endlocal & exit /b %exitCode%
::===============================================================================================================
:Update
	call :MsgBox "You should use this partition if you already have \n \nVentoy & Grub2FM MultiBoot \n \non your disk."  "VBInformation" "..:: ATTENTION ::.."
	echo Which MultiBoot build do you want to update??
	echo.
	echo.   [V]entoy       [G]rub2FM       [M]ain Menu
	echo.
	choice /C:VGM /N /M "YOUR CHOICE : "
	if errorlevel 3 goto :Main 
	if errorlevel 2 goto :Grub2FMUpdate
	if errorlevel 1 goto :VentoyUpdate
::===============================================================================================================
:VentoyUpdate
	cd /d %~dp0Ventoy
	call :showDiskTable
    set /p "   diskNumber=Select your Ventoy installed disk: "
	Ventoy2Disk.exe VTOYCLI /U /PhyDrive:%diskNumber% /NOUSBCheck
	
	echo.
	echo	 Please wait...
	cd %~dp0

	set "label=VTOYEFI"
	set q1= $Q1=(gwmi Win32_Volume -filter $('Label='+[char]34+$env:label+[char]34)).DeviceID
	set q2= $Q2=(gwmi MSFT_Partition -Namespace root\Microsoft\Windows\Storage ^|where {$_.AccessPaths -contains $Q1}).DiskNumber
	for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; $Q2}"') do set vdisk=%%s

	call :FAT32Letter
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %vdisk%
		echo LIST PART
		echo SEL PART 2		
		echo SET ID = 0B OVERRIDE 
		echo ASSIGN LETTER=%fat32drive%
    )
    type "%scriptFile%" >nul
	echo.
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	
	echo	 Fixing mbr boot file from Grub2FM to Ventoy...
	robocopy files %fat32drive%:\grub\i386-pc core.img >nul
	TIMEOUT /T 2 > nul

	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %vdisk%
		echo LIST PART
		echo SEL PART 2		
		echo REMOVE LETTER=%fat32drive%
		echo SET ID = 27 OVERRIDE 
		echo SET ID = EF OVERRIDE 
    )
    type "%scriptFile%" >nul
	echo.
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul

	echo.
    echo	 OK
	echo.
	choice /C:MX /N /M "Press M button for MAIN MENU -- Press the X button for EXIT : "
	if errorlevel 2 Exit
	if errorlevel 1 goto :Main
::===============================================================================================================
:Grub2FMUpdate
	echo.
	echo	 Please wait...
	cd %~dp0

	set "label=GRB2FM_BOOT"
	set q1= $Q1=(gwmi Win32_Volume -filter $('Label='+[char]34+$env:label+[char]34)).DeviceID
	set q2= $Q2=(gwmi MSFT_Partition -Namespace root\Microsoft\Windows\Storage ^|where {$_.AccessPaths -contains $Q1}).DiskNumber
	for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; $Q2}"') do set gdisk=%%s

	call :FAT32Letter
	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %gdisk%
		echo LIST PART
		echo SEL PART 3		
		echo SET ID = 0B OVERRIDE 
		echo ASSIGN LETTER=%fat32drive%
    )
    type "%scriptFile%" >nul
	echo.
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul
	
	echo	 Updating Grub2FM files...
	robocopy Grub2FM %fat32drive%:\ /E >nul
	TIMEOUT /T 2 > nul

	set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
		echo LIST DISK	
		echo SELECT DISK %gdisk%
		echo LIST PART
		echo SEL PART 3		
		echo REMOVE LETTER=%fat32drive%
		echo SET ID = 27 OVERRIDE 
		echo SET ID = EF OVERRIDE 
    )
    type "%scriptFile%" >nul
	echo.
	diskpart /s "%scriptFile%" >nul
    del /q "%scriptFile%" >nul

	echo.
    echo	 OK
	echo.
	choice /C:MX /N /M "Press M button for MAIN MENU -- Press the X button for EXIT : "
	if errorlevel 2 Exit
	if errorlevel 1 goto :Main
::===============================================================================================================
:showDiskTable
	echo.
    echo ======================================================
	echo	 ATTENTION: MAKE SURE YOU CHOOSE THE CORRECT DISK !!!
	echo.
	echo list disk | diskpart | findstr /b /c:" "
    echo ======================================================
    goto :eof
::===============================================================================================================
:TNCTR
	start https://www.tnctr.com/topic/1174848-ventoy-grub2fm-multiboot-orijinal-dosyalarla/
	start https://github.com/a1ive/grub2-filemanager/releases
	start https://github.com/ventoy/Ventoy/releases
	goto Main
::===============================================================================================================