@echo off
REM V2.0 Updated 2023-02-04 Tomse
REM Moved to using Tesseract OCR

set source=G:\Retro-Work

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

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

choice /C LSN /m "Use large (L), small (S) or no (N) lastpage  "
if %errorlevel%==1 (
set page=brought_to_you_by.pdf
) else if %errorlevel%==2 (
set page=brought_to_you_by_[TABLET].pdf
) else if %errorlevel%==3 (
set page=""
)

choice /C DEG /m "(D)anish, (E)nglish, (G)erman "
if %errorlevel%==1 (
set lang=dan
) else if %errorlevel%==2 (
set lang=eng
) else if %errorlevel%==3 (
set lang=deu
)




if NOT EXIST output mkdir output
set thumb=1

for %%i in (*.tif) DO (
if !thumb!==1 (
magick convert %%i -resize 240x310^^! -quality 80%% "!folder!\thumb.jpg"
)
set thumb=2
start tesseract %%i output\%%~ni -l %lang% pdf
)
echo Wait till all other screens are gone before continuing
pause

cd output
pdftk *.pdf "%source%\%page%" cat output "%folder%\manual.pdf"

cd %folder%
mkdir temp

echo [ /Title () > temp\pdfmarks.txt
echo  /Author (Scanned and Processed by Tomse @ http://retro-commodore.eu) >> temp\pdfmarks.txt
echo  /Subject () >> temp\pdfmarks.txt
echo  /Keywords (RCEU, Commodore) >> temp\pdfmarks.txt
echo  /ModDate (D:%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%) >> temp\pdfmarks.txt
echo  /CreationDate (D:%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%) >> temp\pdfmarks.txt
echo  /Creator (retro-commodore.eu) >> temp\pdfmarks.txt
echo  /Producer (retro-commodore.eu) >> temp\pdfmarks.txt
echo  /DOCINFO pdfmark >> temp\pdfmarks.txt



if exist manual.pdf pdftotext manual.pdf manual2.ocr.txt

if exist manual.pdf gswin64c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=manual2.pdf "manual.pdf" temp\pdfmarks.txt
if exist mag_recognized.pdf gswin64c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="[300dpi].pdf" "mag_recognized.pdf" temp\pdfmarks.txt
if exist _tablet_recognized.pdf gswin64c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="[150dpi][ocr].pdf" "_tablet_recognized.pdf" temp\pdfmarks.txt
if exist audiomedia_recognized.pdf gswin64c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="Privat_Computer_199x_-_xx.pdf" "audiomedia_recognized.pdf" temp\pdfmarks.txt
cls

start manual2.pdf 

choice /m "Delete old files?"

if %ERRORLEVEL% EQU 1 goto delete
goto end

:delete
del manual.pdf

:end