#!/bin/sh
#!/bin/sh5
# originally:
# From: mfullmer@magnus.acs.ohio-state.edu (Mark A Fullmer)
# "This may still work for vtsomething's, it may not.  Once it did..."
#
# Heavily modified by Rob Funk, rfunk@magnus.acs.ohio-state.edu)
# Added portability, so it might even work on terminals besides
# "vtsomethings".
#
# Let me (rfunk) know if there are any other terminal types I should
# add to this.

PATH=/usr/5bin:/usr/bin:/bin;
echo="echo -e"
PORTABLE=1

# form feed is standard for printers...
ff="\014"
esc="\033"
csi="${esc}["

if [ $PORTABLE -eq 0 ]
then
  #$echo "Using defaults (vt102/vt220)"
  pf="${csi}4i"
  po="${csi}5i"
else
  #$echo "Using termcap..."
  # actually, tput likes the terminfo codes (e.g. mc4 instead of pf)
  pf=`tput mc4`
  po=`tput mc5`
  if [ -z "$pf" -o -z "$po" ]
  then
    $echo "Termcap is clueless, so it's up to me..."
    # just assign them based on the terminal type -- termcap is dumb
    case $TERM in
    # vt100/vt102/vt220 and similar
    # include wyse 75/85
    vt10? | vt2?? | wy*[78]5)
      pf="${csi}4i"
      po="${csi}5i"
      ;;
    # some xterm emulators can handle this the same as vt102
    xterm)
      pf="${csi}4i"
      po="${csi}5i"
      ;;
    vt52)
      pf="${esc}X"
      po="${esc}W"
      ;;
    # dec vt300/320, AT&T 615/620/630
    vt3[02]0* | {att,dmd}6[123][05] | 6[123][05]{att,dmd})
      pf="${csi}?4i"
      po="${csi}?5i"
      ;;
    # televideo 955
    *955*)
      pf="${esc}a"
      po="${esc}\`"
      ;;
    # wyse
    wy*[356]0* | wy*99*)
      # ^T/^R
      pf="\024"
      po="\022"
      ;;
    # default -- ask if they want the default codes
    *)
      $echo "Sorry, I don't know how to print to your $TERM terminal"
      $echo "Would you like to try the default (vt102/vt220) codes? [ny]"
      # be sure to read from tty -- we might be getting the file from stdin
      read yn < /dev/tty
      # transform to one character, lower case
      yn=`echo $yn | cut -c 1 | tr [A-Z] [a-z]`
      if [ "x$yn" = "xy" ]
      then
        $echo "Using default codes..."
        pf="${csi}4i"
	po="${csi}5i"
      else
        echo "OK, then I give up."
	exit 1
      fi
      ;;
    esac
  fi # [ -z "$pf" -o -z "$po" ]
fi # [ $PORTABLE -eq 0 ]


myexit() { $echo $pf; $echo "Print Aborted!" ;exit;}
trap myexit 1 2 3 15

$echo Sending data to printer port...
# switch from screen mode to printer mode
$echo $po

# Finally do some real work
if [ $# -eq 0 ]
then
  cat
else
  while [ $# -gt 0 ]
  do
    cat $1
    # form feed after each file:
    $echo $ff
    shift
  done
fi

# switch back from printer mode to screen mode
$echo $pf
echo Done...
