#!/bin/sh
if [ "$#" -ne 1 ]
then echo "Usage: $0 pdf_file"; exit 1
fi

# Get the base of the filename
base=`echo $1 | sed 's/\.[Pp][Dd][Ff]//`
echo $base

# Run the pdftohtml converter
pdftohtml -raw -reflow -noframes -enc Latin1 $1

# Rescale all of the jpg images for uBook
for i in $base*.jpg
do echo Scaling $i; convert $i -resize '750x400>' $$.jpg && mv $$.jpg $i
done

# Rescale  and convert all of the png images for uBook into jpg
for i in $base*.png
do echo Scaling $i;
   j=`basename $i .png`.jpg
   convert $i -resize '750x400>' $j && rm $i
done

# Rewrite the .png in the HTML file to .jpg
perl -pi -e 's{.png"}{.jpg"}' $base*.html

# Zip up the HTML and jpeg files
zip -9 $base.zip $base*.html $base*.jpg

# And remove the HTML and jpeg files
rm $base*.html $base*.jpg
exit 0
