@echo off
:: Rev 1.1 2015/03/24 by-label added
:: Rev 1.2 2015/08/21 check for binary file containing 00 bytes and skip if found
:: Rev 1.3 2016/02/04 fix goto :EOF not doing popd
:: Rev 1.4 2018/11/07 Various changes!

pushd "%~dp0"

if not "%~1"=="" goto :skpuse
echo Replaces CDLABEL and UUID in a file
echo Usage: %0 [File] [UUID] [LABEL]
echo New file made = sed.txt
:skpuse

set SSEDFAIL=

SETLOCAL
set MYFILE="%~1"
set UUID=%~2
set LABEL=%~3
set Q=%~4

if /i "%~pnx1"=="\menu.lst" goto :END1
if /i "%~pnx1"=="\Sample_MyCSM_TextMode.cfg" goto :END1
if /i "%~pnx1"=="\Sample_MyCSM.cfg" goto :END1
if /i "%~p1"=="\e2b\" goto :END1
if /i "%~p1"=="\e2b\LANG\" goto :END1
if /i "%~nx1"=="video.lst" goto :END1
if /i "%~nx1"=="command.lst" goto :END1
if /i "%~nx1"=="crypto.lst" goto :END1
if /i "%~nx1"=="fs.lst" goto :END1
if /i "%~nx1"=="moddep.lst" goto :END1
if /i "%~nx1"=="partmap.lst" goto :END1
if /i "%~nx1"=="parttool.lst" goto :END1
if /i "%~nx1"=="terminal.lst" goto :END1
if /i "%~nx1"=="refind.conf" goto :END1

REM ignore file if not a menu file
set menu=0
find /i "menu" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "title" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "linux" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "kernel" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "insmod" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "include" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i ".c32" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "label" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "live" %MYFILE% >nul && set menu=1
if "%menu%"=="0" find /i "UUID" %MYFILE% >nul && set menu=1

if "%menu%"=="0" echo Skipping %~pnx1 >> LREPLACE.log
if "%menu%"=="0" goto :END1

::set MYFILE=C:\temp\test.cfg
::set UUID=NEW-NEW
::set LABEL=NEWLABEL

if "%MYFILE%"=="" set /p Input MYFILE=File : 
if not exist "%MYFILE%" goto :END1
if "%UUID%"==""   set /p Input UUID=UUID : 
if "%UUID%"=="" goto :END1
if "%LABEL%"==""  set /p Input LABEL=LABEL: 
if "%LABEL%"=="" goto :END1

REM @echo on

if exist sed1.txt del sed1.txt

::check for binary file
ssed   "s/\x0/ZZXXZZ/g" "%~1" > sed1.txt
find "ZZXXZZ" sed1.txt > nul
:: if binary byte 0 found in file then it's a binary file
if not errorlevel 1 goto :END1

::if not "%~4"=="QUIET" echo Replacing UUID with %UUID% and LABEL with %LABEL% in %MYFILE%...
echo Processing %MYFILE%... %Q%
echo Processing %MYFILE% (%~z1)... >> LREPLACE.log

if exist sed1.txt del sed1.txt
:: Search strings - e.g. cdlabel in any case followed by = and any char except a space
set CDL=[Cc][Dd][Ll][Aa][Bb][Ee][Ll]=[^^ ]*
set L=[Ll][Aa][Bb][Ee][Ll]=[^^ ]*
set MED=[Mm][Ee][Dd][Ii][Aa]

copy %MYFILE% sed.txt > nul

REM Use fc to slow down file access (maybe solve access denied/permission errors after ssed?)

REM These all don't solve the timing problem on fast drives!
REM set FAST=REM
REM set "FAST=echo %~pnx1 >> LREPLACE.LOG || REM "
REM set "FAST=fc sed.txt sed1.txt >> LREPLACE.LOG || REM "

ssed   "s/:%CDL%/:LABEL=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/=%CDL%/=LABEL=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/:%L%/:LABEL=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/=%L%/=LABEL=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/:[Uu][Uu][Ii][Dd]=[^^ ]*/:UUID=%UUID%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::ssed   "s/=[Uu][Uu][Ii][Dd]=[a-zA-Z0-9-].*/=UUID=%UUID%/g" sed.txt > sed1.txt
ssed   "s/=[Uu][Uu][Ii][Dd]=[^^ ]*/=UUID=%UUID%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/ [Uu][Uu][Ii][Dd]=[^^ ]*/ UUID=%UUID%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/ %MED%=[^^ ]*/ media=usb/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/ archisolabel=[^^ ]*/ archisolabel=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

:: extra

ssed   "s/ pmedia=cd//g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/ boot=cdrom/ boot=usb/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

:: Liberte
ssed   "s/cdroot_hash=[^^ ]*//g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

:: Panda Safe
ssed   "s/live-media=removable//g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::Manjaro
ssed   "s/misolabel=[^^ ]*/ misolabel=%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::Porteus - user must make porteussave.dat file
ssed   "s#changes=/porteus#changes=/porteus/porteussave.dat#g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::PCLinuxOS
ssed   "s/append livecd=/append fromusb livecd=/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/ui gfxboot.com/#ui gfxboot.com/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::GRML
::ssed   "s/bootid=/ignore_bootid NULL=/g" sed.txt > sed1.txt
ssed   "s/bootid=[^^ ]*/ignore_bootid /g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::Trinity rescue
ssed   "s/initrd=initrd.trk r/initrd=initrd.trk vollabel=%LABEL% r/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::Mageia/Ubuntu - remove (newline)ui gfxboot line
ssed   "s/^[.]*ui gfxboot/#ui gfxboot/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

ssed   "s/isolinux/syslinux/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::remove findiso from grub
ssed   "s/^[ ]*findiso/#findiso/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

::live-media=/dev/disk/by-label/eSysRescueLiveCD replace label
ssed   "s/by-label[/]*[^^ ]*/by-label\/%LABEL%/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt

:: HP utilities - remove hpboot
find /I "hpboot" sed.txt > nul
if errorlevel 1 goto :nothp
ssed "s/kernel[ ]*hpboot[^^ ]*/kernel vmlinuz/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt
ssed "s/append[ ]*vmlinuz[^^ ]*/append/g" sed.txt > sed1.txt
%FAST% fc sed.txt sed1.txt > nul || fc sed.txt sed1.txt >> LREPLACE.LOG
if exist sed1.txt copy sed1.txt sed.txt > nul
if exist sed1.txt del sed1.txt
:nothp

if exist sed1.txt del sed1.txt
del %MYFILE%
copy sed.txt %MYFILE% > nul
del sed.txt


goto :q
echo ----------------------------------- %Q%
type %MYFILE% %Q%
echo --- %~nx1 --- %Q%
:q

:END1

ENDLOCAL & set SSEDFAIL=%SSEDFAIL%

popd


