#! /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 "vsmextfs"
set path=($path /usr/lib/osa/bin /usr/sbin)
set installdir=$PACKAGE_HOME/install
switch ( "$progname" )
case "vsmnewfs":
    set op=new
    breaksw
case "vsmextfs":
    set op=ext
    breaksw
default:
    set op=ext
    breaksw
endsw
set devs=()
while ($#argv > 0)
    if ("$argv[1]" =~ -*) then
	if ($#devs > 0) then
	    logmsg "Usage: %s [-eiIL] [cXtXdXsX-list]\n" $0
	    exit(1)
	endif
	switch ("$argv[1]")
	case "-e":
	    set op=ext
	    breaksw
	case "-i":
	    set op=new
	    breaksw
	case "-I":
	    set noinit
	    breaksw
	case "-L":
	    set noinit
	    set nolabel
	    breaksw
	default:
	    logmsg "%s: Illegal option: %s\n" $0 $argv[1]
	    exit(1)
	    breaksw
	endsw
    else
	set devs=($devs $argv[1])
    endif
    shift
end
set osrel="5.6"
set osvers="Generic_105181-12"
set bootlevel=(02.05.01.00 02.05.06.32)
set firmlevel=(02.05.02.14 02.05.02.32)
set patches=( 106513-02 )
vsmshutdown -qp
if ($status != 0) then
    logmsg "\
Media Central Server is currently operating.\
Media Central Server must be shutdown to initialize storage.\
\nShutdown Media Central Server [y/n]? "
    set ok=$<
    if ($ok !~ y*) goto abort
    logmsg "Shutting Down Media Central Server..."
    mcsinit stop
    while (1)
	sleep 2
	vsmshutdown -qp
	if ($status == 0) break
	printf "." 
    end
    logmsg "Shutdown Complete\n"
endif
#
# Find a1000s on legal busses
#
set a1000s=(`vsmfsinfo -rq $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
logmsg "\
A1000 performance characterization relies on A1000 write caches being\
enabled.  Write cache state must be checked EACH time this program\
is run.\
\
Have you verified write cache is enabled on ALL A1000s [y/n]? "
set ok=$<
if ($ok !~ y*) then
    logmsg "\
Please use /usr/lib/osa/bin/rm6 to verify, see Media Central\
Installation manual for further details.\n"
    goto abort
endif
#
# 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`"
switch ("$platform")
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 Solaris installed correctly with enough memory and swap
#
if ( ! { vsmswapck } ) then
    set badconfig=1
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
#
# 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
#
# 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
#
# Get active targets
#
set tinit
set fsnames=(`vsmfsinfo -nxq $a1000s`)
set exitstat=$status
if ($exitstat != 0 || $#fsnames != 1) then
    if ($op == "ext") then
	if ($exitstat == 0 && $#fsnames > 1) then
	    goto badextend;
	endif
	logmsg "\nNo Media Central file system found for extension\n"
	goto abort
    endif
else
    set fsname=$fsnames[1]
    if ($op == "ext") then
	set lt=(`vsmfsinfo -tlxq -N $fsname $a1000s`)
	if ($status != 0) then
	    goto abort
	endif
	if ($#lt > 0) then
	    logmsg "\nTargets %s have been characterized but not yet\
added to file system %s.  You may optionally redo the\
characterization of these targets.  In either case, they will\
be added to file system when Media Central is restarted.\
Do you wish to redo characterization of these targets [y/n]? " "$lt" $fsname
	    set ok=$<
	    if ($ok =~ y*) then
		set tinit=($lt)
	    endif
	endif
	set tinit=($tinit `vsmfsinfo -tixq $a1000s`)
	if ($#tinit == 0) then
	    logmsg "\nNo A1000s found to extend file system %s\n" $fsname
	    goto abort
	endif
	if ($tinit[1] == 0) then
badextend:
	    logmsg "\
FATAL: The existing A1000 configuration is a reduced bandwidth\
configuration for the %s.  vsmextfs cannot extend capacity\
of this system with additional A1000s having SCSI target number 0.\
\
You may preserve current file system contents and increase storage\
capacity by daisy-chaining new A1000s after all existing A1000s.\
Ensure that target numbers of new A1000s are assigned correctly.\
\
Streaming bandwidth capacity cannot be increased without reloading\
of all content.  Use the command vsmnewfs if this is desired.\n" $platform
	    goto abort
	endif
	logmsg "\
Found additional A1000s having target number(s): %s\
Do you want to extend capacity of %s [y/n]? " $tinit $fsname
	set ok=$<
	if ($ok !~ y*) then
	    goto abort
	endif
    endif
endif
if ($op == "new") then
    #
    # Point of no return
    #
    logmsg "\nWARNING: INITIALIZING AND ERASING the following A1000s\n"
    echo $a1000s
    if ($?fsname) then
	logmsg "\nWARNING: This will DESTROY existing file system: %s\n" $fsname
    else
	logmsg "\nWARNING: This will DESTROY any previous contents\n"
    endif
    logmsg "\nARE YOU SURE want to continue [y/n]? "
    set ok=$<
    if ($ok !~ y*) goto abort
    set tinit=(0 1 2 3 4 5 6 7)
    set fsname=`hostname`
else
    logmsg "\nExtending capacity of file system %s\n" $fsname
endif
#
# Setup proper lun structure on each A1000
#
if ($?noinit) then
    logmsg "\nSkipping A1000 lun setup\n"
else
    logmsg "\nStarting initialization of A1000s\n"
    foreach i ($tinit)
	@ t = $i + 1
	set t=($targets[$t])
	foreach j ($t)
	    rm -f /tmp/vsm.$j.flag /tmp/vsm.$j.log
	    a1000init -p /tmp/vsm.$j.flag $j 256 >& /tmp/vsm.$j.log &
	end
    end
    logmsg "A1000 initialization may take upto 2 hours\n"
    wait
    foreach i ($tinit)
	@ t = $i + 1
	set t=($targets[$t])
	foreach j ($t)
	    if (-r /tmp/vsm.$j.flag) then
		logmsg "\nInitialization of %s failed\n" $j
		cat /tmp/vsm.$j.log
		rm -f /tmp/vsm.$j.flag
		set abortconfig=1
	    endif
	end
    end
    if ( $?abortconfig ) goto abort
    logmsg "A1000 initialization complete\n"
endif
#
# Characterize each "band" of A1000s
#
if ($?nolabel) then
    logmsg "\nSkipping A1000 characterization\n"
else
    foreach i ($tinit)
	@ t = $i + 1
	set t=($targets[$t])
	if ($#t > 0) then
	    set devs=()
	    foreach i ($t)
		set ct=`expr $i : '\(c[0-9]t[0-9]\)d0s0'`
		set devs=($devs /dev/rdsk/${ct}d0s2 /dev/rdsk/${ct}d1s2)
	    end
	    logmsg "\nStarting characterization of %s\n" "$t"
	    logmsg "Characterization may take upto 5 hours per target set\n"
	    vsmlabel -n $fsname $devs
	    if ($status != 0) then
		logmsg "Characterization of %s failed\n" "$t"
		goto abort
	    endif
	endif
    end
    logmsg "A1000 characterization complete\n"
endif
if ($op == "new") then
    logmsg "\nCreating empty Media Central file system\n"
    vsmmkfs -n $fsname -v
    if ($status != 0) then
	logmsg "File system creation failed\n"
	goto abort
    endif
    logmsg "Media Central file system creation complete\n"
else
    logmsg "Media Central file system extension complete\n"
endif
logmsg "%s Completed Successfully\n" ${progname}
exit 0

abort:
logmsg "Command aborted\n"
exit 1
