
@echo off
cls
echo Data entry missing data repair tool.
echo.

if exist %1 goto DO
goto MISSING

:DO
if exist *.zro del *.zro
if exist *.old del *.old
if exist *.add del *.add
if exist fill.bat del fill.bat


:MISSING
REM Assume ALL data is missing except name and ID.

set ID = %1
set PriorityMode      =0
set EdsrShift         =0
set VSync             =0
set MaxTexCnt         =0
set BankChck          =0
set CDWait            =0
set ADXStream         =0
set OneSectorWait     =0
set NormalSpriteCheck =0
set CharPtnChck       =0
set ScaleSpriteCheck  =0
set DistSpriteCheck   =0
set FrameWait         =0

REM Create empty database.
echo set PriorityMode      =%PriorityMode% > %ID%.zro
echo set EdsrShift         =%EdsrShift% >> %ID%.zro          
echo set VSync             =%VSync% >> %ID%.zro              
echo set MaxTexCnt         =%MaxTexCnt% >> %ID%.zro          
echo set BankChck          =%BankChck% >> %ID%.zro           
echo set CDWait            =%CDWait% >> %ID%.zro            
echo set ADXStream         =%ADXStream% >> %ID%.zro         
echo set OneSectorWait     =%OneSectorWait% >> %ID%.zro     
echo set NormalSpriteCheck =%NormalSpriteCheck% >> %ID%.zro 
echo set CharPtnChck       =%CharPtnChck% >> %ID%.zro       
echo set ScaleSpriteCheck  =%ScaleSpriteCheck% >> %ID%.zro  
echo set DistSpriteCheck   =%DistSpriteCheck% >> %ID%.zro   
echo set FrameWait         =%FrameWait% >> %ID%.zro          
echo. >> %ID%.zro

:FILLIN
REM Load exising data to replace missing data.

copy %1 %1.old
Insert.exe <%1.old >%1.add /T
Insert.exe <%1.old >%1.add /I15,1
gsar.exe -i -o "-s               " "-rset " %1.add
gsar.exe -i -o "-s;" "-rGameName = :x22" %1.add
REM Enclose the game name so as not to mess up the variable table.
gsar.exe -i -o "-sset [" "-r:x22set ID = " %1.add
gsar.exe -i -o "-s]" "-r" %1.add
gsar.exe -i -o "-s:x22set" "-r:x22:x0d:x0aset" %1.add
gsar.exe -i -o "-s:x0d:x0a:x22" "-r:x22" %1.add
gsar.exe -i -o "-sset :x0d:x0a" "-r" %1.add

:CREATE
REM Pass assumed missing data onto the existing data for merging.
REM Produce new entry with known data and missing filled in.
copy *.zro+*.add fill.bat

REM Perform some cleanup.
gsar.exe -i -o "-s:x1a" "-r" fill.bat
Insert.exe <fill.bat >fill2.bat /T
del fill.bat
rename fill2.bat fill.bat
gsar.exe -i -o "-s:x3d:x0d:x0a" "-r:x3d:x30:x0d:x0a" fill.bat

REM Remove spaces in SET statements (bug in XP)
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-s:x20:x3d" "-r:x3d" Fill.bat
gsar.exe -i -o "-sID= " "-rID=" fill.bat


copy fill.bat+*.mrg fill2.bat

REM Perform some cleanup.
del fill.bat
rename fill2.bat fill.bat
gsar.exe -i -o "-s:x1a" "-r" fill.bat


REM Merge it all together and produce new data entry with all fields.
Call fill.bat

REM Remove trailing spaces.
Insert.exe <%ID%.txt >%ID%2.txt /T
del %ID%.txt
rename %ID%2.txt %ID%.txt

cls
echo It appears all has gone well.
echo You can now take your corrected file, %ID%.txt and
echo rename it to %ID%.dat.
echo Move the new data file to your GameData folder.
echo.
echo You should run the database compiler again to add
echo the update to GAMES.DAT
echo.
goto EXIT

:MISSING
Echo The data file %1 does not exist!
echo Check the file name and try again.

:EXIT
echo.
echo Press a key to delete work files.


if exist *.zro del *.zro
if exist *.old del *.old
if exist *.add del *.add
if exist fill.bat del fill.bat
