#!/bin/sh
# @(#)start-atalk.sh	1.65 revision of 98/02/24  15:33:32
# This is an unpublished work copyright (c) 1989 HELIOS Software GmbH
# 30159 Hannover, Germany

ESDIR=/usr/local/es
export ESDIR

LFILE=$ESDIR/conf/ethershare.license

if test -f $ESDIR/conf/servers
then
	SERVERS=`sed -e 's/#.*$//g' $ESDIR/conf/servers`
else
	SERVERS="desksrv afpsrv papsrv mailsrv admsrv termsrv timesrv ndsrv lpd"
fi

# test how we can suppress the new line on echo
if test "X`echo -n`" = "X-n"
then
	en=''
else
	en='-n'
fi
if test "X`echo '\c'`" = "X\c"
then
	ec=''
else
	ec='\c'
fi

SILENT=n
NEEDATALKD=y
if test $# -gt 0
then
	while getopts s c
	do
		case $c in
		s)
			SILENT=y
			;;
		\?)
			echo "Usage: $0 [-s]" >&2
			exit 1
			;;
		esac
	done
	shift `expr $OPTIND - 1`
fi

# Check for running AppleTalk.
$ESDIR/etc/procstat | egrep 'atalkd|desksrv' >/dev/null 2>&1
if test $? -eq 0
then
        echo "AppleTalk is already running, use \"stop-atalk now\" to stop it." >&2
	exit 1
fi

# Check that rpc is up, it is needed for the communication between afpsrv
# and desksrv.
$ESDIR/etc/procstat | egrep 'rpcbind|portmap' >/dev/null 2>&1
if test $? -ne 0
then
	echo "portmap or rpcbind is not running, make sure rpc is configured" >&2
	echo "and running before starting AppleTalk." >&2
	exit 1
fi

# Set all modes in ESDIR according to ESmodes file(s)
if test -f $ESDIR/etc/ESmodes 
then
	( cd $ESDIR; cat $ESDIR/etc/ESmodes* | $ESDIR/etc/setmode -q)
fi

# Verify that the license (and all its dependents) is entered properly.
$ESDIR/lcheck -d -f $LFILE -v "EtherShare OPI:2.0.0" EtherShare
if test $? -ne 0
then
	echo "The above mentioned license is not valid. Please re-enter" >&2
	echo "the license using the install program." >&2
	exit 1
fi

# Check that an afpipaccess file is present, if not create a reasonable
# default.
if test ! -f $ESDIR/conf/afpipaccess
then
	$ESDIR/etc/mkipaccess
fi

# Sun AppleTalk protocol modules
if test -f $ESDIR/.sun4
then
	loaded=`/usr/etc/modstat | sed -e 's/.\{58\}\(.\{2,11\}\)/\1/' |egrep "lt|atalk"`
	if test -z "$loaded"
	then
		# check if this system supports SLIC
		havelt=`/usr/etc/devinfo 2>/dev/null | grep "'PAC-Helios,lt'"`
		if test -n "$havelt"
		then
			echo $en "Loading lt.o: "$ec
			/usr/etc/modload -entry _lt_load -sym $ESDIR/kernel/lt.o
		else
			at="atalk.`/bin/arch -k`.o"
			echo $en "Loading $at: "$ec
			/usr/etc/modload -entry _atalk_load -sym $ESDIR/kernel/$at
		fi
	
	fi
fi

# IBM AppleTalk protocol modules
if test -f $ESDIR/.rs6000
then
	if test -d /dev/appletalk
	then
		NEEDATALKD=n
		ln -sf libtoolkit_s.a.apple $ESDIR/lib/libtoolkit_s.a
	else
		ln -sf libtoolkit_s.a.helios $ESDIR/lib/libtoolkit_s.a
		if test -z "`lsdev -C -l atalk0`"
		then
			mkdev -t atalk
		else
			if test -z "`lsdev -C -l atalk0 -S a`"
			then
				mkdev -l atalk0
			fi
		fi
	fi
	if test -x /usr/sbin/lcdstring
	then
		if test -f $ESDIR/opisrv
		then
			/usr/sbin/lcdstring "HELIOS EtherShare and HELIOS EtherShare OPI"
		else
			/usr/sbin/lcdstring "HELIOS EtherShare"
		fi
	fi
fi

# SGI AppleTalk Protocol modules
if test -f $ESDIR/.sgi
then
	ml list | grep atalk >/dev/null 2>&1
	if test $? -ne 0
	then
		OSVER=`uname -r|sed -e 's/\([0-9]*\.[0-9]*\).*/\1/'`
		KMOD="atalk${OSVER}-`$ESDIR/kernel/mpstate`.o"
		ml ld -c $ESDIR/kernel/$KMOD -p atalk
	fi
fi

# HP-UX AppleTalk STREAMS modules
if test -f $ESDIR/.hppa
then
	rm -f /dev/ddp
	major=`/usr/sbin/lsdev -h -d ddp | awk '{print $1}'`
	if test -n "$major"
	then
		mknod /dev/ddp c 72 $major
		chmod 666 /dev/ddp
	fi
fi

# Apple Rhaposdy does not need atalkd
if test -f $ESDIR/.rhappc
then
	NEEDATALKD=n
	$ESDIR/etc/forall lpd kill
fi

# DGUX Intel systems take the machine ID from the first network interface
# configured for IP. To be able to access the machine ID from non root
# programs (pstext, layout) we change the mode on the device here.
if test -f /dgux
then
	chmod 0666 /dev/`netstat -i |sed 1d |awk '$1 !~ "loop" {print $1}'|sed 1q`
fi

# Purge orphan entries from stmp file.
$ESDIR/swho -p >/dev/null

# remove temporary files if they killed admsrv the hard way
for i in ptmp gtmp atmp vtmp stmp
do
	rm -f /etc/$i $ESDIR/conf/$i /var/yp/$i /etc/yp/$i
done

# Sun & S5R4: remove shared segment if they killed afpsrv the hard way
rm -f $ESDIR/afpmem*

if test $SILENT = n
then
	(echo $en 'starting appletalk network:'$ec )	>/dev/console
fi

if test $NEEDATALKD = y
then
	# Atalkd must be running for any other service to start
	if test $SILENT = n
	then
		(echo $en " atalkd"$ec )		>/dev/console
	fi
	$ESDIR/atalkd
fi

# Build a Versions file for the Mac Admin Versions command. This has
# to be done after starting atalkd to get the AppleTalk network
# configuration.
(
	trap '' 1 2 15
	$ESDIR/etc/update-versions &
)

for i in $SERVERS
do
	if test -f $ESDIR/$i
	then
		if test $SILENT = n
		then
			(echo $en " $i"$ec )		>/dev/console
		fi
		$ESDIR/$i
	fi
done

if test $SILENT = n
then
	(echo '.')					>/dev/console
fi

if test -f $ESDIR/rebuild
then
	# Just open each public volume, this causes desksrv to examine the
	# consistency of the desktop and start a real rebuild if necessary.
	awk -F: '/^[^#]/ && /readwrite/ { print $1 }' $ESDIR/conf/afpvolumes | while read dir
	do
		$ESDIR/rebuild -n -o "$dir"
	done
fi
