#!/bin/sh
# @(#)stop-atalk.sh	1.17 revision of 98/02/06  11:18:53
# This is an unpublished work copyright (c) 1989 Helios Software GmbH
# 30159 Hannover, Germany

ESDIR=/usr/local/es

if test -f $ESDIR/conf/servers
then
	x=`sed -e 's/#.*$//g' -e '/afpsrv/d' -e '/desksrv/d' -e '/ndsrv/d' -e 's/\(.\{1,8\}\).*/\1/g' $ESDIR/conf/servers`
	for i in $x
	do
		SERVERS="$i $SERVERS"
	done
else
	SERVERS="timesrv papsrv mailsrv admsrv termsrv lpd"
fi

USAGE="Usage: $0 [-g grace] [-m message] [now]"
GRACE=""
MESSAGE=""

if test $# -gt 0
then
	while getopts 'g:m:' c
	do
		case $c in
		g)
			GRACE="-g $OPTARG"
			;;
		m)
			MESSAGE="$OPTARG"
			;;
		\?)
			echo $USAGE >&2
			exit 1
			;;
		esac
	done
	shift `expr $OPTIND - 1`
	if test $# -gt 0
	then
		if test "$1" = "now"
		then
			GRACE="-g 0"
		else
			echo $USAGE >&2
			exit 1
		fi
	fi
fi

if test x"$GRACE" = x
then
	echo "Shutting down EtherShare in 5 minutes. To stop EtherShare"
	echo "immediatly use \"stop-atalk now\"."
fi

for i in $SERVERS
do
	$ESDIR/etc/forall $i kill
	while test $? -ne 0
	do
		sleep 2
		$ESDIR/etc/forall $i kill
	done
done

# The filing service must be stopped seperately to allow for the grace
# period.
if test -n "$MESSAGE"
then
	$ESDIR/etc/stop-afp $GRACE -m "$MESSAGE"
else
	$ESDIR/etc/stop-afp $GRACE
fi
$ESDIR/etc/forall desksrv kill

# atalkd must be stopped last, it will destroy the streams mux.
$ESDIR/etc/forall atalkd kill

# Unload any shared libraries on AIX to make updates easier.
if test -x /usr/sbin/slibclean
then
	/usr/sbin/slibclean
fi

exit 0
