#!/bin/bash

LAT=`pdftotext -W 130 -H 50 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | head -n 1`
LON=`pdftotext -W 130 -H 50 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | tail -n 1`

#echo LAT: $LAT LON: $LON

SIZE=`pdfinfo $1 | grep "Page size"`
XMAX=`echo $SIZE | sed 's/.*: *\([0-9]*\) x \([0-9]*\).*/\1/'`
YMAX=`echo $SIZE | sed 's/.*: *\([0-9]*\) x \([0-9]*\).*/\2/'`

#echo Size: $XMAX x $YMAX points

XMAX2=`expr $XMAX - 130`
YMAX2=`expr $YMAX - 1000`

LAT2=`pdftotext -x $XMAX2 -y $YMAX2 -W 1300 -H 1000 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | head -n 1`
LON2=`pdftotext -x $XMAX2 -y $YMAX2 -W 1300 -H 1000 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | tail -n 1`

#echo LAT2: $LAT2 LON2: $LON2

LAT3=`pdftotext -W 130 -y $YMAX2 -H 1000 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | head -n 1`
LON3=`pdftotext -W 130 -y $YMAX2 -H 1000 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | tail -n 1`

#echo LAT3: $LAT3 LON3: $LON3

LAT4=`pdftotext -H 50 -x $XMAX2 -W 1300 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | head -n 1`
LON4=`pdftotext -H 50 -x $XMAX2 -W 1300 $1 - | grep -e '[0-9][0-9][0-9][0-9]' | tail -n 1`

#echo LAT4: $LAT4 LON4: $LON4

#echo `expr $LAT2 - $LAT`   , `expr $LON - $LON2`

if [ `expr '(' $LAT2 - $LAT '>' 1000 ')' '*' '(' $LAT2 - $LAT '<' 20000 ')'` -eq 1 ]
then
    FOUNDLAT1=$LAT
    FOUNDLAT2=$LAT2
fi

if [ `expr '(' $LAT2 - $LAT3 '>' 1000 ')' '*' '(' $LAT2 - $LAT3 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLAT1=$LAT3
    FOUNDLAT2=$LAT2
fi

if [ `expr '(' $LAT4 - $LAT '>' 1000 ')' '*' '(' $LAT4 - $LAT '<' 20000 ')'` -eq 1 ]
then
    FOUNDLAT1=$LAT
    FOUNDLAT2=$LAT4
fi

if [ `expr '(' $LAT4 - $LAT3 '>' 1000 ')' '*' '(' $LAT4 - $LAT3 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLAT1=$LAT3
    FOUNDLAT2=$LAT4
fi

if [ `expr '(' $LON - $LON2 '>' 1000 ')' '*' '(' $LON - $LON2 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLON1=$LON
    FOUNDLON2=$LON2
fi

if [ `expr '(' $LON4 - $LON2 '>' 1000 ')' '*' '(' $LON4 - $LON2 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLON1=$LON4
    FOUNDLON2=$LON2
fi

if [ `expr '(' $LON4 - $LON3 '>' 1000 ')' '*' '(' $LON4 - $LON3 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLON1=$LON4
    FOUNDLON2=$LON3
fi

if [ `expr '(' $LON - $LON3 '>' 1000 ')' '*' '(' $LON - $LON3 '<' 20000 ')'` -eq 1 ]
then
    FOUNDLON1=$LON
    FOUNDLON2=$LON3
fi

if [ "$FOUNDLON1" = "" ]
then
    echo Could not find longitude lower bound!
    exit 1
fi
if [ "$FOUNDLON2" = "" ]
then
    echo Could not find longitude upper bound!
    exit 1
fi
if [ "$FOUNDLAT1" = "" ]
then
    echo Could not find latitude lower bound!
    exit 1
fi
if [ "$FOUNDLAT2" = "" ]
then
    echo Could not find latitude upper bound!
    exit 1
fi

BASEN=`basename $1 .pdf`

gs -q -sDEVICE=png48  -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 -r200 -sOutputFile=$BASEN"_tmp.png" $1
convert -trim $BASEN"_tmp.png" $BASEN".png"

gdal_translate  -co "COMPRESS=LZW" -co "TILED=YES" -a_srs EPSG:29193 -a_ullr $FOUNDLAT1 $FOUNDLON1 $FOUNDLAT2 $FOUNDLON2 -of GTiff $BASEN".png" $BASEN".tif"

mkdir $BASEN

gdal_retile.py -ps 1024 1024 -targetDir $BASEN $BASEN".tif"
