#! /bin/sh
#
# Anytog3 : converts various file formats to g3 (fax graphical format)
#
# J.-P. Demailly, Dec 1998
#

PATH=

TYPE=$1

#
# Help!
#
if test "$TYPE" = "-h" || test "$TYPE" = "" ; then
  echo "Usage: anytog3 [type] [tmpdir] [input] [output_radix] [options]
Options: specify geometry, e.g. [2-7]:134%+25+35mm
         meaning that pages 2-7 of the document are rescaled by factor 134%
         and shifted by 25mm to the right, 35mm downwards"
  exit
fi

TMPDIR=$2
INPUTFILE=`echo $3`
OUTRADIX=$2/`echo $4`
shift 4
OPTIONS="`echo $*`"

#
# Procedure which parses the geometry specification
#
geometry()
{

if test "$OPTIONS" = "" ; then
  exit
fi

i=0

while :
do

  i=$[$i+1]
  if test "$[$i<10]" = "1" ; then
     ii=00$i
  elif test "$[$i<100]" = "1" ; then
     ii=0$i
  else
     ii=$i
  fi

  if [ ! -r $OUTRADIX.$ii ] ; then
     exit
  fi 

  rest=""

  for j in $OPTIONS
  do
    num=`echo $j | cut -d"]" -f 1`
    i1=`echo $num | cut -d"-" -f 1 | cut -d "[" -f 2`
    i2=`echo $num | cut -d"-" -f 2`

    if test "$[($i>=$i1)*($i<=$i2)]" = "1" ; then
       rest=`echo $j | cut -d":" -f 2`
    fi
  done

  if test "$rest" != "" ; then
    perc=`echo $rest | cut -d"%" -f 1`
    perc1=$[$perc/100]
    perc2=$[$perc-100*$perc1]
    if test "$[$perc2<10]" = "1" ; then
       perc2=0$perc2
    fi
    perc="$perc1.$perc2"
    xymove=`echo $rest | cut -d"%" -f 2`

    mv $OUTRADIX.$ii $OUTRADIX.$ii.orig
    efix -i fax -o fax -s${perc}x${perc} \
         -d$xymove $OUTRADIX.$ii.orig > $OUTRADIX.$ii
    rm -f $OUTRADIX.$ii.orig
  fi

done
}

#
# Already a fax file!
#
if test "$TYPE" = "fax" || test "$TYPE" = "g3" ; then
  cp $INPUTFILE $OUTRADIX.001
  geometry
  exit
fi

#
# PostScript files
#
if test "$TYPE" = "ps" ; then
  gs -q -sDEVICE=faxg3 -dNOPAUSE -sPAPERSIZE=a4 \
        -sOutputFile=$OUTRADIX.%03d $INPUTFILE quit.ps $OPTIONS
  geometry
  exit
fi

#
# PDF files
#
if test "$TYPE" = "pdf" ; then
  pdftops $INPUTFILE - | gs -q -sDEVICE=faxg3 -dNOPAUSE \
     -sPAPERSIZE=a4 -sOutputFile=$OUTRADIX.%03d - quit.ps $OPTIONS
  geometry
  exit
fi

#
# LaTeX files
#
if test "$TYPE" = "ltx" ; then
  TEXCMD="latex"
fi

#
# TeX files
#
if test "$TYPE" = "tex" ; then
  TEXCMD=`tail -n 20 $INPUTFILE \
       | grep "TeX-command-default" | cut -d":" -f 2 \
       | sed -e "/\"/s///g" | tr A-Z a-z`
  TEXCMD=`echo $TEXCMD`
  if test "$TEXCMD" = "" ; then
    TEXCMD="tex"
  fi
fi

#
# TeX or LaTeX files
#
if test "$TEXCMD" != "" ; then
  INDEX=`echo $OUTRADIX | cut -d"_" -f 2`
  TMPTEXFILE=$TMPDIR/texdoc_$INDEX.tex
  DVIFILE=$TMPDIR/texdoc_$INDEX.dvi
  rm -f $TMPTEXFILE
  ln -s $INPUTFILE $TMPTEXFILE
  (
   cd $TMPDIR
   $TEXCMD \\nonstopmode \\input $TMPTEXFILE
   if test "$TEXCMD" = "latex" ; then
      $TEXCMD \\nonstopmode \\input $TMPTEXFILE
   fi
  )
fi

#
# dvi files
#
if test "$TYPE" = "dvi" ; then
  DVIFILE=$INPUTFILE
fi

if test "$DVIFILE" != "" ; then
  dvips -t a4 $DVIFILE -O 0cm,1.2cm -o "!cat" | \
  gs -q -sDEVICE=faxg3 -dNOPAUSE -sPAPERSIZE=a4 -sOutputFile=$OUTRADIX.%03d -
  geometry
  exit
fi

#
# Various image formats
# Uses the "convert" utility from the ImageMagick package
# If you don't have ImageMagick, you can use e.g. something like
#
# giftoppm $INPUTFILE | ppmtopgm | pgmtopbm | pbmpscale 2 | pbmtog3 \
#     > $OUTRADIX.001
#
# to process gif files. But this is of course not very nice...
#

if test "$TYPE" = "image" ; then
   convert $INPUTFILE fax:$OUTRADIX.001
   geometry
   exit
fi

#
# Formatting option (assumes that the file is a text file...)
#
case "$OPTIONS" in
*@*)
   enscript $INPUTFILE -1 -R --header='||' -M A4 -f $OPTIONS -o - | \
   gs -q -sDEVICE=faxg3 -dNOPAUSE -sPAPERSIZE=a4 \
         -sOutputFile=$OUTRADIX.%03d - quit.ps $OPTIONS
   exit
   ;;
esac

#
# Default: ascii text files
#
efix -ve -itext -ofax -p21x29.7cm -d-0.3,0.5 -l56 -s1x1 -n $OUTRADIX.%03d \
  $INPUTFILE
geometry
