#!/bin/bash
# CVS version control block - do not edit manually
#  $RCSfile: any2djvu.sh,v $
#  $Revision: 1.11 $
#  $Date: 2002/11/14 06:08:47 $
#  $Source: /home/cvs/djvulibre/tools/any2djvu.sh,v $

function copyright()
{
  echo "Copyright (C) 2002 David Kreil <D.Kreil@IEEE.Org>"
  echo "Modified by Barak A. Pearlmutter <bap@debian.org>"
  echo 'CVS $Revision: 1.11 $'
  echo "Released under the GNU GPL v2, 21-Oct-2002."
}

function warranty()
{
  echo "This program is distributed in the hope that it will be useful,"
  echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
  echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
  echo "GNU General Public License for more details."
}

# TO DO:
#  - command line options for resolution, ocr, urls
#  - error handling

rurl="http://any2djvu.djvuzone.org"
rcgi="any2djvu.php"
res=400
ocr=1
docformat=2
cgiopts="&docformat=$docformat&resolution=$res&ocr=$ocr"

function warn()
{
  echo "Notes:"
  echo " - Filenames are assumed to require no URL-encoding."
  echo " - Documents must be PostScript (.ps, .ps.gz) or PDF (.pdf)."
  echo " - Conversion is at 400dpi, with English OCR enabled."
  echo " - This script should not be used for large scale conversions of"
  echo "   documents, as it may badly affect and hence endanger the free web"
  echo "   service to the community."
  echo " - This software comes with NO WARRANTY."
}

function usage()
{
  echo "Convert files from .ps/.ps.gz/.pdf to .djvu"
  echo "Usage:	$0 url {filename(s)}"
  echo "Must be run from a web-accessible directory."
  echo "First argument is the external base-URL for that directory."
  echo "Examples:"
  echo "  cd ~bap/public_html/foo"
  echo "  any2djvu.sh http://www.cs.unm.edu/~bap/foo file.ps"
  echo "  any2djvu.sh http://www.inference.phy.cam.ac.uk/mackay *.ps.gz bar.pdf"
}

lurl="$1"; shift
if [ -z "$lurl" ]; then
  echo "error: no external URL specified"
  usage
  exit 1
fi

log=`date -u`": "`whoami`'@'`hostname`", pid $$: $0 (cwd "`pwd`")"
echo '/-- Started' $log >&2

in="$1"; shift;
if [ -z "$in" ]; then
  echo "error: no files to convert"
  usage
  exit 1
fi

copyright
warn

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 - "$rurl/$rcgi?urlupload=$lurl/$in$cgiopts" | tee $log
    l=`egrep "href=djvu/.*\.djvu" $log`
    l=${l##*href=}
    l=${l%%>*}
    wget -O $b.djvu "$rurl/$l"
    ls -l $b.djvu
    in="$1"; shift;
done

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

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