#!/bin/bash -login
# Nov 1999                                    Petr Olsak

user=`echo $1 | cut -dA -f2 | cut -d@ -f1`
user=`/usr/bin/cnvuser $user`
userhost=`echo $1 | cut -dA -f2`

dd of=/tmp/pprint.$$ count=1 bs=2 2> /dev/null

if echo -n '%!' | cmp - /tmp/pprint.$$ > /dev/null ; then
# The PrintJob is in PostScript, we insert a duplex:
  echo `date '+%X, %d.%m %Y'`: $userhost "PS-duplex" >> /var/log/fs1-use
  cat /usr/bin/duplex.ps /tmp/pprint.$$ - /etc/endjob | lpr -U $user -Pfs1raw
  rm /tmp/pprint.$$
  exit
fi
if grep '[--]' /tmp/pprint.$$ > /dev/null; then
# The PrintJob contains raw data from Windows, no changes are possible:
  echo `date '+%X, %d.%m %Y'`: $userhost "MS-Win" >> /var/log/fs1-use
  cat /tmp/pprint.$$ - | lpr -U $user -Pfs1raw
  rm /tmp/pprint.$$
  exit
fi

cat /tmp/pprint.$$ - > u$$.txt

if cat u$$.txt | tr 'X\000-\010\016-\031\033-\037' 'B[X*26]' |\
   grep -l X >/dev/null; then
#  Binary data?
   echo `date '+%X, %d.%m %Y'`: $userhost "Binary" >> /var/log/fs1-use
   lpr -U $user -Pfs1raw u$$.txt    
   rm /tmp/pprint.$$ u$$.txt
   exit
fi

# The PrintJob contains ASCII text, we will test encoding of CZ
if cat u$$.txt | tr 'X\200-\377' 'B[X*128]' | grep -l X >/dev/null; then
   if cat u$$.txt | tr \
      'X\200-\214\216-\227\231-\233\241-\244\246-\250\252\254-\255\257-\264\266\267\272\274\275\277\302\303\306\307\312\313\316\320\321\325\327\333\336\337\342\343\346\347\352\353\356\360\361\365\367\373' 'B[X*128]' |\
      grep -l X >/dev/null; then
      if cat u$$.txt | tr 'X\260-\377' 'B[X*128]' |\
        grep -l X >/dev/null; then
        trfile=il2-cs
      else
        trfile=kam-cs
      fi
   else
      trfile=il2-cs
   fi
else
   echo US ASCII text
   trfile=il2-cs
fi


# We make PostScript via TeX
ln -s /usr/local/texmf/tex/plain/misc/verbtisk.tex u$$.tex
/usr/local/bin/tex -fmt=csplain -translate-file=$trfile \\def\\user{$userhost}\\def\\file{u$$.txt}\\input u$$ \\act\\end >/dev/null
echo `date '+%X, %d.%m %Y'`: $userhost "Plain-text" [$trfile] >> /var/log/fs1-use
(/usr/local/bin/dvips -q -h duplex.ps -o - u$$; cat /etc/endjob) | lpr -U $user -Pfs1raw >> /var/log/fs1-use 2>&1
rm /tmp/pprint.$$ u$$.tex u$$.dvi u$$.log u$$.txt


