#!/bin/sh


#note, using a patched version of abiword 2.2.7 with filetype override on commandline.
# --useextension=.txt

#v1.0.6
#abiword-2.4.1 has --import-extension, but it doesn't work.
#so, for now, using nenscript...

PAPERSIZE="`cat /etc/pdq/printrc|grep "driver_args"|head -n 1|cut -f 2 -d '='|tr -d '}'|tr -d '"'|tr -d ' '`"

#v1.0.3
#if the nenscript pkg installed, assume no other way to convert text file to postscript...
NENPAPER="A4"
if [ "$PAPERSIZE" = "letter" ];then
 NENPAPER="US"
fi
if [ -f /usr/sbin/nenscript ];then
 if [ "$1" ];then
  file -b $1 | grep -i "postscript"
 else #to handle piped input.
  false
 fi
 if [ $? -eq 0 ];then
  lpr $1
 else
  if [ "$1" ];then
   PSFILE="`basename $1`"
  else
   PSFILE="tempprintfile"
  fi
  #note, if no $1 then nenscript accepts stdin...
  /usr/sbin/nenscript -p/tmp/$PSFILE.ps -T${NENPAPER} ${1}
  sync
  lpr /tmp/$PSFILE.ps
 fi
 exit
fi

TEXTFILE="`basename $1`"
PATHFILE="$1"

PRINFO="INFORMATION:
This application is using /usr/bin/lprshell to convert
the file to Postscript prior to sending it to lpr.
In the case of plain text, ABW, DOC and RTF files, 
Abiword is used as the filter. Abiword can be invoked from
the commandline to convert these formats to Postscript.

Note that lpr is a symlink to pdq, which will call
gs (Ghostscript) to print the file.
After a short delay, your file should print.
If it does not, did you run the Puppy Printer Wizard?
Then, did you run XPDQ and choose the printer as the
default printer?"
xmessage -timeout 120 -bg "#ff00ff" -title "lprshell information" -buttons "" "$PRINFO" &

EXTRATXT=""
OVERRIDEOPTION=""
file -b $1 | grep -i "postscript"
if [ $? -eq 0 ];then
 cp -af $1 /tmp/$TEXTFILE
else

 echo "$TEXTFILE" | grep -i ".txt"
 if [ ! $? -eq 0 ];then
  echo "$TEXTFILE" | grep -i ".rtf"
  if [ ! $? -eq 0 ];then
   echo "$TEXTFILE" | grep -i ".doc"
   if [ ! $? -eq 0 ];then
    echo "$TEXTFILE" | grep -i ".abw"
    if [ ! $? -eq 0 ];then
     #treat file as plain text...
     cp -af $1 $1.txt
     PATHFILE="$1.txt"
     EXTRATXT="yes"
     OVERRIDEOPTION='--useextension=.txt'
    fi
   fi
  fi
 else
  OVERRIDEOPTION='--useextension=.txt'
 fi

 #Ted ++printToFilePaper $PATHFILE /tmp/$TEXTFILE.ps $PAPERSIZE
 abiword --print=/tmp/$TEXTFILE.ps $OVERRIDEOPTION $PATHFILE
 TEXTFILE="$TEXTFILE.ps"
 if [ "$EXTRATXT" = "yes" ];then
  rm -f $1.txt
 fi
fi

sync
sleep 2
lpr /tmp/$TEXTFILE

#rm -f /tmp/$TEXTFILE
#killall xmessage
