#!/bin/zsh
#
# (C) Thomas Khler <jean-luc@picard.franken.de>
# Released under GPL Version 2

## We need a pdf file to convert. Currently supported only files with ending .pdf (all lowercase)
if [ -z "$1" ] ; then
echo usage: $0 "path/to/filename.pdf"
exit 1
fi

if [ -z "$TMPDIR" ] ; then
    TMPDIR=/tmp
fi
export TMPDIR

mkdir -p $TMPDIR || exit 2
mkdir $TMPDIR/plucker-$$ || exit 3
cp "$1"  $TMPDIR/plucker-$$ || exit 4

cd $TMPDIR/plucker-$$ || exit 5


file=${1:s/.pdf//}
file=`basename "$file"`
mv -f $file.pdf ${${file:gs/ /_/}:gs/-//}.pdf
file=${${file:gs/ /_/}:gs/-//}


## convert the .pdf to html
pdftohtml -c *

### We need this file palmcolor8.map in this directory.
cp /usr/share/netpbm/palmcolor8.map .

### correct some links so that plucker-build understands them correctly
sed -e "s,href=\",href=\"file://$PWD/,"  ${file}_ind.html >  ${file}_ind1.html

### convert the html to plucker format
plucker-build --pluckerdir="$TMPDIR" --bpp=8 --doc-file=$file --depth-first ${file}_ind1.html
