@echo off
if "%1" == "" goto usage
echo Warning the bootsector of drive %1 will be modified:
echo the syslinux bootsector will be written to drive %1
echo ONLY for FAT12 or FAT16 formatted media!
echo .
echo WARNING: writing wrong bootsectors may result in data loss!!!
echo (there are two alternatives which are less dangerous:
echo - Win9x/ME users can use syslinux.com -s 
echo - Linux users (including SPB-Linux) can use the command syslinux -s)
echo .
echo (press ctrl+c and then y to cancel; any other key to continue)
pause
echo .

rem check if we are outside the drive given by %1
if exist %1\spblinux.tmp del %1\spblinux.tmp
if exist \spblinux.tmp del \spblinux.tmp
echo spbinstallcheck>/spblinux.tmp
if exist %1\spblinux.tmp goto error1
if exist \spblinux.tmp del \spblinux.tmp

echo original bootsector of drive %1 will be stored in bootorig.sec>log.txt
if not exist log.txt goto error0
echo read the bootsector of drive %1 ...
dd if=\\.\%1 of=bootorig.tmp bs=512 count=1
if errorlevel 1 goto error2
if exist bootorig.sec goto warning
if exist bootnew.sec del bootnew.sec
ren bootorig.tmp bootorig.sec
:next0
copy ldlinux.bss bootnew.sec
if not exist bootnew.sec goto error0
echo install syslinux to the file bootnew.sec ...
if not exist ldlinux.bss goto error3
dd if=bootorig.sec of=bootnew.sec bs=1 skip=11 seek=11 count=51
if errorlevel 1 goto error4
echo write new bootsector to drive %1 ...
dd if=bootnew.sec of=\\.\%1 bs=512 count=1
if errorlevel 1 goto error5
echo done. - the syslinux boot sector has been written to drive %1
echo (to undo this change type: uninstal %1)

goto end

:usage
echo usage: install g:
echo installs the syslinux bootsector to drive g:
echo drive g: MUST be FAT12 or FAT16 formatted
echo a backup of the original bootsector is stored in
echo           bootorig.sec
echo (uninstall g: writes the original bootsector back to g:)
echo .
echo WARNING: writing wrong bootsectors may result in data loss!!!
echo (there are two alternatives which are less dangerous:
echo - Win9x/ME users can use syslinux.com -s 
echo - Linux users (including SPB-Linux) can use the command syslinux -s)
pause
goto end

:warning
if not exist bootnew.sec goto w2
fc /b bootnew.sec bootorig.tmp
echo (the bootsector is already installed if the file compare process did not show any differences)
:w2
echo .
echo the backup file bootorig.sec already exists; move it to another folder
echo if you want to install the syslinux bootsector a second time.
echo .
echo The bootsector on drive %1 has not been changed.
goto end


:error0
echo Error: you need write access in
cd
echo The bootsector on drive %1 has not been changed.
goto end

:error1
del %1\spblinux.tmp
echo Error: install.bat must be run from the harddisk
echo (not from the drive where spblinux shall be installed;
echo copy the folder spbsetup to the harddisk)
echo .
echo The bootsector on drive %1 has not been changed.
goto end

:error2
echo Error: reading of the bootsector of drive %1 failed
echo The bootsector on drive %1 has not been changed.
goto end

:error3
echo Error: the file ldlinux.bss could not be found.
echo The bootsector on drive %1 has not been changed.
goto end

:error4
echo Error: ldlinux.bss could not be installed to file bootnew.sec.
echo The bootsector on drive %1 has not been changed.
goto end

:error5
echo An error occurred when writing file bootnew.sec 
echo to the bootsector of drive %1
goto end

:end