
REM

IF "%orientation%"=="1" (
	SET xSize=637
	SET ySize=849
) ELSE IF "%orientation%"=="2" (
	SET xSize=601
	SET ySize=800
) ELSE IF "%orientation%"=="3" (
	SET xSize=637
	SET ySize=849
)

ECHO.
ECHO ...Creating LRS source file (ignore any errors during this step)

cmd /A /C ECHO ^<?xml version=^"1.0^" encoding=^"UTF-16^"?^> > "%relpath%\temp\temp.lrs"
ECHO ^<BBeBXylog version="1.0"^>^<Property/^>^<BookInformation^>^<Info version="1.0" ^>^<BookInfo^> >> "%relpath%\temp\temp.lrs"
ECHO ^<Title reading=""^>%title%^</Title^>							>> "%relpath%\temp\temp.lrs"
ECHO ^<Author reading=""^>%author%^</Author^>							>> "%relpath%\temp\temp.lrs"
ECHO ^<BookID^>FB_0000000000001^</BookID^>							>> "%relpath%\temp\temp.lrs"
ECHO ^<Publisher reading=""^>Aleksandr Dubinsky's Imprint of Corporeal Pressings^</Publisher^>	>> "%relpath%\temp\temp.lrs"
ECHO ^<Label reading=""^>^</Label^>								>> "%relpath%\temp\temp.lrs"
ECHO ^<Category^>^</Category^>									>> "%relpath%\temp\temp.lrs"
ECHO ^<Classification^>^</Classification^> 							>> "%relpath%\temp\temp.lrs"
ECHO ^<FreeText^>^</FreeText^> 									>> "%relpath%\temp\temp.lrs"
ECHO ^</BookInfo^>^<DocInfo^> 									>> "%relpath%\temp\temp.lrs"
ECHO ^<CThumbnail file="%relpath%modules\book_thumb.gif"/^>					>> "%relpath%\temp\temp.lrs"
ECHO ^<Language^>en^</Language^>								>> "%relpath%\temp\temp.lrs"
ECHO ^<Creator^>^</Creator^>									>> "%relpath%\temp\temp.lrs"
ECHO ^<CreationDate^> 2006-12-11 ^</CreationDate^>						>> "%relpath%\temp\temp.lrs"
ECHO ^<Producer^>PDFrasterFarian v2.1^</Producer^>						>> "%relpath%\temp\temp.lrs"
ECHO ^<SumPage^>%numPages%^</SumPage^>								>> "%relpath%\temp\temp.lrs"
ECHO ^</DocInfo^>^</Info^> 									>> "%relpath%\temp\temp.lrs"

CALL :outputTOC

ECHO ^</BookInformation^>^<Main^> 								>> "%relpath%\temp\temp.lrs"

ECHO %numPages%
FOR /L %%G IN (1, 1, %numPages%) DO call :LRS1 %%G

ECHO ^</Main^>^<Template version="1.0"^>^</Template^>^<Style^>					>> "%relpath%\temp\temp.lrs"

ECHO ^<BookStyle stylelabel="58" objid="58"^>^<SetDefault rubyalign="center" rubyadjust="none" rubyoverhang="none" empdotsposition="before" emplineposition="before" emplinetype="solid" setwaitprop="noreplay"/^> >> "%relpath%\temp\temp.lrs" >> "%relpath%\temp\temp.lrs"
ECHO ^<BookSetting bindingdirection="Lr" dpi="1600" screenwidth="600" screenheight="800" colordepth="24"/^>^</BookStyle^> >> "%relpath%\temp\temp.lrs"
ECHO ^<PageStyle stylelabel="77" topmargin="0" headheight="0" headsep="0" oddsidemargin="0" textheight="800" textwidth="600" footspace="0" evensidemargin="0" footheight="0" layout="LrTb" bgimagemode="fix" pageposition="any" setwaitprop="noreplay" setemptyview="empty" objid="77"/^> >> "%relpath%\temp\temp.lrs"
ECHO ^<BlockStyle stylelabel="86" bgimagemode="fix" framemode="square" blockwidth="600" blockheight="800" blockrule="horz.adjustable" layout="LrTb" framewidth="0" framecolor="0x00000000" topskip="0" sidemargin="0" footskip="0" objid="86"/^> >> "%relpath%\temp\temp.lrs"
ECHO ^</Style^>^<Objects^> 									>> "%relpath%\temp\temp.lrs"

FOR /L %%G IN (1, 1, %numPages%) DO call :LRS2 %%G

ECHO ^</Objects^>^</BBeBXylog^> 								>> "%relpath%\temp\temp.lrs"

ECHO LRS creation DONE
ECHO.
GOTO :eof



:LRS1
call :setPageVar %1
ECHO ^<Page pagestyle="77" objid="10%page%" objlabel="Page.10%page%"^>^<BlockSpace xspace="0" yspace="0"/^>^<ImageBlock x0="0" y0="0" x1="600" y1="800" xsize="%xSize%" ysize="%ySize%" refstream="30%page%" blockwidth="600" blockheight="768" topskip="0" footskip="0" sidemargin="0" blockstyle="86" objid="20%page%" objlabel="Block.20%page%"^>^</ImageBlock^>^</Page^> >> "%relpath%\temp\temp.lrs"
GOTO :eof

:LRS2
call :setPageVar %1
ECHO ^<ImageStream encoding="PNG" file="%relpath%\temp\result\%page%" objid="30%page%" imagestreamlabel="ImageStream.30%page%"/^> >> "%relpath%\temp\temp.lrs"
GOTO :eof


:outputTOC
	IF "%orientation%"=="3" SET /A numPages=numPages*4
	ECHO ^<TOC^> >> "%relpath%\temp\temp.lrs"
	FOR /F "tokens=1-9" %%A in ('call more "%relpath%temp\doc_data.txt"') DO CALL :doFor %%A %%B %%C %%D %%E %%F %%G %%H %%I
	ECHO ^</TOC^> >> "%relpath%\temp\temp.lrs"
GOTO :eof
:doFor
	IF "%1"=="BookmarkTitle:" SET bookmarkTitle=%2 %3 %4 %5 %6 %7 %8 %9
	SET param1=%BookmarkTitle:/=%
	SET param1=%BookmarkTitle:\=%
	SET param1=%BookmarkTitle::=%
	
	REM IF "%1"=="BookmarkTitle:" SET bookmarkTitle=TOC
	
	IF "%1"=="BookmarkPageNumber:" CALL :setToCPage %2
	IF "%1"=="BookmarkPageNumber:" IF NOT %page% LEQ 0 IF NOT %page% GTR %numPages% ECHO ^<TocLabel refobj="20%page%" refpage="10%page%"^>%bookmarkTitle%^</TocLabel^> >> "%relpath%\temp\temp.lrs"
GOTO :eof
:setToCPage
	SET temp1=%1

	IF "%orientation%"=="3" (
		SET /A temp2=^(temp1-1^)*4+1
	) ELSE (
		SET temp2=%temp1%
	)
	
	CALL :setPageVar %temp2%
GOTO :eof


:setPageVar
	SET page=0000%1
	SET temp=%1
:begin		
	IF NOT DEFINED temp GOTO :eof
	SET temp=%temp:~1%
	SET page=%page:~1%
GOTO :begin
GOTO :eof