#! /bin/sh
#
# bsdfuncs -- Common shell functions for LPS support scripts for BSD systems
#
# @(#)bsdfuncs	1.1	LPS_UNX_COM	05/10/94
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# DESCRIPTION:
#
# This file contains common Bourne shell functions used by many
# of the PrintServer support scripts.
#
# This script expects that the common kit variables have already
# been defined and exported.
#
# This file must be imported (aka "sourced") via the "." shell command,
# afterwhich all functions can be used by the client shell script.
#
# FUNCTION LIST:
#    getcapentry
#    mkcapentry
#
###

# Ensure required shell variables are set

: ${PRINTCAP:=/etc/printcap}
: ${TMPDIR:=/tmp}

#----------------------------------------------------------------------

# getcapentry()
#
# This function is used to determine if the named print queue exists,
# and if so, the critical queue variables are extracted into the
# proper environment variables.  If the queue does not exist, then
# the environment variables are cleared.
#
# Parameters:
#    $1 - The primary name of the print queue
#
# The return value represents the status of the queue entry:
#     0 - The name does not represent a defined queue entry
#     1 - The name represents a the primary name of a defined queue
#     2 - The name represents an alias for a defined queue
#     3 - The name represents a defined remote print queue
#     4 - Error encountered while try to fetch the $PRINTCAP entry
###

getcapentry ()
{
    QNAME=$1

    CAP_ENT=""
    CAP_AF=""
    CAP_ALIAS=""
    CAP_LF=""
    CAP_LP=""
    CAP_PL=""
    CAP_PW=""
    CAP_RM=""
    CAP_RP=""
    CAP_SD=""

    if caplist -f $PRINTCAP -e $QNAME > /dev/null
    then
	CAP_ENT="`sed -n -e '/^'$QNAME'[:|]/,/[^\\\\]$/p' < $PRINTCAP`"
	if [ -z "$CAP_ENT" ]
	then	# The name is an alias for a queue
	    CAP_ENT="`sed -n -e '/\\|'$QNAME'[:|]/,/[^\\\\]$/p' < $PRINTCAP`"
	    CAP_ALIAS="`expr "$CAP_ENT" : '\([^|:]*\)|'`"
	fi

	CAP_AF="`expr "$CAP_ENT" : '.*:af=\([^:]*\):'`"
	CAP_LF="`expr "$CAP_ENT" : '.*:lf=\([^:]*\):'`"
	CAP_LP="`expr "$CAP_ENT" : '.*:lp=\([^:]*\):'`"
	CAP_PL="`expr "$CAP_ENT" : '.*:pl#\([^:]*\):'`"
	CAP_PW="`expr "$CAP_ENT" : '.*:pw#\([^:]*\):'`"
	CAP_RM="`expr "$CAP_ENT" : '.*:rm=\([^:]*\):'`"
	CAP_RP="`expr "$CAP_ENT" : '.*:rp=\([^:]*\):'`"
	CAP_SD="`expr "$CAP_ENT" : '.*:sd=\([^:]*\):'`"

	if [ "$CAP_RP" ]
	then
	    return 3
	elif [ "$CAP_ALIAS" ]
	then
	    return 2
	else
	    return 1
	fi
    else
	return 0    # Name is not currently in use
    fi

    return 4
}

#----------------------------------------------------------------------

# mkcapentry()
#
# This function is used to create the $PRINTCAP entry based on the
# current state of all relevant environment variables.
#
# Parameters:
#    $1 - Primary name of the new print queue
#    $2 - Device name
#    $3 - Spooling directory
#    $4 - Directory path for filter programs
#    $5 - Log file path
#    $6 - Accounting file path
#    $7 - Page width
#    $8 - Page length
#    $9 - Associated PrintServer object name
#
# Upon successful return, the new $PRINTCAP entry will exist in a file
# defined by the $CAPENTRY shell variable.
#
# The format of the entry is pretty flexible, but the format guidelines
# described in printcap(5) should be followed if possible.
#
# The return value is 0 for success, 1 for failure, with messages going
# to stdout and/or stderr as necessary.

mkcapentry ()
{
    QNAME=$1
    LP=$2
    SD=$3
    BIN=$4
    LF=$5
    AF=$6
    PW=$7
    PL=$8
    PSNAME=$9

    if [ $# -ne 9 ]
    then
	echo
	echo "    Improper call of mkcapentry() function!"
	echo
	return 1
    fi

    CAPENTRY=$TMPDIR/mkcap.$$

    cat > $CAPENTRY << END_OF_INPUT
${QNAME}|${QNAME}|${QNAME} (PrintServer ${PSNAME}):\\
	:lp=${LP}:\\
	:sd=${SD}:\\
	:of=${BIN}/lpsof:\\
	:if=${BIN}/lpsif:\\
END_OF_INPUT

    if [ "$AF" ]	# If accounting has been specified
    then
	echo "	:af=${AF}:\\" >> $CAPENTRY
    fi

    if [ "$LF" ]	# If a log file has been specified
    then
	echo "	:lf=${LF}:\\" >> $CAPENTRY
    fi
	
    # These fields are REQUIRED for all Print Client entries in $PRINTCAP

    echo "	:pl#${PL}:pw#${PW}:mx#0:sb:" >> $CAPENTRY

    return 0 
}
