#!/bin/sh
#Tag 0x00000700
#**************************************************************************
#*
#* 		  Copyright (c) 1993 Silicon Graphics, Inc.
#*			All Rights Reserved
#*
#*	   THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
#*
#* The copyright notice above does not evidence any actual of intended
#* publication of such source code, and is an unpublished work by Silicon
#* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
#* the property of Silicon Graphics, Inc. Any use, duplication or
#* disclosure not specifically authorized by Silicon Graphics is strictly
#* prohibited.
#*
#* RESTRICTED RIGHTS LEGEND:
#*
#* Use, duplication or disclosure by the Government is subject to
#* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
#* Technical Data and Computer Software clause at DFARS 52.227-7013,
#* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
#* Supplement. Unpublished - rights reserved under the Copyright Laws of
#* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
#* Shoreline Blvd., Mountain View, CA 94039-7311
#**************************************************************************
#*
#* File: mknetpr
#*
#* $Revision: 1.22 $
#*
#* Description: Shell script for adding a remote printer to the local
#*	System V spooling system.
#*
#*	Usage: mknetpr [<local printer name>] [<remote host>]
#*		       [<remote printer name>]
#*
#*	If the local printer name, remote printer name and remote
#*	printer host are all specified on the command line, mknetpr does
#*	not prompt for input and terminates upon encountering errors. If the
#*	command line information is not complete, mknetpr runs interactively
#*	prompting for the missing information and allowing recovery from
#*	certain installation error conditions.
#*
#**************************************************************************


# Well known directories and programs

LPUTIL_DIR=/usr/lib
LPUTIL=$LPUTIL_DIR/lputil
OUT_FILT="/usr/bin/pg -s -p more... -e"
SPOOL_DIR=/var/spool/lp
LPSTAT=/usr/bin/lpstat


# Global variables

LOCAL_PNAME=""
REMOTE_PNAME=""
REMOTE_HOST=""
INTERACT=0
REMOTE_ID="lp"


#
# Prints the program usage string
#
PrintUsage()
{
    echo "Usage: mknetpr [<local printer name>] [<remote host>] \c"
    echo "[<remote printer name>]"
}


#
# Checks that we are the super-user and verifies
# that the System V spooling system is present
#
CheckPermSpooler()
{
    idstr=`id`
    cid=`expr "$idstr" : '^[ 	]*uid=.*(\(.*\))[ 	]*gid='`
    if [ "$cid" != "root" ]; then
        echo "You must be logged in as root to use this program."
        exit 1;
    fi
    if [ ! -r $SPOOL_DIR ]; then
        echo "The System V spooling system is not installed on this system."
        exit 1
    fi
}

#
# Check for a duplicate printer name. Returns 0 if dup,
# 1 if name is unique
#
CheckDupName()
{
    /bin/ls $SPOOL_DIR/member/$1 2> /dev/null 1> /dev/null
    return $?
}


#
# Query user for a name for the local printer
#
QueryLocalName()
{
    while [ "$LOCAL_PNAME" = "" ]; do
        echo "Enter new printer name (14 chars max.): \c"
        read LOCAL_PNAME
        VerifyPname $LOCAL_PNAME
        if [ $? -eq 1 ]; then
	    echo "Invalid response: Name must be 14 chars (A-Za-z0-9_ only)"
	    LOCAL_PNAME=""
        fi
        if [ "$LOCAL_PNAME" = "" ]; then
	    continue
        fi
	CheckDupName $LOCAL_PNAME
        if [ $? -eq 0 ]; then
	    echo " "
            echo "WARNING: Printer $LOCAL_PNAME already exists."
            echo '\nChoose a new name (y/n)[y]? \c'
            read yn
            if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
	        LOCAL_PNAME=""
            fi
        fi
    done
}


#
# Test for pure decimal positive integer input
#
# Expects $1 to be the value to check. Returns 0 if
# OK, 1 if invalid positive integer or if more than one
# argument specified
#
VerifyInt()
{
    if [ $# -eq 0 ]; then
	return 0
    fi
    if [ $# -gt 1 ]; then
	return 1
    fi
    echo $1 | egrep -s '^[0-9]*$'
    return $?
}


#
# Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
# only and no spaces)
#
VerifyPname()
{
    if [ $# -eq 0 ]; then
	return 0
    fi
    if [ $# -gt 1 ]; then
	return 1
    fi
    echo $1 | egrep -s '^[A-Za-z0-9_]*$'
    if [ $? -eq 1 ]; then
	return 1
    fi
    if [ `expr $1 : '.*'`  -gt 14 ]; then
	return 1
    fi
    return 0
}


#
# Test for a properly constructed remote host name
#
VerifyHostname()
{
    if [ $# -gt 1 ]; then
	return 1
    fi
    return 0
}


#########################################################################
#
# Main program
#

#
# Ensure that we are root and that the main System V
# spooling system directory is present
#
CheckPermSpooler

#
# Get command line arguments, if any, and determine if we
# should run in interactive mode
#
case $# in
    0) INTERACT=1
       ;;
    1) INTERACT=1
       LOCAL_PNAME=$1
       ;;
    2) INTERACT=1
       LOCAL_PNAME=$1
       REMOTE_HOST=$2
       ;;
    3) INTERACT=0
       LOCAL_PNAME=$1
       REMOTE_HOST=$2
       REMOTE_PNAME=$3
       ;;
    *) PrintUsage
       exit 1
       ;;
esac

#
# Display an intro message
#
echo "Remote Printer Installation Tool\n"

#
# Make sure we have a remote printer host
#
if [ "$REMOTE_HOST" = "" ]; then
    while [ "$REMOTE_HOST" = "" ]; do
        echo "Enter remote host machine name: \c"
        read REMOTE_HOST
        VerifyHostname $REMOTE_HOST
        if [ $? -eq 1 ]; then
	    echo "Invalid response: Improperly constructed hostname"
	    REMOTE_HOST=""
        fi
    done
    echo " "
fi

#
# First test that we can talk to the remote host
#
echo "Testing connection to remote host $REMOTE_HOST...\n"
REMOTE_NAMES=`$LPUTIL chkremote "$REMOTE_HOST" "$REMOTE_ID"`
if [ $? -ne 0 ]; then
    echo "Failed to connect to $REMOTE_HOST"
    echo "Check that remote machine is up and allows login as $REMOTE_ID"
    echo "You may need to run \"addclient(1M)\" on $REMOTE_HOST\n"
    exit 1
fi

#
# Now make sure there are printers on the remote host
#
if [ "$REMOTE_NAMES" = "" ]; then
    echo "There are either no printers connected to $REMOTE_HOST"
    echo "or the network connection to $REMOTE_HOST has failed. Check"
    echo "that the remote machine is up and has at least one printer"
    echo "connected."
    exit 1
fi

echo "Connection to $REMOTE_HOST verified\n"

#
# Query user for a name for this printer
#
if [ "$LOCAL_PNAME" = "" ]; then
    QueryLocalName
    echo " "
else
    echo "Requested local printer name: $LOCAL_PNAME\n"
    VerifyPname $LOCAL_PNAME
    if [ $? -eq 1 ]; then
	echo "Error: Local printer name must be 14 chars (A-Za-z0-9_ only)"
	if [ $INTERACT -eq 1 ]; then
	    LOCAL_PNAME=""
	    QueryLocalName
	    echo " "
	else
	    exit 1
	fi
    fi
    CheckDupName $LOCAL_PNAME
    if [ \( $? -eq 0 \) -a \( $INTERACT -eq 1 \) ]; then
	echo " "
        echo "WARNING: Printer $LOCAL_PNAME already exists."
        echo '\nChoose a new name (y/n)[y]? \c'
        read yn
        if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
	    LOCAL_PNAME=""
	    QueryLocalName
	    echo " "
        fi
    fi
fi

#
# Now select the remote printer
#
if [ "$REMOTE_PNAME" = "" ]; then
    echo "Printers on $REMOTE_HOST are:"
    nump=0
    for pname in $REMOTE_NAMES
    do
	nump=`expr $nump + 1`
	echo "\t$nump. $pname"
    done
    rnum=""
    while [ "$rnum" = "" ]; do
        echo "Enter number of printer to install: \c"
        read rnum
        VerifyInt $rnum
        if [ $? -eq 1 ]; then
            echo "Invalid response: Printer number must be between 1 and $nump"
            rnum=""
        fi
        if [ "$rnum" = "" ]; then
            continue
        fi
        if [ \( $rnum -eq 0 \) -o \( $rnum -gt $nump \) ]; then
            echo "Invalid response: Number must be between 1 and $nump"
            rnum=""
        fi
    done
    cnt=0
    for pname in $REMOTE_NAMES
    do
	cnt=`expr $cnt + 1`
	if [ $cnt -eq $rnum ]; then
	    REMOTE_PNAME=$pname
	    break
	fi
    done
else
    foundit=0
    for pname in $REMOTE_NAMES
    do
	if [ "$pname" = "$REMOTE_PNAME" ]; then
	    foundit=1
	    break
	fi
    done
    if [ $foundit -eq 0 ]; then
	echo "Remote printer $REMOTE_PNAME not found on $REMOTE_HOST"
	echo "Known printers on $REMOTE_HOST are:\n"
	echo "$REMOTE_NAMES\n"
	exit 1
    fi
fi

#
# Inform the user of what comes next
#
echo " "
echo "Installing remote printer $LOCAL_PNAME..."
echo " "

#
# Get the type and model name of the remote printer
#
rinfo=`$LPUTIL remoteinfo $REMOTE_HOST $REMOTE_PNAME $REMOTE_ID`
if [ \( $? -ne 0 \) -o \( "$rinfo" = "" \) ]; then
    echo "Could not get remote printer information"
    echo "Check network connection and printer installation on remote host"
    exit 1
fi
# Change IFS to parse newlines as separators.
oldIFS=$IFS
IFS='
'
for item in $rinfo
do
    val=`echo $item | egrep '^NAME='`
    if [ "$val" != "" ]; then
	NFLAG=$val
    else
        val=`echo $item | egrep '^TYPE='`
        if [ "$val" != "" ]; then
	    TFLAG=$val
	fi
    fi
done
IFS=$oldIFS

#
# Install the printer.
#
$LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $LOCAL_PNAME
if [ $? -ne 0 ]; then
    echo "ERROR: Installation of new printer failed."
    echo "       $LPUTIL command failed."
    exit 1
fi

#
# Inform the user of what has happened
#
echo "Remote printer $LOCAL_PNAME has been installed"
echo " "
echo "Here is your printing environment:"
echo " "
$LPSTAT -t | $OUT_FILT

#
# Return a successful exit code
#
exit 0
