
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:"=%
IF "%param0:~0,5%" == "FOUND" GOTO :checkFAILED0
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 and try again.
ECHO.
set /P temp=Press RETURN to close...
GOTO :eof

:checkSUCCEDED



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

SET type=1

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


:askInstallType
CLS
@ECHO OFF
ECHO.
ECHO This will install the PDFrasterFarian option to Explorer context menus for PDFs
ECHO.
ECHO.
ECHO You may also set the defaults for when running PDFrasterFarian
ECHO     This will change the behavior of Simple and Normal modes
ECHO     And make Advanced mode quicker
ECHO.
REM ECHO You can also create one-click conversion profiles (err... soon)
REM ECHO     These can be accessed from the pdf context menu
ECHO.
REM ECHO NOTE: Select 2 or 3 only after having studied Advanced mode!
ECHO NOTE: Select 2 only after having studied Advanced mode!
ECHO.
ECHO.
ECHO.
ECHO NEW USERS: just PRESS the ENTER/RETURN key
ECHO.
ECHO.
ECHO Install type:
ECHO   1) quick install                          (recommended)
ECHO   2) install and set defaults               (get to know PDFrasterFarian first)
REM ECHO   3) create one-click profile               (get to know PDFrasterFarian first)
ECHO.
ECHO.
ECHO                                               (press RETURN for default option)
ECHO                                                    (Type q then RETURN to quit)
SET /P type=[%type%]: 
CLS
@ECHO OFF


IF "%type%"=="1" (
	call :setRegistry
) ELSE IF "%type%"=="2" (
	call :setDefaults
	call :setRegistry
) ELSE IF "%type%"=="3" (
	ECHO OPTION NOT YET IMPLEMENTED!
	pause
	GOTO :askInstallType
) ELSE IF "%type%"=="q" ( GOTO end
) ELSE GOTO askInstallType




ECHO.
ECHO.
ECHO.
ECHO.
ECHO Installation done...
ECHO.
ECHO TO USE: right-click a PDF file in Windows Explorer
ECHO         select 'PDFrasterFarian'
ECHO.
ECHO.
ECHO ADDITIONAL SOFTWARE:
ECHO    In order to use the Explorer right-click menu, you need to install either
ECHO          Adobe Reader or Adobe Acrobat Pro
ECHO.
ECHO    In order to use FULL image processing, you will need to install AutoImager
ECHO          autoimager.com/download.asp    (note, trial won't expire)
ECHO.
ECHO    To do manual cropping use Adobe Acrobat Pro, which gives the best results,
ECHO          or the free JAP software, at files.ebook.googlepages.com/setup.exe
ECHO.
SET /P temp=Press RETURN to close...
:end
exit



:setRegistry
	SET options=%UI%.%priority%.%orientation%.%adjustOverlaps%.%hOverlap%.%vOverlap%.%autocrop%.%processing%.%resMultiplier%.%page%.%threads%
	
	"%relpath%\software\REG.exe" ADD HKEY_CLASSES_ROOT\AcroExch.Document\shell\PDFrasterFarian\command /f /ve /t REG_SZ /d "\"%relpath%\PDFrasterFarian.cmd\" \"%%1\" \"%%1\" %options%"
	"%relpath%\software\REG.exe" ADD HKEY_CLASSES_ROOT\AcroExch.Document.7\shell\PDFrasterFarian\command /f /ve /t REG_SZ /d "\"%relpath%\PDFrasterFarian.cmd\" \"%%1\" \"%%1\" %options%"
GOTO :eof













:setDefaults

: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
	CLS
	@ECHO OFF
	ECHO.
	ECHO In Windows, each process is assigned a priority (most processes are 'normal'^)
	ECHO.
	ECHO This setting lets you control whether PDFrasterFarian
	ECHO   receives greater priority for requisitioning CPU power
	ECHO.
	ECHO.
	ECHO A setting of 'BelowNormal' will leave your computer very responsive
	ECHO.
	ECHO WARNING: Using 'AboveNormal' will temporarily turn your computer into molasses
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Processing priority for running the kernels:
	ECHO   1^) AboveNormal                 (not recommended^)
	ECHO   2^) Normal                      (faster if CPU is already being heavily used^)
	ECHO   3^) BelowNormal                 (recommended^)
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)

:promptPriority
	SET /P priority=[%priority%]: 
 
       IF "%priority%"=="1" (	REM
) ELSE IF "%priority%"=="2" (	REM
) ELSE IF "%priority%"=="3" (	REM
) ELSE IF "%priority%"=="b" (	GOTO :askInterface
) ELSE IF "%priority%"=="q" (	GOTO :end
) ELSE (			ECHO Invalid input!&	GOTO :promptPriority
)




:askAutocrop
	CLS
	@ECHO OFF
	ECHO.
	ECHO Autocropping eliminates unnecessary blank margins, enlarging the text
	ECHO.
	ECHO However, manual cropping with, eg, Acrobat Pro is recommended since
	ECHO   it permits removing visible things such as page numbers.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Autocrop the file?
	ECHO   y^) Yes
	ECHO   n^) No
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                    (Type q then RETURN to quit^)

:promptAutocrop
	SET /P autocrop=[%autocrop%]: 
 
       IF "%autocrop%"=="y" (	REM
) ELSE IF "%autocrop%"=="n" (	REM
) ELSE IF "%autocrop%"=="q" (	GOTO :end
) ELSE (			ECHO Invalid input!&	GOTO :promptAutocrop
)



:askOrientation
	CLS
	@ECHO OFF
	ECHO.
	ECHO You have the option of showing whole pages on the Reader's screen
	ECHO     or splitting each page into two or four parts.
	ECHO.
	ECHO.
	ECHO Halfs layout uses the Reader's landscape mode.
	ECHO     To enable the Reader's landscape mode, hold the Size button for 5 seconds.
	ECHO     Page count is not affected
	ECHO.
	ECHO Quarters layout uses the Reader's portrait mode (as does the whole-page layout^)
	ECHO     Page count increases 4x
	ECHO     You have the option of deciding the overlaps with this layout
	ECHO.
	ECHO.
	ECHO.
	ECHO Arrange as:
	ECHO   1^) whole page                      
	ECHO   2^) split into halfs                (use the Reader's landscape mode^)
	ECHO   3^) split into quarters             (for multi-column documents only^)
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)
		
:promptOrientation
	SET /P orientation=[%orientation%]: 
 
        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
 )
 


ECHO.
ECHO.
ECHO Note, this prompt sets the default answer for the following question
ECHO       Note: question appears when selecting split-into-quarters mode
ECHO.
ECHO After this, you will be asked to specify values to set as overlap defaults
ECHO       the defaults will be used when you answer 'n' during use
ECHO.
ECHO.
ECHO Do you want to adjust the overlaps for split-into-quarters layout?
ECHO.
:askOverlapsLabel
SET /P adjustOverlaps=[%adjustOverlaps%]: 
IF "%adjustOverlaps%"=="y" (		REM
)ELSE IF "%adjustOverlaps%"=="n" (	REM
)ELSE IF "%adjustOverlaps%"=="q" ( 	GOTO :end
)ELSE (					ECHO Invalid input!&		GOTO :askOverlapsLabel
)

:askOverlap
	CLS
	@ECHO OFF
	ECHO.
	ECHO For two-column documents, 0%% horizontal overlap may work well
	ECHO For three-column documents, as much as 33%% horizontal overlap may be needed
	ECHO.
	ECHO Vertical overlap matters less, but having 10%% is usually ergonomic
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Enter a horizontal overlap ^(in percent^)
	ECHO.
	ECHO.
	ECHO                                               ^(press RETURN for default option^)
:promptHOverlap
	SET /P hOverlap=[%hOverlap%]: 
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
	SET /P vOverlap=[%vOverlap%]: 
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
)	




:askProcessing
	CLS
	@ECHO OFF
	ECHO.
	ECHO The full-blown post-processing does a good job increasing boldness and contrast
	ECHO       However, this mode can be slow (especially with higher post-proc values^)
	ECHO.
	ECHO The simple post-processing increases contrast only
	ECHO       Letters turn out sharper, but less "natural" and less bold
	ECHO.
	ECHO Choose 'none' for no post-processing (rasterize only^)
	ECHO       Sometimes useful (perhaps with image-based PDFs^)
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Level of post-processing:
	ECHO   1^) full                   (slower. Best quality. Requires AutoImager^)
	ECHO   2^) simple                 (faster^)
	ECHO   3^) none
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)
:promptProcessing
	SET /P processing=[%processing%]: 
 
       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
)



:askResMultiplier
	CLS
	@ECHO OFF
	ECHO.
	ECHO You can adjust the image enhancement. Higher values mean less boldness
	ECHO.
	ECHO     NOTE: Paradoxically, higher values quickly take much longer to calculate
	ECHO     NOTE: Higher values take geometrically more RAM as well
	ECHO     NOTE: Running 4 threads at once at a boldness of 20 can use 732MB of memory
	ECHO            On ordinary settings (2 threads, 8 boldness^), up to 52MB are used
	ECHO            Users of old machines can decrease thread count (rerun install.cmd^)
	ECHO                                        use lower (heavier^) boldness values
	ECHO                                        or use 'simple' processing
	ECHO            Users with 1GB or more of ram need to shut up
	ECHO.
	ECHO.
	ECHO.
	ECHO Level of boldness:
	ECHO   3^) heavy                              (for large-size text, esp in landscape^)
	ECHO   6^) medium
	ECHO  12^) light
	ECHO   x^) You may enter any integer value 2-20
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)
:promptResMultiplier
	SET /P resMultiplier=[%resMultiplier%]: 

       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
)




:askPreview
	CLS
	@ECHO OFF
	ECHO.
	ECHO Enter a page to preview
	ECHO   x^) Any integer
	ECHO   s^) Skip                                     (go straight to converting^)
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)
:promptPreview
	SET /P page=[%page%]: 

       IF "%page%"=="s" ( 	  REM
) ELSE IF "%page%"=="b" ( 	  GOTO :askResMultiplier
) ELSE IF "%page%"=="q" ( 	  GOTO :end
) ELSE IF %page% LEQ 0  ( 	  ECHO Invalid input!&	GOTO :promptPreview
) ELSE (			  REM
)



:askThreads
	CLS
	@ECHO OFF
	ECHO.
	ECHO You can adjust the number of pages processed at the same time
	ECHO.
	ECHO.
	ECHO This setting is intended for users of multicore or multiprocessor machines
	ECHO.
	ECHO Set the number of threads to be greater than the number of cores
	ECHO on your machine. (count HyperThreaded cores as two)
	ECHO.
	ECHO Each thread takes up RAM (especially when using high boldness values)
	ECHO.
	ECHO.
	ECHO.
	ECHO.
	ECHO Number of threads:
	ECHO   x^) You may enter any integer value 1-20
	ECHO.
	ECHO.
	ECHO                                               (press RETURN for default option^)
	ECHO                                                 (Enter b to go back, q to quit^)
:promptThreads
	SET /P threads=[%threads%]: 

       IF "%threads%"=="s" ( 	  REM
) ELSE IF "%threads%"=="b" ( 	  GOTO :askPreview
) ELSE IF "%threads%"=="q" ( 	  GOTO :end
) ELSE IF %threads% LEQ 0  ( 	  ECHO Invalid input!&	GOTO :promptThreads
) ELSE IF %threads% GTR 20 ( 	  ECHO Invalid input!&	GOTO :promptThreads
) ELSE (			  REM
)
ECHO.

GOTO :eof