#!/bin/bash -x

# Note that on a Pi with a 32Gb SD card, with the free space below:

# Filesystem     1K-blocks     Used Available Use% Mounted on
# /dev/mmcblk0p2  29469820 13243184  14709728  48% /

# we actually run out of disk space - not because the disk is full,
# but because we're out of inodes...:

# Filesystem      Inodes   IUsed IFree IUse% Mounted on
# /dev/mmcblk0p2 1839328 1833567  5761  100% /

# Interim solution... zip up all the tile files. (or use my x86 linux,
# but the noise from the fan when running a big job is very annoying)

# Outside requirements: ocrmypdf pdfimages convert deskew zip

if [ "`which convert`" == "" ] ; then
    echo This suite relies heavily on the program \'convert\' from ImageMagick.
    echo You do not appear to have it installed, so please install that first, using:
    echo "   sudo apt-get install imagemagick"
    exit 1
fi

# # Personal tweak.  lets me browse intermediate results on my home system
if [ "`whoami`" == "gtoal" ] ; then
    # should be safe to leave in.  I doubt my distant relation gerard is likely
    # to be running this code ;-)
    if [[ -L /var/www/html/original || -e /var/www/html/original ]]; then
      echo Skipping creation of symbolic link /var/www/html/original
    else
      ln -s `pwd`/original /var/www/html/original
    fi
    if [[ -L /var/www/html/cropped || -e /var/www/html/cropped ]]; then
      echo Skipping creation of symbolic link /var/www/html/cropped
    else
      ln -s `pwd`/cropped /var/www/html/cropped
    fi
    if [[ -L /var/www/html/level || -e /var/www/html/level ]]; then
      echo Skipping creation of symbolic link /var/www/html/level
    else
      ln -s `pwd`/level /var/www/html/level
    fi
    if [[ -L /var/www/html/grid || -e /var/www/html/grid ]]; then
      echo Skipping creation of symbolic link /var/www/html/grid
    else
      ln -s `pwd`/grid /var/www/html/grid
    fi
    if [[ -L /var/www/html/tiles || -e /var/www/html/tiles ]]; then
      echo Skipping creation of symbolic link /var/www/html/tiles
    else
      ln -s `pwd`/tiles /var/www/html/tiles
    fi
fi

# # Add very poor OCR of text to PDF (not used)
if [ -f Algol_W_Listing_Nov69.pdf ] ; then
    if [ -f AW.pdf ] ; then
        :
    else
        if [ "`which ocrmypdf`" == "" ] ; then
            echo ocrmypdf is not available, so I will not be creating
            echo a searchable version of the listing into AW.pdf.
            echo This is not a critical omission.
            echo ocrmypdf can be installed by issuing the command:
            echo "   sudo apt-get install ocrmypdf"
        else
            echo Creating a searchable version of the listing into AW.pdf
            echo \- this is not used anywhere, it\'s just for your convenience.
            echo This step can be skipped by commenting out the line:
            echo "   ocrmypdf Algol_W_Listing_Nov69.pdf AW.pdf"
            echo "in this script. ($0)"
            ocrmypdf Algol_W_Listing_Nov69.pdf AW.pdf
        fi
    fi
else
    echo The input for this process is file:  Algol_W_Listing_Nov69.pdf
    echo \- please place that file in the working directory, `pwd`
    echo 
    echo You can download the original using:
    echo "  wget https://bitsavers.org/pdf/stanford/listing/Algol_W_Listing_Nov69.pdf"
    exit 1
fi


# # get the individual pages as png files from the original pdf file:
if [ -f original/AW-000.png ] ; then
    echo Skipping extraction of images from Algol_W_Listing_Nov69.pdf with \'pdfimages\'
else
    if [ "`which pdfimages`" == "" ] ; then
        echo "This process requires page images in subdirectory original/AW-*.png"
        echo \- we normally generate these from Algol_W_Listing_Nov69.pdf
        echo using program \'pdfimages\', which can be installed using the command:
        echo "   sudo apt-get install poppler-utils"
        echo Alternatively, you can just place scanned png images directly into
        echo that directory.  They should be named original/AW-000.png et seq.
        exit 1
    fi
    mkdir -p original
    pdfimages -png Algol_W_Listing_Nov69.pdf original/AW
fi

for pfilename in original/AW-*.png; do
    filename="`basename $pfilename .png`"
    if [ -f cropped/$filename.png ] ; then
        echo Skipping cropped/$filename.png
    else
        # Remove perforations and unused edges of page scans:
        mkdir -p cropped
        convert original/$filename.png -crop 5200x4100+180+150 +repage cropped/$filename.png
    fi
    if [ -f level/$filename.png ] ; then
        echo Skipping level/$filename.png
    else
        if [ "`which deskew`" == "" ] ; then
            echo \# It is essential to ensure that all input images are aligned tightly with
            echo \# the horizontal axis.  We fix up images here using the command \'deskew\'
            echo \# which unfortunately does not have an easy installer.  What you need to
            echo \# do is:
            echo ""
            echo wget https://galfar.vevb.net/wp/download/Deskew-1.30.zip
            echo unzip Deskew-1.30.zip
            UNAME="$(uname -s)"
            if [ "${UNAME}" == "" ] ; then
                UNAME="unknown"
            fi
            case "${UNAME}" in
                Linux*)     machine="Linux";;
                Darwin*)    machine="Mac";;
                CYGWIN*)    machine="Cygwin";;
                MINGW*)     machine="MinGw";;
                MSYS_NT*)   machine="Git";;
                *)          machine="${UNAME}"
            esac
            if [ "$machine" == "Linux" ] ; then
                ARCH="`arch`"
                case $ARCH in
                *"86"*)
                    echo sudo cp Deskew-1.30/Bin/deskew /usr/local/bin/deskew;;
                *"arm"*)
                    echo sudo cp Deskew-1.30/Bin/deskew-arm /usr/local/bin/deskew;;
                *"aarch"*)
                    echo sudo cp Deskew-1.30/Bin/deskew-arm /usr/local/bin/deskew;;
                *)
                    echo \# Depending on the system you are using, issue one of:
                    echo sudo cp Deskew-1.30/Bin/deskew /usr/local/bin/deskew
                    echo sudo cp Deskew-1.30/Bin/deskew-arm /usr/local/bin/deskew
                esac
            elif [ "$machine" == "Mac" ] ; then
                 echo sudo cp Deskew-1.30/Bin/deskew-mac /usr/local/bin/deskew
            else
                echo \# Look in Deskew-1.30/Bin and find the executable that is appropriate
                echo "# for your $machine system, and copy it to your bin directory (often /usr/local/bin)"
                echo \# so that it is executed when you type: deskew
            fi
            echo rm -rf Deskew-1.30 Deskew-1.30.zip
            exit 1
        fi

        # Do small rotations of trimmed page to make it absolutely horizontal:
        mkdir -p level
        deskew -f rgb24 -s s -b FFFFFF -l 0.0001 -o level/$filename.png cropped/$filename.png |& fgrep "[deg]"
    fi


done

#make getlines
#cc -Wall -Ofast -o segment segment.c -I/usr/include/netpbm -L/usr/local/netpbm/lib -lnetpbm
#cc -Wall -Wno-unused-but-set-variable -g -o findmean findmean.c -lm
#cc -Wall -o grid2html grid2html.c -lnetpbm
#cc -Wall -o reoffset reoffset.c -I/usr/include/netpbm -L/usr/local/netpbm/lib -lnetpbm -lm
if [ -f  prescan.results ] ; then
    echo Prescan was done previously.  Skipping.
else
    cp /dev/null prescan.results
    for pfilename in level/AW-*.png ; do
        filename="`basename $pfilename .png`"
        convert $pfilename $filename.pnm
        # >&2 echo "$filename"
        # segment.c does part of what getlines used to do -
        # it determines the grid spacing - but it does *not*
        # output any images.
        export LD_LIBRARY_PATH="/usr/local/netpbm/lib"
        ./segment $filename.pnm | tee /dev/stderr >> prescan.results
        rm $filename.pnm
    done
fi
SPACING=( `./findmean < prescan.results` )
echo Using character grid ${SPACING[0]} x ${SPACING[1]}
# We have to re-generate the offsets at the new fixed spacing values,
# then output the gridded page and the lines and the individual chars
mkdir -p grid
mkdir -p tiles
cp /dev/null offset.log
for pfilename in level/AW-*.png ; do
    filename="`basename $pfilename .png`"
    if [ -f grid/${filename}@*@*.png ] ; then
        echo Skipping grid/${filename}@*@*.png
    else
      convert $pfilename $filename.pnm
      export LD_LIBRARY_PATH="/usr/local/netpbm/lib"
      PARAMS=( `./reoffset --xstep ${SPACING[1]} --ystep ${SPACING[0]} $filename.pnm` )
      # AW-003.pnm 66.2 1.600000 63 40.0 0.000000 131
      echo $PARAMS >> offset.log
      # getlines should be renamed something like extract,
      # and have the grid calculation part removed.
      # getlines *used to* do the grid determination,
      # but that was moved to segment.c when it was
      # re-written with a better algorithm.
      echo ./getlines --exact-line-spacing=${SPACING[0]} \
           --exact-column-spacing=${SPACING[1]} \
           --line-offset=${PARAMS[2]} \
           --column-offset=${PARAMS[5]} \
           --grid --lines --tiles \
           -v $filename.pnm
      ./getlines --exact-line-spacing=${SPACING[0]} \
           --exact-column-spacing=${SPACING[1]} \
           --line-offset=${PARAMS[2]} \
           --column-offset=${PARAMS[5]} \
           --grid --lines --tiles \
           -v $filename.pnm
      rm $filename.pnm
      find tiles/$filename -name 'col???.pgm' | while read f ; do
        echo convert $f `dirname $f`/`basename $f .pgm`.png \&\& rm $f
        convert $f `dirname $f`/`basename $f .pgm`.png && rm $f
      done
    fi
    LONGNAME="`basename grid/${filename}@*@*.png .png`"
    if [ -f grid/$filename.html ] ; then
        echo Skipping grid/$filename.html
    else
      convert grid/${filename}*.png ${LONGNAME}.pnm
      ./grid2html ${LONGNAME}.pnm > grid/$filename.html
      rm ${LONGNAME}.pnm
    fi
done

   # if [ -f tiles/$filename.zip ] ; then
   #     echo Skipping zip tiles/$filename.zip
   # else
   #     echo "pushd tiles ; zip -r $filename.zip $filename/line* ; popd"
   #     pushd tiles >& /dev/null ; zip -r $filename.zip $filename/line* ; popd >& /dev/null
   #     rm -rf tiles/$filename/line*
   # fi                               

exit 0
