REM todo
REM manual cropping
REM djvu
REM ask threadcount
REM batch mode
REM profiles

REM recompile pdftk
REM fix color reduction

REM change temp dir for gs and imgmagick
REM in simple mode, prompt for alpha bits, text alignment
REM redo ask for quarters


REM issues
REM autocropping doesn't work if page was manually cropped


REM check if input file ends in .PDF

CLS
CLS
@ECHO OFF



REM Check for illegal characters which Windows CMD scripts can't handle

SET param0="%~dp0"
SET param0=%param0:*)=FOUND%
SET param0=%param0:*&=FOUND%
SET param0=%param0:"=%
SET param1="%~f1"
SET param1=%param1:*)=FOUND%
SET param1=%param1:*&=FOUND%
SET param1=%param1:"=%
SET param2="%~f2"
SET param2=%param2:*)=FOUND%
SET param2=%param2:*&=FOUND%
SET param2=%param2:"=%
IF "%param1%"=="" SET param1= 
IF "%param2%"=="" SET param2= 
IF "%param0:~0,5%" == "FOUND" GOTO :checkFAILED0
IF "%param1:~0,5%" == "FOUND" GOTO :checkFAILED1
IF "%param2:~0,5%" == "FOUND" GOTO :checkFAILED1
GOTO :checkSUCCEDED

:checkFAILED0
ECHO ERROR!!!!
ECHO.
ECHO The path of PDFrasterFarian contains an illegal character!
ECHO.
ECHO It is either a closing paranthesis or an ampersand (AND sign)
ECHO PDFrasterFarian cannot continue. Please rename the directory and try again.
ECHO.
set /P temp=Press RETURN to close...
GOTO :eof
:checkFAILED1
ECHO ERROR!!!!
ECHO.
ECHO The path of the input and/or output file contains an illegal character!
ECHO.
ECHO It is either a closing paranthesis or an ampersand (AND sign)
ECHO PDFrasterFarian cannot continue. Please rename and try again.
ECHO.
set /P temp=Press RETURN to close...
GOTO :eof

:checkSUCCEDED




SET relpath=%~dp0
SET PATH=%relpath%\modules;%relpath%\software;%PATH%

SET inputFile=%~f1
SET outputFile=%~f2



SET options=%3
IF "%options%"=="" (
	SET UI=2
	SET priority=3
	SET orientation=2
	SET adjustOverlaps=n
	SET hOverlap=25
	SET vOverlap=10
	SET autocrop=y
	SET processing=1
	SET resMultiplier=6
	SET page=1
	SET threads=2
		
	SET promptless=n
	
	SET inputType=2
) ELSE (
	FOR /F "delims=. tokens=1-20" %%A in ("%options%") DO (
		SET UI=%%A
		SET priority=%%B
		SET orientation=%%C
		SET adjustOverlaps=%%D
		SET hOverlap=%%E
		SET vOverlap=%%F
		SET autocrop=%%G
		SET processing=%%H
		SET resMultiplier=%%I
		SET page=%%J
		SET threads=%%K
		
		SET promptless=%%L
		
		SET inputType=%%M
	)
)
SET numPages=
SET continue=y
SET title=
SET author=

TITLE PDFrasterFarian v2.2.0 - %~n1


IF NOT EXIST "%relpath%\temp\" mkdir "%relpath%\temp\"
IF NOT EXIST "%relpath%\temp\stage1" mkdir "%relpath%\temp\stage1\" & IF NOT EXIST "%relpath%\temp\stage2" mkdir "%relpath%\temp\stage2\"
IF NOT EXIST "%relpath%\temp\stage3" mkdir "%relpath%\temp\stage3\" & IF NOT EXIST "%relpath%\temp\stage4" mkdir "%relpath%\temp\stage4\"
IF NOT EXIST "%relpath%\temp\stage5" mkdir "%relpath%\temp\stage5\" & IF NOT EXIST "%relpath%\temp\stage6" mkdir "%relpath%\temp\stage6\"
IF NOT EXIST "%relpath%\temp\stage7" mkdir "%relpath%\temp\stage7\" & IF NOT EXIST "%relpath%\temp\stage8" mkdir "%relpath%\temp\stage8\"
IF NOT EXIST "%relpath%\temp\stage9" mkdir "%relpath%\temp\stage9\" & IF NOT EXIST "%relpath%\temp\result" mkdir "%relpath%\temp\result\"
del /Q "%relpath%\temp\*"
del /Q "%relpath%\temp\stage1\*" & del /Q "%relpath%\temp\stage2\*" & del /Q "%relpath%\temp\stage3\*" & del /Q "%relpath%\temp\stage4\*"
del /Q "%relpath%\temp\stage5\*" & del /Q "%relpath%\temp\stage6\*" & del /Q "%relpath%\temp\stage7\*" & del /Q "%relpath%\temp\stage8\*"
del /Q "%relpath%\temp\stage9\*" & del /Q "%relpath%\temp\result\*"






IF EXIST "%inputFile%" GOTO :skipAskInputFile

:askInputFile
 call "UI captions.cmd" askInputFile

IF "%inputFile%"=="q" (		GOTO :end
)

:skipAskInputFile
ECHO.
ECHO.




IF NOT "%outputFile%"=="" GOTO :skipAskoutputFile

:askOutputFile
 call "UI captions.cmd" askOutputFile

IF "%OutputFile%"=="q" (	GOTO :end
)

:skipAskOutputFile
IF NOT "%outputFile:~-4%"==".lrf" IF "%outputFile:~-4%"==".pdf" (SET outputFile=%outputFile:~,-4%.lrf) ELSE (SET outputFile=%outputFile%.lrf)
ECHO __test__ > "%outputFile%"
FOR /F %%A in ('more "%outputFile%"') DO SET contents=%%A
IF NOT "%contents%"=="__test__" (
	call "%relpath%\modules\UI captions.cmd" warn2OutputFile
	GOTO :promptOutputFile
) ELSE del /Q "%outputFile%"




CLS
@ECHO OFF
ECHO Reading PDF data... (this may take a minute)
ECHO.

"%relpath%\software\pdcat.exe" "%inputFile%" "%relpath%\temp\input.pdf"
IF EXIST "%relpath%\temp\input.pdf" SET inputFile=%relpath%\temp\input.pdf

"%relpath%\software\pdftk.exe" "%inputFile%" dump_data output "%relpath%\temp\doc_data.txt"

FOR /F "tokens=1-9" %%A in ('call more "%relpath%\temp\doc_data.txt"') DO IF "%%A"=="NumberOfPages:" SET numPages=%%B



IF "%promptless%"=="y"  (	goto :skipPrompts
)
	:askInterface
	 call "UI captions.cmd" askInterface
	:promptInterface
	 call "UI captions.cmd" promptInterface

	       IF "%UI%"=="1" (		REM
	) ELSE IF "%UI%"=="2" (		REM
	) ELSE IF "%UI%"=="3" (		REM
	) ELSE IF "%UI%"=="q" (		GOTO :end
	) ELSE (			ECHO ERROR: invalid option&	GOTO :promptInterface
	)
	
	
	

	:askPriority
	 call "UI captions.cmd" askPriority
	:promptPriority
	 call "UI captions.cmd" promptPriority

	       IF "%priority%"=="1" (	SET priority=ABOVENORMAL
	) ELSE IF "%priority%"=="2" (	SET priority=NORMAL
	) ELSE IF "%priority%"=="3" (	SET priority=BELOWNORMAL
	) ELSE IF "%priority%"=="b" (	GOTO :askInterface
	) ELSE IF "%priority%"=="q" (	GOTO :end
	) ELSE (			ECHO Invalid input!&	GOTO :promptPriority
	)




	:askAutocrop
	 call "UI captions.cmd" askAutocrop
	:promptAutocrop
	 call "UI captions.cmd" promptAutocrop

	       IF "%autocrop%"=="y" (	REM
	) ELSE IF "%autocrop%"=="n" (	REM
	) ELSE IF "%autocrop%"=="b" (	GOTO :askInterface
	) ELSE IF "%autocrop%"=="q" (	GOTO :end
	) ELSE (			ECHO Invalid input!&	GOTO :promptAutocrop
	)




	:askVSplit
	 call "UI captions.cmd" askVSplit
	:promptVSplit
	 call "UI captions.cmd" promptVSplit


	:askVSplitPosition
	 call "UI captions.cmd" askVSplitPosition
	:promptVSplitPosition
	 call "UI captions.cmd" askVSplitPosition



	:askOrientation
	 call "UI captions.cmd" askOrientation
	:promptOrientation
	 call "UI captions.cmd" promptOrientation

	       IF "%orientation%"=="1" ( REM
	) ELSE IF "%orientation%"=="2" ( REM
	) ELSE IF "%orientation%"=="3" ( REM
	) ELSE IF "%orientation%"=="b" ( GOTO :askAutocrop
	) ELSE IF "%orientation%"=="q" ( GOTO :end
	) ELSE (			ECHO Invalid input!&	GOTO :promptOrientation
	)

	IF NOT "%orientation%"=="3" GOTO :skipQuartersConfig
	IF "%UI%"=="1" GOTO :skipQuartersConfig
		ECHO.
		ECHO Would you like to adjust the overlaps?
		:askOverlapsLabel
		SET /P adjustOverlaps=[%adjustOverlaps%]: 

		      IF "%adjustOverlaps%"=="y" (	REM
		)ELSE IF "%adjustOverlaps%"=="n" (	GOTO :skipQuartersConfig
		)ELSE IF "%adjustOverlaps%"=="q" ( 	GOTO :end
		)ELSE (					ECHO Invalid input!&		GOTO :askOverlapsLabel
		)

		:askOverlap
		 call "UI captions.cmd" askOverlap
		:promptHOverlap
		 call "UI captions.cmd" promptHOverlap

		       IF "%hOverlap%"=="q" ( 	GOTO :end
		) ELSE IF %hOverlap% LSS 0 (	ECHO Invalid input!&		GOTO :hOverlapLabel
		) ELSE IF %hOverlap% GEQ 50 (	ECHO Invalid input! Overlap cannot be more than 50%&	GOTO :hOverlapLabel
		)

		ECHO.
		ECHO Enter a vertical overlap ^(in percent^)
		ECHO.
		ECHO                                               ^(press RETURN for default option^)
		:promptVOverlap
		 call "UI captions.cmd" promptVOverlap

		       IF "%vOverlap%"=="q" (	GOTO :end
		) ELSE IF %vOverlap% LSS 0 (	ECHO Invalid input!&		GOTO :vOverlapLabel
		) ELSE IF %vOverlap% GEQ 50 (	ECHO Invalid input! Overlap cannot be more than 50%&		GOTO :vOverlapLabel
		)	
	:skipQuartersConfig




	:askProcessing
	 call "UI captions.cmd" askProcessing
	:promptProcessing
	 call "UI captions.cmd" promptProcessing

	       IF "%processing%"=="1" ( REM
	) ELSE IF "%processing%"=="2" (	REM
	) ELSE IF "%processing%"=="3" (	REM
	) ELSE IF "%processing%"=="b" ( GOTO :askOrientation
	) ELSE IF "%processing%"=="q" ( GOTO :end
	) ELSE (			ECHO Invalid input!&	GOTO :promptProcessing
	)

	IF "%processing%" NEQ "1" GOTO :skipAskMultiplier
		:askResMultiplier
		 call "UI captions.cmd" askResMultiplier
		:promptResMultiplier
		 call "UI captions.cmd" promptResMultiplier

		       IF "%resMultiplier%"=="b" ( GOTO :askProcessing
		) ELSE IF "%resMultiplier%"=="q" ( GOTO :end
		) ELSE IF %resMultiplier% LSS 2  ( ECHO Invalid input!&	  GOTO :promptResMultiplier
		) ELSE IF %resMultiplier% GTR 20 ( ECHO Invalid input!&	  GOTO :promptResMultiplier
		) ELSE (			  REM
		)
	:skipAskMultiplier





	:askPreview
	 call "UI captions.cmd" askPreview
	:promptPreview
	 call "UI captions.cmd" promptPreview

	       IF "%page%"=="s" ( 	  GOTO :skipPreview
	) ELSE IF "%page%"=="b" ( 	  GOTO :askProcessing
	) ELSE IF "%page%"=="q" ( 	  GOTO :end
	) ELSE IF %page% LEQ 0  ( 	  ECHO Invalid input!&	GOTO :promptPreview
	) ELSE IF %page% GTR %numPages% ( ECHO Invalid input!&	GOTO :promptPreview
	) ELSE (			  REM
	)
	ECHO.


	CLS
	@ECHO OFF
	ECHO Generating preview....
	call :setPageVar %page%
	SET previewFilename=p%RANDOM%.png
	call kernel-preview.cmd
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Opening MSPaint...
	IF NOT EXIST "%relpath%\temp\%previewFilename%" (
		ECHO.
		ECHO.
		ECHO.
		ECHO ERROR: preview generation FAILED!
		ECHO.
		ECHO If the problem persists, please report it at the mobileread forums.
	) ELSE (
		start mspaint "%relpath%\temp\%previewFilename%"
	)




	:askContinue
	 call "UI captions.cmd" askContinue
	:promptContinue
	 call "UI captions.cmd" promptContinue

	       IF "%continue%"=="y" (	REM
	) ELSE IF "%continue%"=="n" (	GOTO :askOrientation
	) ELSE IF "%continue%"=="p" (	GOTO :askPreview
	) ELSE IF "%continue%"=="b" (	GOTO :askProcessing
	) ELSE IF "%continue%"=="q" (	GOTO :end
	) ELSE (			ECHO Invalid input!&	GOTO :promptContinue
	)
	:skipPreview

:skipPrompts


:askMeta
CLS
@ECHO OFF
ECHO.
ECHO Setting up the conversion is done!
ECHO.

SET title=%4
:askTitle
IF "%title%"=="" (
	SET /P title=Please enter a title for this book: 
)
if "%title%"=="" ( GOTO :askTitle
)
SET title=%title:"=%

SET author=%5
IF "%author%"=="" (
	SET /P author=Please enter an author for this book (optional^): 
)
IF "%author%"=="" ( GOTO :skipReplace 
)
SET author=%author:"=%
:skipReplace




IF EXIST "%TEMP%\magick.*" del /Q "%TEMP%\magick.*"
SET /A loopIterations=(numPages-numPages%%threads)/threads
SET /A lastIteration=loopIterations+1
SET /A loopRemainder=numPages%%threads
ECHO.
ECHO.
ECHO ...PROCESSING
FOR /L %%A in (1, 1, %loopIterations%) DO (
	for /L %%B in (1, 1, %threads%) DO call :startThread %%A %%B
	call :waitForThreads %%A %threads%
)
for /L %%B in (1, 1, %loopRemainder%) DO (
	call :startThread %lastIteration% %%B
)
call :waitForThreads %lastIteration% %loopRemainder%

"%relpath%\software\sleep.exe" 2

ECHO ...Moving files
IF "%orientation%"=="3" (
	FOR %%G IN ("%relpath%\temp\stage9\*") DO (
		ECHO Dicing page:  %%~nG of %numPages%
		CALL :getQuarters %%~nG
		DEL /Q "%relpath%\temp\stage9\%%~nG"
	)
) ELSE MOVE /Y "%relpath%\temp\stage9\*" "%relpath%\temp\result\"


ECHO Processing DONE



call kernel-LRS.cmd



ECHO ...Creating compiled LRF (BBeB) ebook file
"%relpath%\software\lrs2lrf\lrs2lrf.exe" "%relpath%\temp\temp.lrs" "%outputFile%"
ECHO LRF creation DONE







CLS
@ECHO OFF
ECHO.
IF "%orientation%"=="1" ECHO Orientation: portrait
IF "%orientation%"=="2" ECHO Orientation: landscape
IF "%orientation%"=="3" (
	ECHO Orientation: quarters
	ECHO   Horizontal overlap: %hOverlap%%
	ECHO   Vertical overlap: %vOverlap%%
)
ECHO Autocropping: %autocrop%
IF "%processing%"=="1" (
	ECHO Processing: full
	ECHO   Boldness: %resMultiplier%
IF "%processing%"=="2" ECHO Processing: simple
IF "%processing%"=="3" ECHO Processing: none
)
ECHO.
ECHO.
ECHO Title: %title%
ECHO Author: %author%
ECHO.
ECHO.
ECHO.
ECHO You can find your ebook at:
ECHO %outputFile%
ECHO.
ECHO.
ECHO.
IF "%promptless%"=="n" (
	SET /P temp=Press RETURN to close...
)

:end

del /Q "%relpath%\temp\*" & del /Q "%relpath%\temp\bbox\*" & del /Q "%relpath%\temp\bbox_flags\*" & del /Q "%relpath%\temp\result\*" & del /Q "%relpath%\temp\stage1\*" & del /Q "%relpath%\temp\stage2\*" & del /Q "%relpath%\temp\stage3\*" & del /Q "%relpath%\temp\stage4\*" & del /Q "%relpath%\temp\stage5\*" & del /Q "%relpath%\temp\stage6\*" & del /Q "%relpath%\temp\stage7\*"
IF EXIST lrs2lrf.log   del /Q lrs2lrf.log
IF EXIST doc_data.txt  del /Q doc_data.txt

GOTO :eof








:getQuarters
	SET origPage=%1
	SET tempPage=####%origPage%
	SET tempPage=%tempPage:####000=%
	SET tempPage=%tempPage:####00=%
	SET tempPage=%tempPage:####0=%
	
	SET /A newPage=(tempPage-1)*4+1
	call :setPageVar %newPage%
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage9\%origPage%" -gravity NorthWest -crop 565x754+0+0 "%relpath%\temp\result\%page%"
	
	SET /A newPage=newPage+1
	call :setPageVar %newPage%
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage9\%origPage%" -gravity SouthWest -crop 565x754+0+0 "%relpath%\temp\result\%page%"
	
	SET /A newPage=newPage+1
	call :setPageVar %newPage%
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage9\%origPage%" -gravity NorthEast -crop 565x754+0+0 "%relpath%\temp\result\%page%"
	
	SET /A newPage=newPage+1
	call :setPageVar %newPage%
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage9\%origPage%" -gravity SouthEast -crop 565x754+0+0 "%relpath%\temp\result\%page%"
GOTO :eof




:startThread
	SET a=%1
	SET b=%2
	SET /A page=(a-1)*threads + b
	ECHO Starting to process page:  %page% of %numPages%
	call :setPageVar %page%
	start "processing thread" /MIN /%priority% kernel-processing.cmd "%relpath%" "%inputFile%" %page% %autocrop% %orientation% %processing% %resMultiplier%
GOTO :eof


:waitForThreads
  SET a=%1
  SET b=%2
  SET /A firstPage=(a-1)*threads + 1
  SET /A lastPage=(a-1)*threads + b
  SET flag=0

 :bigLoop
   "%relpath%\software\sleep.exe" 1
   SET flag=1
   SET /A page2=firstPage
  :smallLoop
    IF %page2% GTR %lastPage% GOTO :eof
    call :setPageVar %page2%
    IF %page2% GTR %lastPage% ECHO GREATER
    IF NOT EXIST "%relpath%\temp\stage9\%page%" GOTO :bigLoop
    SET /A page2+=1
   GOTO :smallLoop
  GOTO :bigLoop
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