restart-AppleTalk   [plain text]


#!/bin/sh
#
# restart-AppleTalk
#

AT_CFG_FILE=/etc/appletalk.cfg
scutil_Commands=/tmp/.atUpdate-$$


#
# First, we identify all current state information
# in the cache and generate "scutil" commands to remove
# the old data.
#
echo "open"	>  ${scutil_Commands}
echo "lock"	>> ${scutil_Commands}
scutil << __END_OF_SCUTIL_COMMAND 2>/dev/null	| \
	sed -n -e 's/.* = /remove /p'	>> ${scutil_Commands}
open
list ^State:/Network/Global/AppleTalk\$ regex
close
quit
__END_OF_SCUTIL_COMMAND


#
# If running, shutdown current AppleTalk networking
#

CHECK=1
LIMIT=10

while [ 1 ]
do
	#
	# shutdown the stack
	#
	appletalk -d >/dev/null 2>&1
	status=$?
	case "${status}" in
	0 )	# shutdown successful
		break
		;;
	89 )	# AppleTalk not running
		break
		;;
	esac

	#
	# Error detected during shutdown
	#
	CHECK=`expr ${CHECK} + 1`
	if [ ${CHECK} -le ${LIMIT} ]; then
		#
		# Delay and retry
		#
		sleep 1
		continue
	else
		#
		# We've waiting long enough, something's not right
		#
		logger -i -p daemon.notice -t restart-AppleTalk "AppleTalk shutdown failed (status=${status})"
		break
	fi
done


#
# Check for AppleTalk networking configuration file
#
if [ ! -f ${AT_CFG_FILE} ]; then
	#
	# we have no configured interfaces and the AppleTalk stack
	# has been shut down so simply remove any existing status
	# from the cache.
	#
	echo "unlock"	>> ${scutil_Commands}
	echo "close"	>> ${scutil_Commands}
	echo "quit"	>> ${scutil_Commands}
	if [ `cat ${scutil_Commands} | wc -l` -gt 5 ]; then
		scutil < ${scutil_Commands}
	fi
	rm ${scutil_Commands}

	exit 0
fi


#
# Start AppleTalk networking based on the information in
# the configuration file(s).
#
. /etc/rc.common

ATO=""
if [ "${APPLETALK_HOSTNAME:=-AUTOMATIC-}" != "-AUTOMATIC-" ]; then
	APPLETALK_HOSTNAME=`echo "X${APPLETALK_HOSTNAME}" | sed -e 's/^X//' -e 's/"/\\\\"/g' -e '/[ <>]/s/\(.*\)/"\1"/'`
	ATO="-C ${APPLETALK_HOSTNAME}"
fi

if [ "${APPLETALK:=-NO-}" != "-NO-" ]; then
	case "${APPLETALK}" in
	-ROUTER-)
		# Router mode
		eval appletalk ${ATO} -r -q
		;;

	-MULTIHOME-)
		# Multihome non-routing mode
		eval appletalk ${ATO} -x -q
		;;

	*)
		# Single port on specified interface, non-routing
		eval appletalk ${ATO} -u ${APPLETALK} -q
		;;

	esac
fi


#
# Update computer name
#
echo "d.init"							>> ${scutil_Commands}
echo "get Setup:/Network/Global/AppleTalk"			>> ${scutil_Commands}
if [ -z "${ATO}" ]; then
	echo "d.add ComputerName `uname -n`"			>> ${scutil_Commands}
fi
echo "set State:/Network/Global/AppleTalk"			>> ${scutil_Commands}

echo "unlock"	>> ${scutil_Commands}
echo "close"	>> ${scutil_Commands}
echo "quit"	>> ${scutil_Commands}
if [ `cat ${scutil_Commands} | wc -l` -gt 5 ]; then
	scutil < ${scutil_Commands} > /dev/null 2>&1
fi
rm ${scutil_Commands}

exit 0