#! /bin/csh -f
#
# %CSH_PRELUDE%
# Prelude.csh
#
# This is a shell "prelude" that finds where the script it is included in
# was exec'ed from and sets PATH so subsequent exec's use commands from same
# release image.
#
# This prelude is included into all csh scripts at install time by
# Makefile.csh which copies the following text after any line in
# the csh script that contains "%CSH_PRELUDE%".
#
set prog=$0
# The command as executed by the user
set progname=$prog:t
if ($prog:h == $prog) then
    foreach i ($path)
	if (-x $i/$prog) then
	    set prog=$i/$prog
	    goto resolveLinks
	endif
    end
    echo "${prog}: Couldn't determine package home"
    exit 1
endif
resolveLinks:
if (! $?hardpaths) set nohardpaths
set dir=$cwd
set hardpaths
while (1)
    if ($prog:h != $prog) then
	cd $prog:h
    endif
    set lsout=(`/usr/bin/ls -l $prog:t`)
    if ("$lsout[1]" !~ l*) break
    set prog=$lsout[$#lsout]
end
cd ..
setenv PACKAGE_HOME $cwd
if ($?nohardpaths) unset hardpaths
cd $dir
setenv PROGRAM ${prog:t}
set path=($PACKAGE_HOME/bin $PACKAGE_HOME/sbin /bin /sbin)
#
# End of Prelude.csh
#
setenv PROGRAM "vsmaudit"
set path=($path /usr/lib/osa/bin /usr/sbin)
set installdir=$PACKAGE_HOME/install
set devs=()
set osrel="5.6"
set osvers="Generic_105181-12"
set bootlevel=(02.05.01.00)
set firmlevel=(02.05.02.14 02.05.06.32)
set patches=( 106513-02 )

#
# Find a1000s on legal busses
#
set a1000s=(`vsmfsinfo -r $devs`)
if ($status != 0 || $#a1000s == 0) then
    logmsg "\nNo A1000s found on system\n"
    goto abort
endif
logmsg "\nFound %d A1000's, is that expected number [y/n]? " $#a1000s
set ok=$<
if ($ok !~ y*) then
    logmsg "\
Please verify:\
     SCSI controller(s) in proper PCI slot\
     SCSI buses properly terminated with DIFFENTIAL terminator\
     A1000(s) connected to proper SCSI buses\
     A1000(s) have proper target number(s)\
     A1000(s) powered on\n"
    goto abort
endif

set fsnames=( `vsmfsinfo -nx $a1000s` )
onintr cleanup

#
# Verify SUNWbwsr package installed correctly
#

set shm=(`rmparamck -S`)
switch ($status)
case 0:
    breaksw
case 9:
    goto abort
default:
    logmsg "\nWARNING: /etc/system not modified with Media Central mods\n"
    if (-r $installdir/vsm_system \
	    && -r $installdir/i.vsm_system) then
	logmsg "\nInstall /etc/system mods now [y/n]? "
	set resp=$<
	if ($resp =~ y*) then
	    echo "$installdir/vsm_system /etc/system" | \
		sh $installdir/i.vsm_system
	    touch /reconfigure
	    set mustreboot=1
	else
	    set badconfig=1
	endif
    else
	set badconfig=1
    endif
    breaksw
endsw

set shm=(`rmparamck -R`)
switch ($status)
case 0:
    breaksw
case 9:
    goto abort
default:
    logmsg "\nWARNING: /etc/osa/rmparams not modified with Media Central mods\n"
    if (-r $installdir/vsm_rmparams && -r $installdir/i.vsm_rmparams) then
	logmsg "\nInstall /etc/osa/rmparams mods [y/n]? "
	set resp=$<
	if ($resp =~ y*) then
	    echo "$installdir/vsm_rmparams /etc/osa/rmparams" | \
		sh $installdir/i.vsm_rmparams
	    touch /reconfigure
	    set mustreboot=1
	else
	    set badconfig=1
	endif
    else
	set badconfig=1
    endif
    breaksw
endsw


#
# Determine platform
#
set platform="`uname -i`"
case "SUNW,Ultra-60":
case "SUNW,Ultra-250":
    set platform=E250
    set nA1000 = 2
    breaksw
case "SUNW,Ultra-4":
case "SUNW,Ultra-450":
    set platform=E450
    set nA1000 = 4
    breaksw
default:
    logmsg "NOTE: Unsupported platform: %s\n" $platform
    set nA1000 = 4
    set badconfig=1
    breaksw
endsw


#
# Verify that correct version of Solaris and patches installed.
#
set xrel=(`uname -r`)
if ("$xrel" != "$osrel") then
    logmsg "WARNING: OS Release %s not expected release %s\n" $xrel $osrel
    set badconfig=1
else
    set osnum=`expr $osvers : "\(.*\)-.*"`
    set oslev=`expr $osvers : ".*-\(.*\)"`
    set xvers=(`uname -v`)
    set xnum=`expr $xvers : "\(.*\)-.*"`
    set xlev=`expr $xvers : ".*-\(.*\)"`
    if ("$xnum" != "$osnum" || $xlev < $oslev) then
	logmsg "WARNING: OS Version %s not at expected level of %s\n" \
		$xvers $osvers
	set badconfig=1
    endif
    foreach i ( $patches )
	set pnum=`expr $i : "\(.*\)-.*"`
	set plev=`expr $i : ".*-\(.*\)"`
	set patch=(`patchadd -p | awk '{ print $2 }' | grep $pnum`)
	if ($#patch < 1) then
	    logmsg "WARNING: Required patch %s not installed\n" $i
	    set badconfig=1
	else
	    set patch=$patch[$#patch]
	    set xlev=`expr $patch : ".*-\(.*\)"`
	    if ($xlev < $plev) then
		logmsg "WARNING: Patch %s not at expected level of -%s\n" \
			$patch $plev
		set badconfig=1
	    endif
	endif
    end
endif


#
# Verify all A1000s have correct firmware and nvram values
#
foreach i ($a1000s)
    set fwlevel=(`raidutil -c $i -i | grep "Boot Level"`)
    foreach fw ($bootlevel)
	if ($fwlevel[3] == $fw) then
	    goto bwok
	endif
    end
    logmsg "WARNING: A1000 %s Bootware %s incorrect revision\n" $i $fwlevel[3]
    set badfirmware=1
bwok:
    set fwlevel=(`raidutil -c $i -i | grep "Firmware Level"`)
    foreach fw ($firmlevel)
	if ($fwlevel[3] == $fw) then
	    goto fwok
	endif
    end
    logmsg "WARNING: A1000 %s Firmware %s incorrect revision\n" $i $fwlevel[3]
    set badfirmware=1
fwok:
    nvutil -v -f $i >& /tmp/$$VsmNewFs
    if ($status != 0) then
	logmsg "NOTE: A1000 %s nvram parameters corrected\n" $i
	cat /tmp/$$VsmNewFs
	set powercycle=1
    endif
    rm -f /tmp/$$VsmNewFs
    set nvinfo=(`nvutil -o 2B $i`)
    if ($nvinfo[2] != 0x2b) then
	logmsg "FATAL: Unexpected nvutil output\n"
	set abortconfig=1
    else if ($nvinfo[3] != 0xb2) then
	set powercycle = 1
	logmsg "NOTE: Updating nvram cache block size on %s..." $i
	nvutil -o 2B=B2 $i >& /tmp/$$VsmNewFs
	if ($status != 0) then
	    logmsg "FATAL: nvutil -o 2B=B2 %s FAILED\n" $i
	    cat /tmp/$$VsmNewFs
	    \rm -f /tmp/$$VsmNewFs
	    exit 1
	endif
	logmsg "Done.\n"
	\rm -f /tmp/$$VsmNewFs
    endif
end
if ( $?badfirmware ) then
    set badconfig=1
    logmsg "\nWARNING: A1000 Bootware Revision should be one of:\
%s\n" "$bootlevel"
    logmsg "WARNING: A1000 Firmware Revision should be one of:\
%s\n" "$firmlevel"
    logmsg "WARNING: Please update ALL A1000s with rm6 application\n"
endif

#
#   Foreach A1000 print out the manufacturer information for the drives
#
foreach a1000 ($a1000s)
logmsg "\n%s\n" $a1000
logmsg "=========\n"
drivutil -i $a1000 | \
awk ' { \
if ($0 ~ /drivutil/) \
printf("\n") \
else if ($1 ~ /Location/) \
printf("Location  Capacity(MB)    Status     Vendor  Product ID            Serial Num\n") \
else if ($1 ~ /^\[.*/) \
printf("%5s %13s %13s %10s %10s %15s\n", $1, $2, $3, $4, $5, $7) \
else if ($1 ~ /.*Drive.*/) \
printf("%s\n", $0) \
else \
printf("\n") }'
end



#
# Verify all A1000s are correctly populated with drives
#
set noglob
set cap0=("x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x")
foreach a1000 ($a1000s)
    set tnum=( `expr $a1000 : 'c[0-9]t\([0-9]\)d0s0'` )
    if ($#tnum != 1) then
	logmsg "FATAL: Illegal device name: %s\n" $a1000
	exit 1
    endif
    @ tnum = $tnum + 1
    set foo=(`drivutil -i $a1000 | awk '/^\[/ { print $1 " " $2 " " $3}'`)
    set drives1=("q" "q" "q" "q" "q" "q")
    set drives2=("q" "q" "q" "q" "q" "q")
    while ($#foo > 0)
	set loc=$foo[1]
	set cap=$foo[2]
	set stat=$foo[3]
	shift foo; shift foo; shift foo
	set bus=`expr "$loc" : '\[\([0-9]\),[0-9]\]'`
	set drive=`expr "$loc" : '\[[0-9],\([0-9]\)\]'`
	if ($drive >= 0 && $drive <= 4) then
	    if ($cap0[$tnum] == "x") then
		set cap0[$tnum]=$cap
	    else if ($cap != $cap0[$tnum]) then
		@ target = $tnum - 1
		logmsg "FATAL: All drives in A1000s with target number %s \
not of same capacity\n" $target
		set abortconfig=1
	    endif
	    if ($stat != Optimal) then
		logmsg "FATAL: Drive %s in A1000 %s in state %s\n" \
			$loc $a1000 $stat
		set abortconfig=1
	    endif
	endif
	@ drive = $drive + 1
	if ($bus == 1) then
	    set drives1[$drive] = "x"
	else
	    set drives2[$drive] = "x"
	endif
    end
    set sub=0
    foreach i (0 1 2 3 4)
	@ sub = $i + 1
	if (z"$drives1[$sub]" != zx) then
	    logmsg "FATAL: A1000 %s Drive \[1,%s\] not present\n" $a1000 $i
	    set abortconfig=1
	endif
	if (z"$drives2[$sub]" != zx) then
	    logmsg "FATAL: A1000 %s Drive \[2,%s\] not present\n" $a1000 $i
	    set abortconfig=1
	endif
    end
end
unset noglob

#
# Map out matrix of controllers and targets
#
set targets=("" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
foreach i ($a1000s)
    set tnum=( `expr $i : 'c[0-9]t\([0-9]\)d0s0'` )
    if ($#tnum != 1) then
	logmsg "FATAL: Illegal device name: %s\n" $i
	exit 1
    endif
    @ tnum = $tnum + 1
    set targets[$tnum] = "$targets[$tnum] $i"
end
set ctlrs=("" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
foreach i ($a1000s)
    set cnum=( `expr $i : 'c\([0-9]\)t[0-9]d0s0'` )
    if ($#cnum != 1) then
	logmsg "FATAL: Illegal device name: %s\n" $i
	exit 1
    endif
    @ cnum = $cnum + 1
    set ctlrs[$cnum] = "$ctlrs[$cnum] $i"
end
set i = 0
set nCtlrs = 0
while ($i < $#ctlrs)
    if ("$ctlrs[$i]" != "") then
	@ nCtlrs = $nCtlrs + 1
    endif
    @ i = $i + 1
end

#
# Verify that targets are correctly assigned on each controller
#
set t0=($targets[1])
if ($#t0 == 0) then
    logmsg "FATAL: No A1000s found with target 0\n"
    set abortconfig=1
else if ($#t0 != $nCtlrs) then
    logmsg "FATAL: A1000s have invalid SCSI target numbers.\
Please refer to Media Central Installation Document\
for correct installation procedure.\n"
    set abortconfig=1
else
    if ($#t0 != $nA1000) then
	@ nA1000 = $nA1000 / 2
	if ( $#t0 == $nA1000 ) then
	    logmsg "\
NOTE: Found %s A1000s with target 0.  This configuration is a\
reduced bandwidth configuration for the %s.  Bandwidth capacity\
cannot be increased without reloading of all content.\n" $#t0 $platform
	else
	    logmsg "WARNING: Expected %s A1000s with target 0, found %s\n" \
		    $nA1000 $#t0
	    logmsg "WARNING: Devices found were %s\n" $t0
	    set nA1000=$#t0
	    set badconfig=1
	endif
    endif
    foreach i (1 2 3 4 5 6 7)
	@ t = $i + 1
	set t=($targets[$t])
	if ($#t != $nA1000) then
	    if ($#t == 0) then
		set nA1000=0
	    else if ($nA1000 != 0) then
		logmsg "FATAL: Incorrect number of A1000s at target %s\n" $i
		set abortconfig=1
	    else 
		logmsg "FATAL: A1000 Target numbers not contiguously assigned\n"
		set abortconfig=1
	    endif
	endif
    end
endif

#
# Verify Solaris installed correctly with enough memory and swap
#
if ( ! { vsmswapck } ) then
    set badconfig=1
endif



#
# Startup  VSM server if not already running
#

vsminfo 1>&/dev/null
if ($status != 0) then 
    logmsg "Did not find vsmd process, starting vsmd ..\n"
    vsminit start
    #
    # Check to make sure vsmd is running
    #
    if ($status != 0) then 
	logmsg "vsminit failed ..\n"
	set abortconfig=1
    else 
	logmsg "vsmd started ..\n"
    endif	
endif

#
# If config errors detected, print out appropriate message
#
if ( $?abortconfig ) then
    logmsg "\nFATAL configuration errors detected.\
Correct all FATAL errors before reattempting configuration.\n"
    set doexit=1
else if ( $?badconfig ) then
    logmsg "\nConfiguration errors detected.\
Recommend that errors be corrected before proceeding.\n"
endif
if ($?powercycle) then
    logmsg "\
A1000 nvram updated.\
You must now halt machine (init 0)\
Then, POWER CYCLE ALL A1000s.\
Finally, reboot Solaris and rerun this command\
\nDo system halt now? [y/n]? "
    set ok=$<
    if ($ok =~ y*) then
	/sbin/init 0
	sleep 5
	exit 0
    endif
    logmsg "WARNING: ALL A1000s MUST be power off and back on before\
reattempting configuration.\n"
    set doexit=1
else if ($?mustreboot) then
    logmsg "\
System configuration files modified.\
You MUST reboot the system before resuming configuration.\
After system reboots, rerun this command.\
\nDo reboot now? [y/n]? "
    set ok=$<
    if ($ok =~ y*) then
	/sbin/init 6
	sleep 5
	exit 0
    endif
    logmsg "WARNING: System must be rebooted before resuming configuration\n"
    set doexit=1
else if ( $?badconfig && ! $?doexit ) then
    logmsg "\nTerminate configuration [y/n]? "
    set ok=$<
    if ($ok !~ n*) then
	set doexit=1
    endif
endif
if ( $?doexit ) then
    goto abort
endif

#
# Check to make sure system total throughput is OK
#

set a1000s=( c1t0d0s0 c2t0d0s0 )
foreach fsname ($fsnames)
    set segments=( `vsminfo -f $fsname | grep "Segments"` )
    set rate=( `vsmfsinfo -N $fsname -a | grep "Rate Limit"` )
    set erate=`expr $segments[2] \* 875 \* 100000` 
    set trate=`expr $rate[3] : "\(.*\)\..*"`
    if ( `expr $trate \* 1000000` <  $erate ) then
       logmsg "FATAL: System bit rate mismatch on %s, check configuration\n" $fsname
    set badconfig=1
   endif
end



#
# Check if enough files can be created to run streaming tests
#
set inodeinfo=( `vsminfo | grep "Total/Used Inodes"`)
set maxinodes=$inodeinfo[3]
set usedinodes=$inodeinfo[5]
set minrate=( `vsmfsinfo -N $fsname -a | grep "Min Rate"` )
set intrate=`expr $minrate[3] : "\(.*\)\..*"`
set fractionrate=`expr $minrate[3] : ".*\.\(.*\)"`
set fractionrate=`echo $fractionrate | sed 's/0*$//g'`

set x=1
set l=( `echo $fractionrate | awk '{ print length($0) }'` )
@ l = 6 - $l 
while ($l > 0)
    @ x = $x * 10
    @ l = $l - 1
end

@ numfiles = $erate / ( ($intrate * 1000000) + ($fractionrate * $x) )
@ totalfiles = $usedinodes + $numfiles

if ( $totalfiles >= $maxinodes ) then
	logmsg "FATAL : Large number of files exist on the system\
		%s will be unable to run all the functionality tests\
		.. aborting\n" `basename $0`
	goto abort	
else 
	sleep 1
endif

#
# Start streaming tests .. run tests at the system minimum rate
#
foreach fsname ($fsnames)
    logmsg "\n%s running streaming tests\n" `basename $0`
    logmsg "Streaming tests may take a few minutes to complete. OK? [y/n] " 
    set ok=$<
    if ($ok !~ y*) then
	goto abort
    endif
    vsmsessioniock -f $fsname -b $minrate[3] -t 1 -l 120 -r 
    switch ($status)
    case 0:
	logmsg "%s completed successfully\n" `basename $0`
	goto cleanup
    case 2:
	set exiterror=1
	goto cleanup
    default:
	logmsg "%s unable to run streaming tests\n" `basename $0`
	logmsg "WARNING : Please check system configuration before resuming\n"
	exit $status
    endsw
end

cleanup:
	foreach fsname ($fsnames)
	    set filenames= (`vsmls -f $fsname | grep "Name"` )
	    if ($status == 0) then 
		if ($#filenames > 0) then
	        @ i = $#filenames
		while ($i > 0)
		set fname=(`echo $filenames[$i] | sed 's/\""*//g'`)
		echo $fname | grep "^vsmsessioniock" >& /dev/null
		if ($status == 0) then 
		    vsmrm -f $fsname $fname >& /dev/null
		endif    
		@ i -= 2
		end
		endif
 	    endif	

	end


if ( $?exiterror ) then	
	logmsg "%s detected errors while running streaming tests\n" `basename $0`
	logmsg "Please check system configuration before resuming %s\n" `basename $0`
	exit $exiterror
else 
    exit 0
endif

abort:
logmsg "Command aborted\n"
exit 1
