#!/bin/sh

case "$1" in
  [1-4])	tray=`expr $1 - 1` ;;
  man*)		tray=manual ;;
  *)		echo Usage: "$0 TrayNumber(0-4|manual)" 1>&2
  		exit 1 ;;
esac

case $tray in
  [0-3])
    priority="[ "
    for i in 0 1 2 3; do
      priority="$priority `expr \( $tray + $i \) % 4`"
    done
    priority="$priority ]"
    
    if ! read line; then
      echo Read error 1>&2
      exit 1
    fi
    echo "$line"
    cat<<_EOF_
[{
%%BeginFeature: *InputSlot Tray $1

currentpagedevice /InputAttributes get dup $tray known
{$tray get dup null eq
{ pop }
{ dup length 1 add dict copy
dup /InputAttributes
1 dict dup /Priority $priority put
put setpagedevice
} ifelse }
{pop}
ifelse
%%EndFeature
} stopped cleartomark
_EOF_
	;;
  man*)
    cat<<_EOF_
%%BeginFeature: *ManualFeed True
1 dict dup /ManualFeed true put setpagedevice
%%EndFeature
} stopped cleartomark
_EOF_
	;;
esac
cat
