#!/bin/sh

# $Id: startsrv.sh,v 1.10 2001/03/24 21:17:40 vixie Exp $

# Copyright (c) 1996 by Internet Software Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
# CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.

default_options='-b 9600 -w 8 -p none'
default_sock_prot='ug=rw,o='
default_sock_owner='root:wheel'
default_log_prot='u=rw,g=r,o='

for host
do
	echo -n "startsrv($host):"
	#
	# kill any existing ttysrv on this port
	#
	if [ -f /usr/local/rtty/pid/$host ]; then
		pid=`cat /usr/local/rtty/pid/$host`
		while ps w$pid >/tmp/startsrv$$ 2>&1
		do
			grep -q ttysrv /tmp/startsrv$$ && {
				echo -n " $pid killed"
				kill $pid
				sleep 1
			} || {
				break
			}
		done
		rm /usr/local/rtty/pid/$host /tmp/startsrv$$
	fi
	#
	# start up a new one
	#
	if [ -s /usr/local/rtty/opt/${host}.srv ]; then
		options=`cat /usr/local/rtty/opt/${host}.srv`
	elif [ -s /usr/local/rtty/opt/DEFAULT.srv ]; then
		options=`cat /usr/local/rtty/opt/DEFAULT.srv`
	else
		options="$default_options"
	fi

	if [ -s /usr/local/rtty/prot/${host}.sock ]; then
		sock_prot=`cat /usr/local/rtty/prot/${host}.sock`
	elif [ -s /usr/local/rtty/prot/DEFAULT.sock ]; then
		sock_prot=`cat /usr/local/rtty/prot/DEFAULT.sock`
	else
		sock_prot="$default_sock_prot"
	fi

	if [ -s /usr/local/rtty/owner/${host}.sock ]; then
		sock_owner=`cat /usr/local/rtty/owner/${host}.sock`
	elif [ -s /usr/local/rtty/owner/DEFAULT.sock ]; then
		sock_owner=`cat /usr/local/rtty/owner/DEFAULT.sock`
	else
		sock_owner="$default_sock_owner"
	fi

	if [ -s /usr/local/rtty/prot/${host}.log ]; then
		log_prot=`cat /usr/local/rtty/prot/${host}.log`
	elif [ -s /usr/local/rtty/prot/DEFAULT.log ]; then
		log_prot=`cat /usr/local/rtty/prot/DEFAULT.log`
	else
		log_prot="$default_log_prot"
	fi

	rm -f /usr/local/rtty/sock/$host /usr/local/rtty/pid/$host
	/usr/local/rtty/bin/ttysrv $options \
		-t /usr/local/rtty/dev/$host \
		-s /usr/local/rtty/sock/$host \
		-l /usr/local/rtty/log/$host \
		-i /usr/local/rtty/pid/$host \
		> /usr/local/rtty/out/$host 2>&1 &
	echo -n " "
	tries=5
	while [ $tries -gt 0 -a ! -f /usr/local/rtty/pid/$host ]; do
		echo -n "."
		sleep 1
		tries=`expr $tries - 1`
	done
	if [ ! -f /usr/local/rtty/pid/$host ]; then
		echo " [startup failed]"
	else
		newpid=`cat /usr/local/rtty/pid/$host`
		chmod $sock_prot /usr/local/rtty/sock/$host
		chown $sock_owner /usr/local/rtty/sock/$host
		chmod $log_prot /usr/local/rtty/log/$host
		echo " $newpid started"
	fi
done

exit


syntax highlighted by Code2HTML, v. 0.9.1