#!/bin/csh -f
#
# @(#)startsna.sh	8.19 8.19 94/11/01 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 3270 server.  This scripts starts an instance
# of an SNA 3274 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/3270s or
# in the NIS database (ypcat 3270s.byname).
#
# DIR3270= directory_name  -- basename portion of dir where startsna was found
# SUNLINK= directory_name  -- dir for $DIR3270/startsna eg, /opt/SUNWconn
# P3274=   gateway_name    -- name of the SNA gateway to be started
# S3274=   dlc driver info      
# S3274 is of the form /dev/sdlc or /dev/llc
# M3274=   physical driver info  
# M3274 is of the form /dev/<physical driver name><N> where N is the instance #
#

#
# Set tcp keep-alive interval to 2 minutes.
#
/usr/sbin/ndd -set /dev/tcp tcp_keepalive_interval 180000 

set MATCH=0
set path=( $path )

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

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

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

cd $p

setenv XTWAIT 10
set S32DIR=/etc/SUNWconn/sna3270
set CFGDIR=$S32DIR/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: startsna <gateway_name>"
	exit 1
endif

set gateway=$1

set p=`ps -ef | awk '{proc = substr($0, 48, 59); if (proc == "sna3274 '$gateway'") print $2}'`
if ( "$p" != "" ) then
        echo "Gateway '$gateway' is running. Can't start sna3274."
        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/sna3270/gateway directory.  This
# will set the P3274, C3274 and S3274 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 P3274 environment variable (gateway_name) must be defined for
# this instance of the gateway.
#
if ( $?P3274 == 0 ) then
	echo "Can't start sna3274 - P3274 must be defined"
	exit 1
endif

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

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

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

if ( -e $S3274 ) then
	else
		echo "$S3274 does not exist"
		exit 1
	endif
	
#
# Set the SNA logfile to be: /etc/SUNWconn/sna3270/snalog.gateway_name
#
set LOGFILE=$S32DIR/snalog.$P3274
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 sna3270 gateway $P3274 ..." > $LOGFILE
echo "    envset file: $ENVFILE" >> $LOGFILE
echo "    configuration file: $C3274" >> $LOGFILE
echo "    data link control device driver: $S3274" >> $LOGFILE
echo "    medium access control device driver: $M3274" >> $LOGFILE
echo "    logfile size: $SNALOGSZ" Kbytes >> $LOGFILE
echo " " >> $LOGFILE

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