#!/bin/bash

#
# (C) 2002 D.Kreil@IEEE.Org
#
# usage examples:
#  any2djvu.sh file.ps
#  any2djvu.sh file.ps.gz
#  any2djvu.sh *.ps.gz
#
# for David Kreil's version see
#
#   /home/ftp/pub/www/is/papers/any2djvu.sh
# diff  /home/ftp/pub/www/mackay/any2djvu.sh  /home/ftp/pub/www/is/papers/any2djvu.sh


cat >warn.txt <<EOF

WARNING: Assuming you are running this from  /home/ftp/pub/www/mackay/ ,
	 and that all filenames passed do NOT require URL-encoding.
	 Documents must be in PostScript (.ps, .ps.gz) or PDF (.pdf) format.

NB:	 All files converted at 400dpi with English OCR enabled.

EOF

if [ -z "$1" ]; then
    cat <<EOF
usage:	$0 {filename(s)}

(C) 2002 D.Kreil@IEEE.Org

EOF
    cat warn.txt
    \rm warn.txt
    exit
fi


log=`date -u`": "`whoami`'@'`hostname`", pid $$: $0 (cwd "`pwd`")"

echo '/-- Started' $log >&2

cat warn.txt

in=$1; shift;
while [ -n "$in" ]; do
    b=$in;
    b=${b%.ps.gz}
    b=${b%.ps}
    b=${b%.pdf}
    b=${b%.PDF}
    echo Processing $b ...
    log=$b-any2djvu.log
    wget -O - "http://any2djvu.djvuzone.org/any2djvu.php?urlupload=http://www.inference.phy.cam.ac.uk/mackay/$in&docformat=2&resolution=400&ocr=1&legal=1" |tee $log
    l=`egrep "href=djvu/.*\.djvu" $log`
    l=${l##*href=}
    l=${l%%>*}
    wget -O $b.djvu "http://any2djvu.djvuzone.org/$l"
    ls -l $b.djvu
    in=$1; shift;
done

cat warn.txt

\rm warn.txt

log=`date -u`": "`whoami`'@'`hostname`", pid $$: $0 (cwd "`pwd`")"

echo '\-- Done' $log >&2
