!BAT
:: GRUB4DOS batch script. See Tutorial at www.rmprepusb.com  Tutorial #96
:: Uses: Adds all *.iso file contents to make a single menu
::

:: An optional \grub\xxx.hdr file can be included to contain default menu settings (color etc.)

:: This script used the default image file path as %2 variable - e.g. /_ISO/AutoISO

:: A menu file is generated in memory by this code for all ISO files in the /%2 folder and below
:: Run this grub4dos batch file using the following 2 lines

:: \grub\VistaMen.g4b .Automenu Win8
:: Configfile (md)0x3000+0x50
::
:: This script requires GRUB4DOS 2010-12-14 or later.

:: If ELBACKUP=YES then a copy of the generated menu will be written to 

::=============================================================================

::set DD=0
::Uncomment line below to show debug lines
::set DD=1

debug off
checkrange 20101214:-1 read 0x8278 || echo Err.version && exit 1
:: this batch file calls itself, so use the label as the place to jump to
goto %~x1
exit

:.automenu

set FLD=%2
set KXML=


#used by 
set WINDIR=%2

:: Variables: %0=/xx/automenu.bat %~nx0=automenu.bat %~nx1=.automenu %1=.automenu %~x1=.automenu
::echo 0=%0 nx0=%~nx0 nx1=%~nx1 nx2=%~nx2 nx3=%~nx3 1=%1 x1=%~x1
:: get the menu folder into DIR variable
::echo Checking if .makemenu is loaded - if not load it
::delmod -l %~nx0 || insmod %0 .makemenu
if not "%MOD%"=="1" delmod -l %~nx0 || insmod %0

if "%DD%"=="1" echo Writing default menu entry
if "%DD%"=="1" echo -e default 0\n#timeout 10\n\n > (md)0x3000+0x50
if exist (bd)/%grub%/%FLD%.hdr echo Using default user menu header /%grub%/%FLD%.hdr %redir%
cat --length=0 (bd)/%grub%/%FLD%.hdr && set MDIR=(bd)/%grub%/%FLD%.hdr && call :XLATE
::set ME=%0

if "%FLD%"=="" goto :endloop
set DIR=/_ISO/Windows/%FLD%
echo Using %DIR% as search folder %redir%
echo Looking for files starting at %DIR% %redir%

#ls %DIR%/ | call :cpa

call :cpa

:endloop

echo -e -n \ntitle\x20 >> (md)0x3000+0x50
echo -n \n  >> (md)0x3000+0x50
echo -e \nroot () >> (md)0x3000+0x50

if not "%LBACKMENU%"=="" set MDIR=()/%grub%/MenuWinMenu.hdr && call :XLATE
if not "%LBACKMENU%"=="" echo -e -n \ntitle\x20 >> (md)0x3000+0x50
if not "%LBACKMENU%"=="" echo -n \n  >> (md)0x3000+0x50
if not "%LBACKMENU%"=="" echo -e \nroot () >> (md)0x3000+0x50
if not "%LBACKMENU%"=="" if not exist NOF7HD set MDIR=()/%grub%/MenuF7.hdr && call :XLATE
if not "%LBACKMENU%"=="" if not exist NOF8B  set MDIR=()/%grub%/MenuF8.hdr && call :XLATE
if "%NOF9R%"==""                             set MDIR=()/%grub%/MenuF9.hdr && call :XLATE
#if "%NOF10H%"==""                           set MDIR=()/%grub%/MenuF10.hdr && call :XLATE

set DD=
set DIR=
set hlp=
set FLD=
set MDIR=
:: must set root back!
root (bd)/
delmod %~nx0 > nul

debug 1
exit

:: ----------------------- SUBROUTINES ----------------------


:.makemenu
if "%DD%"=="1" echo @MAKEMENU
setlocal
if "%DD%"=="1" echo Checking (bd)%DIR%/%1
if "%1"=="" endlocal && goto :EOF
::If is a directory ignore it
::ZZZcat --length=0 (bd)%DIR%/%1 || goto :EOF
if  "##"=="#%~x1#" goto :eof
echo %~x1 > (md)0x880+1
cat --locatei=.imgptn (md)0x880+1 > nul && goto :imgptn
if /i not "#.iso#"=="#%~x1#" goto :eof

echo -e \n >> (md)0x3000+0x50
set LL=0
cat --length=0 ()%DIR%/%~n1.txt > nul && set LL=1
if "%LL%"=="1" && cat --length=9 --locate="title " ()%DIR%/%~n1.txt > nul || echo $[0104]ERROR: No 'title' keyword in %~n1.txt! && set LL=0 && pause

if not exist EXTOFF if "%LL%"=="0" echo -e Adding %DIR%/%1 %redir% && echo title %1\n Boot to %DIR%/%1 >> (md)0x3000+0x50
if exist EXTOFF if "%LL%"=="0" echo -e Adding %DIR%/%1 %redir% && echo title %~n1\n Boot to %DIR%/%1 >> (md)0x3000+0x50


if "%LL%"=="1" echo -e Adding %DIR%/%1 and menu title from %DIR%/%~n1.txt %redir% && cat ()%DIR%/%~n1.txt >> (md)0x3000+0x50
set LL=

echo -e \n >> (md)0x3000+0x50
echo set ISO=%1 >> (md)0x3000+0x50
echo /%grub%/RunWin8.g4b /_ISO/WINDOWS/%FLD% >> (md)0x3000+0x50
echo -e boot >> (md)0x3000+0x50
goto :EOF

:imgptn
echo -e \n >> (md)0x3000+0x50
set LL=0
cat --length=0 ()%DIR%/%~n1.txt > nul && set LL=1
if "%LL%"=="1" && cat --length=9 --locate="title " ()%DIR%/%~n1.txt > nul || echo $[0104]ERROR: No 'title' keyword in %~n1.txt! && set LL=0 && pause

if not exist EXTOFF if "%LL%"=="0" echo -e Adding %DIR%/%1 %redir% && echo title %1\n Boot to %DIR%/%1 >> (md)0x3000+0x50
if exist EXTOFF if "%LL%"=="0" echo -e Adding %DIR%/%1 %redir% && echo title %~n1\n Boot to %DIR%/%1 >> (md)0x3000+0x50

if "%LL%"=="1" echo -e Adding %DIR%/%1 and menu title from %DIR%/%~n1.txt %redir% && cat ()%DIR%/%~n1.txt >> (md)0x3000+0x50
set LL=

echo -e \n >> (md)0x3000+0x50
echo set ISO=%1 >> (md)0x3000+0x50
echo /%grub%/qrun.g4b %DIR%/%1 >> (md)0x3000+0x50
echo -e \nboot >> (md)0x3000+0x50
goto :EOF


:cpa
if "%DD%"=="1" echo @CPA: Current Root: %DIR%
if "%DD%"=="1" echo Change source root to (bd)%DIR%/
root (bd)%DIR%/
ls | call :copyfiles
goto :eof

:copyfiles
if "%DD%"=="1" echo @COPYFILES %1
if "%1"=="" goto :eof
::If is a directory
cat --length=0 /%1 || goto :sub-dir
::if "##"=="#%~x1#" goto :sub-dir

::copy file
if "%DD%"=="1" echo @COPY FILE
call :.makemenu %1
shift
goto :copyfiles

:sub-dir
if "%DD%"=="1" echo @SUB_DIR %1
setlocal
set DIR=%DIR%/%1
call :cpa
endlocal
shift
goto :copyfiles


:XLATE
set DDIR=%DIR%
:: get rid of leading ()
if "%DDIR:~0,2%"=="()" set DDIR=%DDIR:~2%
if "%DD%"=="1" echo Changing all $HOME$ strings in file %MDIR% to %DDIR%
echo > (md)0x3100+0x20
set ST=0
set END=0
:XLOOP
if "%DD%"=="1" echo Start=%ST%
cat --skip=%ST% --locate=$HOME$ --number=1 %MDIR% > nul
set /a END=%?% > nul
if  "%END%"=="0" goto :nohome
set /a LENGTH=%END%-%ST% > nul
cat --skip=%ST% --length=%LENGTH% %MDIR% >> (md)0x3100+0x20
echo -n %DDIR% >> (md)0x3100+0x20
set /a ST=%END%+6 > nul
goto :XLOOP

:nohome
cat --skip=%ST% %MDIR% >> (md)0x3100+0x20
echo >> (md)0x3100+0x20
::cat (md)0x3100+0x50
::pause
:: copy to menu

:: check for $$STR in (md)0x3100+0x20
call :XLATE9
set DD=

::cat (md)0x3300+0x20 >> (md)0x3000+0x50
set LENGTH=
set END=
set ST=
set DDIR=
goto :EOF




::-----------------------

:: -- $$STR substitution


:XLATE9
set MDIR=(md)0x3100+0x20
if "%DD%"=="1" echo Changing all $$STR strings in file %MDIR%
echo -n > (md)0x3300+0x20
set ST=0
set END=0

:XLOOP9
if "%DD%"=="1" echo Looking for $$STR starting at %ST%
cat --skip=%ST% --locate="$$STR" --number=1 %MDIR% > nul
set /a END=%?% > nul
if  "%END%"=="0" goto :nohome9
set /a LENGTH=%END%-%ST% > nul
cat --skip=%ST% --length=%LENGTH% %MDIR% >> (md)0x3300+0x20
cat --skip=%END% --length=9 %MDIR% | echo -e !BAT\nset A= > (md)0x3200+1
call (md)0x3200+1
if "%DD%"=="1" echo Found %A%

:: now find A in (md)0x880+0x80
cat --locate=%A% --number=1 (md)0x880+0x80 > nul
set /a ALOC=%?% > nul
if "%ALOC%"=="0" echo $[0104]ERROR %A% NOT FOUND IN STRINGS.TXT FILE! && pause
if "%ALOC%"=="0" echo -n %A% >> (md)0x3300+1 && goto :badstr
set /a ALOC=%ALOC%+10 > nul
:: get end of line
cat --locate=\x0a --skip=%ALOC% --number=1 (md)0x880+0x80 > nul
set /a LL=%?%-%ALOC%-1 > nul
cat --skip=%ALOC% --length=%LL% (md)0x880+0x80 >> (md)0x3300+0x20
if "%DD%"=="1" echo Replacing with
if "%DD%"=="1" cat --skip=%ALOC% --length=%LL% (md)0x880+0x80 && echo 
set A=
set ALOC=
set LL=
:badstr

# move past string and CR\LF
set /a ST=%END%+9 > nul

goto :XLOOP9

:nohome9
if "%DD%"=="1" echo NOHOME9
cat --skip=%ST% %MDIR% >> (md)0x3300+0x20
echo -n >> (md)0x3300+0x20
:: copy to menu
cat (md)0x3300+0x20 >> (md)0x3000+0x50

set ALOC=
set A=
set LL=
set LENGTH=
set END=
set ST=
set DDIR=
goto :EOF





