#!/bin/sh
#
# Polls PJL options from local or network printers
#

#
# Till Kamppeter (till.kamppeter@gmail.com)
#
# Copyright 2001 Till Kamppeter
#
# This software may be freely redistributed under the terms of the GNU
# General Public License (http://www.gnu.org/).
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Modifed by Patrick Powell <papowell at astart.com>
#   - error messages, fixed problem with netcat
#   - increased timeout, added a job end to sequence
#     as this was needed by a couple of printers

NC=/usr/bin/nc
CAT=/bin/cat
PRINTF=/usr/bin/printf
PERL=/usr/bin/perl

usage(){
    cat <<EOF ;
usage: foomatic-getpjloptions <device>
	   foomatic-getpjloptions <hostname> <port>

   <device>:   Device where a local printer is connected to
			   examples: /dev/lp0, /dev/usb/lp0
			   Have your parallel port in EPP/bi-directional mode
			   (see your BIOS settings).
   <hostname>: Host name or IP of a network printer (HP JetDirect,
			   Socket, ...)
   <port>:     Port of your network printer.
echo
   Uni-directional protocols as remote LPD are not supported.

   The option list is sent to standard output.
EOF
	exit 1;
}

case "$1" in
	-* ) usage;;
	""	) usage;;
esac

    # We have at least one argument, so do the work
    (
	# PJL commands to request the printer information
	$PRINTF "\033%%-12345X"
	$PRINTF "@PJL\r\n"
	$PRINTF "@PJL INFO VARIABLES\r\n"
	$PRINTF "@PJL INFO ID\r\n"
	$PRINTF "@PJL INFO CONFIG\r\n"
	$PRINTF "\033%%-12345X"
    ) | if [ ${2:-X} != X ]; then
	# We have two arguments, do ethernet printer request
	# Poll the printer's answer and filter out the newpage characters
	${NC} -w 3 ${1} ${2} 2>/dev/null | ${PERL} -p -e "s/\014//"
    else
	# We have one argument, do local printer request
	# Send commands to printer port
	${CAT} > ${1}
	# Wait ten seconds for the printer to process the request
	sleep 10
	# Poll the printer's answer and filter out the newpage and CR characters
	${CAT} < ${1} | ${PERL} -p -e "s/[\015\014]//"
    fi
