#! /bin/sh
#
# $Id: add_netatalk_printer,v 1.6 2001/10/06 12:27:38 srittau Exp $
#
# A lovely script to add netatalk printers 
#
#ident  "@(#)netatalk   0.5     99/06/22 job@uchicago.edu"  /* Netatalk 1.4*/
#
# This File is released under the Perl Artistic Licence.
# See http://www.perl.org for details
#
# Or you can use it under the licence that accompanies Netatalk 1.3 =)
#
# This file is maintained by Job Bogan <job@uchicago.edu>
# Please contact me at this address for bug reports, problems, etc.
# Please do not bother the netatalk  maintainers with problems from
# this program.
#
#
#
# Exit Codes ...  1 for missed path or user input
# 2 for an aborted run (by not saying yes to a 'are you sure?'
# 3 for failed lpadmin commands
# 4 for possible security problem

# make base name for temp files.

TMP_STUFF="/tmp/`basename $0`.$$"
export TMP_STUFF
rm -rf ${TMP_STUFF}
mkdir -m 700 ${TMP_STUFF} || exit 4
trap 'rm -f $TMP_STUFF > /dev/null 2>&1' 0
trap 'exit' 1 2 3

# grab the pathname the script was called w/
# if it was run by ./add...  then make the path == `pwd`

RUNHOME=`dirname $0`

if [ "$RUNHOME" = "." ]; then
 	RUNHOME=`pwd`
fi

# allow for the env NETATALKHOME to override the guessed one from above

NETATALKHOME=${NETATALKHOME:-$RUNHOME}
export NETATALKHOME

PATH=/bin:${PATH}:${NETATALKHOME}/bin:${NETATALKHOME}/etc:${NETATALKHOME}/etc/filters:/usr/lib:/usr/sbin

if [ ! -x ${NETATALKHOME}/bin/pap ]; then
	echo "OOPS:	I don't see ${NETATALKHOME}/bin/pap ,"
	echo '	Check that it is executable or set ${NETATALKHOME}'
	echo '	so i can find pap at ${NETATALKHOME}/bin/pap'
	echo '[We are guessing the location of the binary from the'
	echo 'path that was used to run this script or $NETATALKHOME]'
	exit 1
fi

# get the user to tell us where to look for things, and list things there.

echo ''
echo "Enter Appletalk zone [default: local]: \c"

read ZONE

echo ""
echo "Looking for LaserWriters in Zone ${ZONE} ..."
${NETATALKHOME}/bin/nbplkup ":LaserWriter@${ZONE}"

echo ""
echo "Enter AppleTalk printer name: \c"

read DEST

if [ "$DEST" = "" ]; then
	echo "OOPS: you need to tell me a printer name"
	exit 1
fi

echo "checking nbplkup ${DEST}:LaserWriter@${ZONE}"
echo ""
TestDEST=`${NETATALKHOME}/bin/nbplkup "${DEST}:LaserWriter@${ZONE}"`
echo "${TestDEST}"
echo ""

if [ "${TestDEST}" = "" ]; then
    echo "I don't see that printer on the network.  You may have entered an incorrect"
    echo "printer name - if so, exit and restart."
    echo "[You should only enter the printer name, not the :LaserWriter portion]"
    sleep 3
fi

unset TestDEST

NBPNAME="${DEST}@${ZONE}"

echo ""
echo ""

# scrunch all of the whitespace and / out of the appletalk name and suggest
# that as the unix name

SUGGEST=`echo "$DEST" | cut -d: -f1 | sed 's/ //g' | sed 's#/##g` ; export SUGGEST

# truncate the suggested name to 14 chars to conform to lp specs.

SUGGEST1=`expr ${SUGGEST} : '\(..............\)'`
SUGGEST=${SUGGEST1:-$SUGGEST}

echo "Enter Unix printer name - [default: ${SUGGEST}] : \c"
read UNIXPRINT
echo ''
if [ "${UNIXPRINT}" = "" ]; then
	UNIXPRINT="${SUGGEST}"
fi
export UNIXPRINT

#####
# Here we check for legal names again. >14 chars
#####

UNIXPRINT1=`expr "${UNIXPRINT}" : '\(..............\)'`

# if UNIXPRINT is shorter than 14chars, UNIXPRINT1 ends up null ("")
# if UNIXPRINT was longer, then we get a 14char version of it.  bleah.

if [ "${UNIXPRINT1}" = "" ]; then
	UNIXPRINT1="${UNIXPRINT}"
else
#if [ "${UNIXPRINT1}" != "${UNIXPRINT}" ]; then
    echo "Oops, that name was too long...  Truncating to 14 chars."
    echo "setting printer name to '${UNIXPRINT}'"
    UNIXPRINT="${UNIXPRINT1}"
fi

echo "Enter a description for this printer.  [The Appletalk name will"
echo "be included by default, and must be included for the printing"
echo "filters to work.] : \c"
read DESC

DESC="${DESC} [${NBPNAME}]"

echo ""
echo "Do you want all print jobs to print out in reversed page order?"
echo "This is usually only desired if this printer stacks jobs"
echo "face up in the output bin. [N/y] \c"
read REV
case ${REV} in		[Yy]*) 	REV="Netatalk-R" ;;
			*)	REV="Netatalk"   ;;
esac
export REV

if lpstat -p ${UNIXPRINT} >/dev/null 2>&1 ; then
	echo ''
	echo "OOPS:	There already exists a printer named '${UNIXPRINT}'"
	echo "		here are the comments from /etc/lp/printers/${UNIXPRINT}/comment"
	cat /etc/lp/printers/${UNIXPRINT}/comment
	echo ""
	echo "Do you still want to do this? This will flush "
	echo "all pending jobs!  [Y/n]? \c"
	read DOTHIS
	case ${DOTHIS} in	[Yy]*)         ;;
				*)	exit 2 ;;
	esac

	echo "Rejecting all new jobs for ${UNIXPRINT}"
	reject -r "Making ${UNIXPRINT} a netatalk printer" ${UNIXPRINT}
	echo "Disabling and Flushing the ${UNIXPRINT} queue"
	disable -c -r "Making ${UNIXPRINT} a netatalk printer" ${UNIXPRINT}
fi

#####
# Check if we need to do silly tricks to share the printer... only on 5.5.1 or older
#####

if [ "`uname -r`" -lt "5.6" ]; then
    echo ""
    echo "Do you want to share this printer with other machines [Y/n]?\c"
    read SHARE
else
    echo "You're running SunOS 5.6 or higher... skipping 'sacadm' and 'pmadm'"
fi

if nistest printers.org_dir; then
    echo "Do you want to add this printer to your NIS printer map? [y/N]"
    echo "(You will need to share this printer with other machines)"
    read NIS
    case ${NIS} in 	[Yy]*)    ADD_NIS=1 ;;
			    *)              ;;
    esac
fi

echo "Do you want to save the error messages from pap in"
echo "'/var/spool/lp/tmp/Netatalk/${UNIXPRINT}' [y/N]"
echo "(If you answer N, error messages will go to /dev/null.)"
read SAVEerr
case ${SAVEerr} in      [Yy]*)     SAVEerr=1 ;;
			    *)     unset SAVEerr ;;
esac

echo "Making ${UNIXPRINT} print to ${DEST} via netatalk"

if [ "${SAVEerr}" = "" ]; then
    LOCKDEV=/dev/null
else
    LOCKS=/var/spool/lp/tmp/Netatalk
    if [ ! -d ${LOCKS} ]; then
	    mkdir -m 0771	${LOCKS}
	    chown lp	${LOCKS}
	    chgrp lp	${LOCKS}
    fi
    LOCKDEV=${LOCKS}/${UNIXPRINT}
    touch		${LOCKDEV}
    chown lp:lp	${LOCKDEV}
    chmod 600	${LOCKDEV}
fi

echo ""

if [ ! -r /etc/lp/filter.table ]; then
        echo "Setting up the existing print filters..."
        for i in `ls /etc/lp/fd | sed 's/.fd$//'`; do
                lpfilter -f ${i} -F/etc/lp/fd/${i}.fd
        done
fi
if lpfilter -f netatalk -l > /dev/null 2>&1 ; then
	echo 'Looks like you have a Netatalk printer filter defined. Good!'
else
	echo "Humm... You need a Netatalk printer filter..."
	echo "... making you one ..."
	cat > /etc/lp/fd/netatalk.fd << EOF
#ident  "@(#)netatalk.fd 0.2 97/09/04 job@uchicago.edu"      /* Netatalk 1.4 */

Input types: postscript
Output types: PS
Printer types: Netatalk
Printers: any
Filter type: fast
Command: ${NETATALKHOME}/etc/filters/ifpap 2>&1 > /dev/null
EOF
	chown lp:lp /etc/lp/fd/netatalk.fd
	chmod 664   /etc/lp/fd/netatalk.fd
	lpfilter -f netatalk -F /etc/lp/fd/netatalk.fd
fi

if lpfilter -f netatalk-r -l 2>&1 > /dev/null ; then
	echo 'Looks like you have a Reverse Netatalk printer filter defined. Good!'
else
	echo "Humm... You need a Reverse Netatalk printer filter..."
	echo "... making you one ..."
	cat > /etc/lp/fd/netatalk-r.fd << EOF
#ident  "@(#)netatalk-r.fd 0.2 97/09/04 job@uchicago.edu"      /* Netatalk 1.4 */

Input types: postscript
Output types: PS
Printer types: Netatalk-R
Printers: any
Filter type: fast
Command: "/usr/lib/lp/postscript/postreverse | ${NETATALKHOME}/etc/filters/ifpap 2>&1 >/dev/null"
EOF
	chown lp:lp /etc/lp/fd/netatalk-r.fd
	chmod 664   /etc/lp/fd/netatalk-r.fd
	lpfilter -f netatalk-r -F /etc/lp/fd/netatalk-r.fd
fi

if [ ! -r /usr/share/lib/terminfo/N/Netatalk ]; then
	echo "Making a Terminfo entry for Netatalk printers"
	cat > ${TMP_STUFF}/terminfo << EOF
Netatalk,
	cols#80, lines#66,
	cpi=null, csnm=^D, lpi=null, scs=^D, slines=^D, u9=^D,
EOF
	tic ${TMP_STUFF}/terminfo
	chown bin:bin /usr/share/lib/terminfo/N/Netatalk
	chmod 644     /usr/share/lib/terminfo/N/Netatalk
fi

if [ ! -r /usr/share/lib/terminfo/N/Netatalk-R ]; then
	echo "Making a Terminfo entry for Reversed Netatalk printers"
	cat > ${TMP_STUFF}/terminfoR << EOF
Netatalk-R,
	cols#80, lines#66,
	cpi=null, csnm=^D, lpi=null, scs=^D, slines=^D, u9=^D,
EOF
	tic "${TMP_STUFF}/terminfoR"
	chown bin:bin /usr/share/lib/terminfo/N/Netatalk-R
	chmod 644     /usr/share/lib/terminfo/N/Netatalk-R
fi

###
# this is old cruft...  we should not have done this in the 1st place.
# we need to edit the template interface file to point to the local netatalk bins
#cat ${RUNHOME}/etc/netatalk.template | sed "s#DEFAULT_NETATALK_HOME#${RUNHOME}#g" \
#	> "/etc/lp/interfaces/${UNIXPRINT}"
#chown lp:lp	/etc/lp/interfaces/${UNIXPRINT}
#chmod 0775	/etc/lp/interfaces/${UNIXPRINT}
# below is the correct way.
###

echo "Setting up ${UNIXPRINT} ...  Edit options later as you see fit."
lpadmin -p ${UNIXPRINT} -D "${DESC}" -T ${REV} \
    -i ${RUNHOME}/netatalk.template -I PS -v ${LOCKDEV} -A none || exit 3
accept ${UNIXPRINT} || exit 3
enable ${UNIXPRINT} || exit 3

case ${SHARE} in	[Nn]*)	exit; ;;
			*)	      ;;
esac

echo "Setting up network sharing for ${UNIXPRINT}"

# from p925 in the Solaris Administration Guide, Vol. II (2.5)

echo ""
if sacadm -l -p tcp > /dev/null; then
   echo "already running a tcp listener"
else
   echo "Defining tcp listener ..."
   sacadm -a -p tcp -t listen -c "/usr/lib/saf/listen tcp" \
          -v `nlsadmin -V` -n 999
fi

# this is extra, but a good idea...

LPD_ADDR="\\x`lpsystem -A`" ; export LPD_ADDR
ADDR_0=`echo $LPD_ADDR | sed -e 's/\\x00020203/\\x00020ACE/g'`; export ADDR_0

if pmadm -l -p tcp -s 0 > /dev/null; then
   echo "<0> service is already defined."
else
   echo "Defining <0>/tcp service ..."
   pmadm -a -p tcp -s 0 -i root \
         -m `nlsadmin -c /usr/lib/saf/nlps_server -A ${ADDR_0}` \
         -v `nlsadmin -V`
fi

# again from the solaris book noted above.

if pmadm -l -p tcp -s lp > /dev/null; then
   echo "<lp> service is already defined."
else
   echo "Defining <lp>/tcp service ..."
   pmadm -a -p tcp -s lp -i root \
         -m `nlsadmin -o /var/spool/lp/fifos/listenS5` \
         -v `nlsadmin -V`
fi

if pmadm -l -p tcp -s lpd > /dev/null; then
   echo "<lpd> service is already defined."
else
   echo "Defining <lpd>/tcp service ..."
   pmadm -a -p tcp -s lpd -i root \
         -m `nlsadmin -o /var/spool/lp/fifos/listenBSD -A ${LPD_ADDR}` \
         -v `nlsadmin -V`
fi

if [ "${ADD_NIS}" = 1 ] ; then
    if nistest "[printer_name=${UNIXPRINT}]printers.org_dir"; then
	nistbladm -m	printer_host=`uname -n` \
	"[printer_name=${UNIXPRINT}]printers.org_dir"  description="$DESC"
    else
	nistbladm -a	printer_name=${UNIXPRINT} \
	description="$DESC" printer_host=`uname -n` printers.org_dir
    fi
fi
