#!/bin/bash
# (c) 2001 Karel 'Clock' Kulhavy
# Toggle invert (several lines below) if you want to change black on white
# to white on black.

filter=`which pbm2png`
ghostscript=`which gs`
dpi=70
basename='doses'
titlestring='Lecture notes Distributed Systems'
bottom='<hr>Send comments to <a href="mailto:clock@atrey.karlin.mff.cuni.cz">Clock</a>'
pageoffset='0'
invert='0'
start=0
pname="/tmp/.pdf2html_pipe_$$"

usage()
{
    echo "\
Usage: pdf2html [--dpi <DPI>] [--basename <BASENAME>] [--titlestring <TITLE>]
                [--bottom <BOTTOM>] [--pageoffset <PAGEOFFSET>]
               [--startpage <page_number>] <the_pdf>

       --dpi           - sets dpi (default is 10)
       --basename      - base name of files (default is 'doses')
       --titlestring   - title of HTML page 
                         ('Lecture notes Distributed Systems' is default)
       --bottom        - bottom comment in HTML page (default is set to:
                         '<hr>Send comments to 
                         <a href=\"mailto:clock@atrey.karlin.mff.cuni.cz\">
                         Clock</a>')
       --pageoffset
       --startpage     - starting page (default is 0). Note thate the first
                         page is 0 page!
"
}

if [ "$#" = 0 ]; then
    usage;
    exit 1
fi

while test $# -gt 0 ; do
    case "${1}" in
       --dpi )
           shift; dpi="${1}"; shift ;;
       --basename )
           shift; basename="${1}"; shift ;;
       --titlestring )
           shift; titlestring="${1}"; shift ;;
       --bottom )
           shift; bottom="${1}"; shift ;;
       --pageoffset )
           shift; pageoffset="${1}"; shift ;;
       --startpage )
           shift; start="${1}"; shift ;;
       * )
           pdf_file="${1}"; shift ;;
    esac
done

PATH=$PATH:./
hdpi=`expr $dpi '*' 17`
vdpi=`expr $dpi '*' 15`
start1=`expr $start + 1`

function delete () { rm -f $pname; }
trap delete SIGINT

rm -f $pname
mkfifo $pname
$ghostscript -q -dFirstPage=$start1 -dNOPAUSE -dBATCH -sDEVICE=pbmraw -sOutputFile="$pname" -r"$hdpi"x"$vdpi" $pdf_file &
$filter "$dpi" "$basename" "$titlestring" "$bottom" "$pageoffset" "$pname" $start $invert
cp /var/lib/pdf2html/left.png .
cp /var/lib/pdf2html/right.png .
cp /var/lib/pdf2html/idx.png .
rm -f $pname

