#! /bin/sh
#Tag 0x00000500
#
# Execute once per client on a host machine (i.e., the machine with the
# physical printer attached.), or use the -a option to allow universal access.
#
# $Revision: 1.9 $
# $Date: 1993/04/06 23:54:23 $

usage() {
	echo "usage: addclient client_machine_name"
	echo "   or  addclient -a"
	exit 1
	}

if [ ! -w /var/spool/lp ]
then
	if [ -r /var/spool/lp ]
	then
		echo "You must be logged in as root to use this program."
		exit 1
	else
		echo "The lp spooling system is not installed on this machine."
		exit 1
	fi
fi

ADDALL=0

while getopts a flag
do
	case $flag in
		a)
			ADDALL=1
			;;
		\?)
			usage
			exit 1
			;;
	esac
done

shift `expr $OPTIND - 1`

cm=$1

if [ $ADDALL -eq 0 ]
then
	if [ -z "$cm" ]
	then
		usage
	fi
	# Make sure we use the proper name for the remote host, not an alias.
	if chkconfig yp
	then
		set -- `ypmatch $cm hosts 2> /dev/null`
	else
		set -- `grep $cm /etc/hosts`
	fi

	if [ -z "$2" ]
	then
		echo "Unknown host \"$cm\""
		exit 1
	fi

	if [ "$2" != "$cm" ]
	then
		echo "Using canonical name \"$2\" instead of alias \"$cm\"."
		cm=$2
	fi
fi

# if there is no home dir for lp, give it one
if grep -s '^lp:.*:/:$' /etc/passwd ; 
then
ed - /etc/passwd << '__JUNK__'
/^lp:/s/:\/:/:\/usr\/spool\/lp:\/bin\/sh/
w
q
__JUNK__
fi

# get the current home directory of the lp user
lpusr=`grep '^lp:' /etc/passwd | cut -d: -f6`

if [ $ADDALL -eq 1 ]
then
	echo "+\n+ root" > $lpusr/.rhosts
	echo "All other hosts can now access printers on this machine."
else
	if grep -s $cm $lpusr/.rhosts ; then 
		echo "$cm is already a client"
	else
		echo "$cm lp" >> $lpusr/.rhosts
		echo "$cm root" >> $lpusr/.rhosts
		echo Added \"$cm lp\" to "$lpusr/.rhosts"
	fi
fi

#
# Make sure the permissions are correct on the .rhost file.
chmod 600 $lpusr/.rhosts
chown lp $lpusr/.rhosts
chgrp lp $lpusr/.rhosts
