#! /bin/sh -f
#
# 	 "@(#)startp2p.sh 1.13     93/10/14 SMI"
#
#        Copyright (c) 1993, Sun Microsystems, Inc.  All Rights Reserved.
#        Sun considers its source code as an unpublished, proprietary
#        trade secret, and it is available only under strict license
#        provisions.  This copyright notice is placed here only to protect
#        Sun in the event the source is deemed a published work.
# 
#        RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#        Government is subject to restrictions as set forth in subparagraph
#        (c)(1)(ii) of the Rights in Technical Data and Computer Software
#        clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#        NASA FAR Supplement.

# Start up a SunLink SNAP2P server.
#
# The first parameter is the logical name of the gateway.
# The names of all configured gateways can be found in /etc/appcs or
# in the NIS/NIS+ database (ypcat/niscat appcs.byname).
#
# The second parameter is the p2pconf input file name.
#
#

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/etc:/opt/SUNWconn/bin:
export PATH
ME=`id | awk '{proc = substr($0,7,4); if (proc = "root") print proc }'`
if [ "$ME" != "root" ]
then
    echo "You must be root to run this script."
    exit 1
fi

if [ $1 ]
then
	HOST="$1"
else
	echo "Gateway name and p2pconf input filename missing."
	echo "Usage:  startp2p <gateway> <p2pconf file>"
	exit 2
fi

if [ $2 ]
then
	if [ -f "$2" ]
	then 
		P2PFILE="$2"
	else
		echo "could not find p2pconf input file."
		echo "Usage:  startp2p <gateway> <p2pconf file>"
		exit 3
	fi
else
	echo "p2pconf input filename missing."
	echo "Usage:  startp2p <gateway> <p2pconf file>"
	exit 4
fi

#
#  see if any appc processes to kill
#
echo "Checking for appc $HOST process currently running...."
gateway=$HOST
psid=`ps -ef | awk 'NF >= 3 { if (($(NF-2) == "./appc" || $(NF-2) == "appc")  && $(NF) == gateway) print $2}' gateway=$1`
if test -n "$psid"
then
    psstr=`ps -ef | awk 'NF >= 3 { if (($(NF-2) == "./appc" || $(NF-2) == "appc")  && $(NF) == gateway) print $0 }' gateway=$1`
    echo "$psstr"
    echo "Do you want to kill the above process? [y or (n)]:  "
    read ans
    if test "$ans" = "y"
    then
		echo "kill $psid"
		kill $psid
    else
		echo "Exit startp2p"
		exit
    fi
fi

#
#  start gateway
#

echo "Starting appc gateway $HOST"
#
#  configure gateway (This is started when appc sends SIGUSR2)
#
trap "p2pconf $HOST $P2PFILE" USR2
trap "exit" CLD
appc -s $HOST &
sleep 8
