@echo off

setlocal EnableDelayedExpansion
set folder=%cd%
cd work\out

choice /m "Fix paging "
if %errorlevel%==1 (
copy D:\Retro-Work\correct_pages.php .\ /y
php -f correct_pages.php
cd newpages
)


if NOT EXIST output mkdir output
if NOT EXIST tablet mkdir tablet
set thumb=1

for %%i in (*.tif) DO (
if !thumb!==1 (
convert %%i -resize 240x310^^! -quality 80%% "!folder!\thumb.jpg"
)
set thumb=2
convert %%i output\%%~ni.pdf
convert -units pixelsperinch %%i -resample 150 tablet\%%~ni.pdf
)

cd output
pdftk *.pdf "D:\Retro-Work\brought_to_you_by.pdf" cat output "%folder%\_manual.pdf"
cd ..\tablet
pdftk *.pdf "D:\Retro-Work\brought_to_you_by_[TABLET].pdf" cat output "%folder%\_tablet.pdf"
cd ..

del output\*.pdf /q
del tablet\*.pdf /q
rd output
rd tablet

cd %folder%
start _manual.pdf
start _tablet.pdf
pause