#!/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,v 1.2.6.1 1994/08/04 21:15:22 mse 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`

# 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 `getopt d:i:l:w:roxT:s:p:n:h: $*`
if [ $? != 0 ]
then
    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] [-x] filename..." >&2
    exit $EXIT_USAGE
fi
shift # shift off XXX

for i in $*
do
    case "$i" in
	-r)
	    options="${options} -r"
	    shift
	    ;;
	-o)
	    pstype="raw"
	    texttype="raw"
	    shift
	    ;;
	-x)
	    options="${options} -x"
	    x_txport=1
	    shift
	    ;;
	-T)
	    printer_type="$2"
	    shift; shift
	    ;;
	-n)
	    net_port="$2"
	    shift; shift
	    ;;
	-s)
	    port_type="s"
	    port_num="$2"
	    shift; shift
	    ;;
	-p)
	    options="${options} -p"
	    port_type="p"
	    port_num="$2"
	    shift; shift
	    ;;
	-h)
	    terminal="$2"
	    shift; shift
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    options="${options} $1 $2"
	    shift; shift
	    ;;
    esac
done

case "${printer_type}" in
    du | as)
	printer_type="ascii"
	;;
    lj | dj | lw | ascii)
	;;
    ps)
	printer_type="lw"
	;;
    *)
	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] [-x] filename..." >&2
	exit $EXIT_USAGE
esac

if [ "X${terminal}" = "X" ]
then
    echo $IAM: Terminal name not given >&2
    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
		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
	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; \
        )
    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 $?
