#!/bin/ksh
#
#ident	"@(#)CON5_net_6.stf	1.4	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the CON5_net_6 test module.
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################


# variables
prgdir=../../prgs	# program directory
maxwarn=50		# max number of nonfatal warnings to tolerate.
srcdir=/tmp/CON5src.$$	# Source directory
targdir=${1:-/mnt/nfstest}	# Target Directory
remotemount=$2		# Remote share
inst=1			# number of instances (instances handled by staf)

ipv6addrfile="/tmp/ipv6.addr.dat"       # This file stores IPv6 addresses
                                        # Configured by this script
multiportfile=/tmp/multiportfile

# functions

clean_up()
{
	echo "CON5_net_6 Ending."
	rm -rf $srcdir
	rm -rf $targdir/*
}

### MAIN ###

# set trap
trap 'clean_up $sdir $targdir; exit' 17

# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
# ( This only makes sense if this script echos warning messages, but
#   inclusion of this variable in the log file is *MANDATORY* in all cases. )
#echo "maxwarnings=${maxwarn}"

((hours=$RUNTIME/3600))
((mins=$RUNTIME%3600))
((mins=$mins/60))
((secs=$RUNTIME%60))
typeset -Z2 mins secs
duration=$hours:$mins:$secs

if [ -s $multiportfile ]
then
		ipv6addrfile=$ipv6addrfile'_'`eval echo $"$#"`
fi

# for bug 4956462, modified some code to check mounting dirs exist or not,
#if not create them. #+ is the modified code
# Check the target is a remote mount
remotehost=`echo $remotemount | cut -f1 -d':'`
if [ "$remotehost" = "IP6ADDR" ]
then
	if [ -r $ipv6addrfile ]
	then
		remotehost=`cat $ipv6addrfile \
				| head -2 | tail -1`
		if [ -z $remotehost ]
		then

			echo "!ERROR: Failed to get IPv6 address."
			exit 1
		fi
	else
		echo "!ERROR: Failed to open $ipv6addrfile."
		exit 1
	fi
elif [ "$remotehost" = "IP64ADDR" ]
then
	if [ -r $ipv6addrfile ]
	then
		remotehost=`cat $ipv6addrfile \
				 | tail -1`
		if [ -z $remotehost ]
		then

			echo "!ERROR: Failed to get IPv6 over IPv4 tunnel address."
			exit 1
		fi
	else
		echo "!ERROR: Failed to open $ipv6addrfile."
		exit 1
	fi
else
	echo "!ERROR: $0 called with bad arguments."
	exit 1
fi

remotedir=`echo $remotemount | cut -f2 -d':'` 
if [ ! -z "`df -k $targdir`" ]
then
	umount $targdir > /dev/null 2>&1
fi
target_mount=`df -k $targdir | tail -1 | cut -f1 -d ' ' | grep ${remotehost}`
if [ -z "$target_mount" ]
then
        if [ ! -d $targdir ] 
        then 
                mkdir $targdir 
        fi 
        rsh $remotehost "if [ ! -d $remotedir ] 
                        then mkdir $remotedir 
                        fi 
                        " 
	mount [$remotehost]:$remotedir $targdir
	if [ $? -ne 0 ]
	then
		echo "!ERROR: NFS share [$remotehost]:$remotedir is not mounted on $targdir."
		exit
	fi
fi

echo "Starting CON5 test for $duration..."

# Make working copy of /kernel
mkdir -p $srcdir
cp -rf /kernel $srcdir

# Remove old log dir
rm -rf $targdir/log $targdir/dcout* $targdir/tbase*

cd CON5/bin
./CON5repeat -nofsck $srcdir $targdir $duration $inst
