#!/bin/csh -f
#
# @(#)startsrje.sh	8.12 8.12 93/07/12 Copyright 1986-1993 SMI
#
# Copyright (c) 1985-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.  Dissassembly, decompilation,
# or other means of reducing the object code to human readable form is
# prohibited by the license agreement under which this code is provided
# to the user or company in possession of this copy.
#
# 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 SNA RJE server.  This scripts starts an instance
# of an SNA RJE gateway process.
#
# The first and only parameter is the logical name of the gateway.
# The name of the gateway is configured by the system administrator.
# The names of all configured gateways can be found in /etc/snarjes or
# in the NIS database (ypcat snarjes.byname).
#
# DIR3770= directory_name  -- basename portion of dir where startsnarje was 
#				found
# SUNLINK= directory_name  -- dir for $DIR3770/startsnarje eg, /opt/SUNWconn
# P3770=   gateway_name    -- name of the SNA RJE gateway to be started
# S3770=   dlc driver info      
# S3770 is of the form /dev/sdlc or /dev/llc
# M3770=   physical driver info  
# M3770 is of the form /dev/<physical driver name><N> where N is the instance #
#

set MATCH=0
set path=( $path )

foreach p ($path)
	if (-x $p/sna3770) then
		if (-x $p/snalogger) then
			set MATCH=1
			break
		endif
	endif
end

if ($MATCH == 0) then
	echo "Can't find sna3770 and snalogger in user's path."
	echo "Trying /opt/SUNWconn/bin ..."
	if (-x /opt/SUNWconn/bin/sna3770) then
		if (-x /opt/SUNWconn/bin/snalogger) then
			set p=/opt/SUNWconn/bin
			set path=( $p $path )
		endif
	endif
endif

echo "Using sna3770 and snalogger in $p/ ..."

cd $p

set XTWAIT=10
set SRJEDIR=/etc/SUNWconn/sna3770
set CFGDIR=$SRJEDIR/gateway

set idname=`id | awk '{proc = substr($0, 7, 4); if (proc == "root") print proc}'`

if ( $idname != "root") then
	echo "You must be root to run this script."
	exit 1
endif

#
# A gateway_name is a required parameter.
#
if ( "$1" == "" ) then
	echo "Usage: startsnarje <gateway_name>"
	exit 1
endif

set gateway=$1

set p=`ps -ef | awk '{proc = substr($0, 48, 59); if (proc == "sna3770 '$gateway'") print $2}'`

if ( "$p" != "" ) then
        echo "Gateway '$gateway' is running. Can't start sna3770."
        echo "If you want to start gateway '$gateway', stop the gateway first."
        exit 1
endif

set ENVFILE="No envset.$gateway found."

#
# The envset.gateway_name file will be executed if it exists in the
# current directory or /etc/SUNWconn/sna3770/gateway directory.  This
# will set the P3770, C3770 and S3770 environment variables to the
# values specified in the envset.gateway_name file.
#
if ( -e envset.$gateway ) then
	source envset.$gateway
	set ENVFILE="envset.$gateway"
else
	if ( -e $CFGDIR/envset.$gateway ) then
		source $CFGDIR/envset.$gateway
		set ENVFILE="$CFGDIR/envset.$gateway"
	endif
endif

#
# The P3770 environment variable (gateway_name) must be defined for
# this instance of the gateway.
#
if ( $?P3770 == 0 ) then
	echo "Can't start sna3770 - P3770 must be defined"
	exit 1
endif

#
# The S3770 environment variable (port to be used) must be defined for
# this instance of the gateway.
#
if ( $?S3770 == 0 ) then
	echo "Can't start sna3770 - S3770 must be defined"
	exit 1
endif

#
# The M3770 environment variable for physical driver must be defined
#
if ( $?M3770 == 0) then
	echo "Can't start sna3770 - M3770 must be defined"
	exit 1
endif

#
# The gateway_name specified must match the environment variable
# for this instance of the gateway.
#
if ( "$gateway" != "$P3770" ) then
	echo "Can't start sna3770 - configuration error"
	exit 1
endif

if ( -e $S3770 ) then
	else
		echo "$S3770 does not exist"
		exit 1
	endif
	
#
# Set the SNA logfile to be: /etc/SUNWconn/sna3770/snalog.gateway_name
#
set LOGFILE=$SRJEDIR/snalog.$P3770
if ( $?SNALOGSZ == 0 ) then
	set SNALOGSZ=2048
endif

if ( $?FILENUM == 0) then
	set FILENUM=1
endif

#
# Save the following info in the SNA logfile.
#
echo "Starting sna3770 gateway $P3770 ..." > $LOGFILE
echo "    envset file: $ENVFILE" >> $LOGFILE
echo "    configuration file: $C3770" >> $LOGFILE
echo "    data link control device driver: $S3770" >> $LOGFILE
echo "    medium access control device driver: $M3770" >> $LOGFILE
echo "    logfile size: $SNALOGSZ" Kbytes >> $LOGFILE
echo " " >> $LOGFILE

#
# Start the specified SNA RJE gateway and redirect both standard output
# and standard error to the end of the SNA logfile.
#
sna3770 $gateway | snalogger $SNALOGSZ $LOGFILE $FILENUM


