#!/bin/sh

#
# Copyright 1993 Network Computing Devices, Inc.
#
# Permission to use, copy, modify, distribute, and sell this software and
# its documentation for any purpose is hereby granted without fee, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name Network Computing Devices, Inc. not be
# used in advertising or publicity pertaining to distribution of this 
# software without specific, written prior permission.
# 
# THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
# LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
# COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
# OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
# WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# $NCDId: @(#) lpncd.sh /main/ncdware/5 11/07/97 14:11:37 gmi @(#) Exp $
#
#  lpncd [-d mask] [-i indent] [-l length] [-w width] [-r] \
#	 [-o] [-s serial_portnum | -p parallel_portnum] [-x] \
#	 [-T printer_type] [-n network_port] [-h ncd_name] filename...
#
#	printer_type = as | lj | dj | ps
#		default is ps
#
#  Print utility for printing directly to a printer attached to an NCD
#  terminal.  This script mainly just parses the input arguments.  The
#  real work is done by printer-specific scripts with filename suffixes
#  of .print
#

READN=ncdreadn
CHR=ncdchr

# Remember who we are.
IAM=`basename $0`
PATH=`dirname $0`:$PATH
export PATH

# Define spooler dictated exit code values.
EXIT_OK=0
EXIT_USAGE=1

# Initialize parameter values.
printer_type="lw"
options=""
port_type="s"
port_num="1"
x_txport=0
pstype="ps"
texttype="text"
terminal=$DISPLAY

# Process arguments.
#set XXX `getopts \?d:i:l:w:roxT:s:p:n:h:u: *`

print_usage() {
    echo "Usage: $IAM [-i indent] [-l length] [-w width] [-r] [-o]" >&2
    echo "       [-s serial_portnum | -p parallel_portnum] [-T printer_type]" >&2
    echo "       [-n network_port] [-h ncd_name] [ -u user@host ] [-x] filename..." >&2
}

if [ "X$1" = "X-help" ]
then
    print_usage
    exit $EXIT_USAGE
fi

while getopts \?d:i:l:w:roxT:s:p:n:h:u: c
do
    case $c in
	r)
	    options="${options} -r"
	    ;;
	o)
	    pstype="raw"
	    texttype="raw"
	    ;;
	x)
	    options="${options} -x"
	    x_txport=1
	    ;;
	T)
	    printer_type=${OPTARG}
	    ;;
	n)
	    net_port=${OPTARG}
	    ;;
	s)
	    port_type="s"
	    port_num=${OPTARG}
	    ;;
	p)
	    options="${options} -p"
	    port_type="p"
	    port_num=${OPTARG}
	    ;;
	h)
	    terminal=${OPTARG}
	    ;;
	i|w|l)
	    if echo ${OPTARG} | egrep '[^0-9]' > /dev/null
	    then
		echo "$IAM: Invalid parameter \"${OPTARG}\" for \"-$c\"" >&2
		print_usage
		exit $EXIT_USAGE
	    fi
	    options="${options} -$c ${OPTARG}"
	    ;;
	\?)
	    print_usage
	    exit $EXIT_USAGE
	    ;;
	*)
	    options="${options} -$c ${OPTARG}"
	    ;;
    esac
done
shift `expr $OPTIND - 1`

case "${printer_type}" in
    du | as)
	printer_type="ascii"
	;;
    lj | dj | lw | ascii)
	;;
    ps)
	printer_type="lw"
	;;
    *)
	echo "$IAM: Invalid printer type. Choices = [ as | lj | dj | ps ]" >&2
	print_usage
	exit $EXIT_USAGE
esac

if [ "X${terminal}" = "X" ]
then
    echo $IAM: Terminal name not given >&2
    print_usage
    exit $EXIT_USAGE
fi
if [ ${x_txport} -eq 0 ]
then
    ncd_name=`echo ${terminal} | sed 's/:.*//'`
else
    ncd_name=${terminal}
fi

if [ "X${net_port}" = "X" ]
then
    if [ ${x_txport} -eq 0 ]
    then
	case "${port_type}${port_num}" in
	    s1)
		net_port=87
		;;
	    s2)
		net_port=5962
		;;
	    s3)
		net_port=5963
		;;
	    p1)
		net_port=5964
		;;
	    p2)
		net_port=5965
		;;
	    *)
		echo $IAM: Invalid serial/parallel port given >&2
		print_usage
		exit $EXIT_USAGE
		;;
	esac
    else
	if [ "${port_type}" = "s" ]
	then
	    net_port="seriald_${port_num}"
	else
	    net_port="paralleld_${port_num}"
	fi
    fi
fi

if [ $# -gt 0 ]
then
    for i in $*
    do
	if [ -f $i ]
	then
	    cat "$i" | \
	    (   A=`${READN} 2`; \
		( ${CHR} $A; cat ) | \
	 	if [ "$A" = "% !" ]; \
	 	then \
	 	${printer_type}.print ${options} ${pstype} ${ncd_name} ${net_port}; \
	 	else \
	 	${printer_type}.print ${options} ${texttype} ${ncd_name} ${net_port}; \
	 	fi; \
	     )
	 else
	     echo "Can not find \"$i\""
	 fi
    done
else
    cat - | \
    (   A=`${READN} 2`; \
	( ${CHR} $A; cat ) | \
	if [ "$A" = "% !" ]; \
	then \
	    ${printer_type}.print ${options} ${pstype} ${ncd_name} ${net_port}; \
	else \
	    ${printer_type}.print ${options} ${texttype} ${ncd_name} ${net_port}; \
	fi; \
    )
fi
exit $?
