#!/bin/csh
#   Given a specific drive, run our standard FA on it

set parallel  = "no"

while ($#)
    set linearg = $1;

    switch ($linearg)
    case "-help":
    case "-h":
	echo "usage: $0 [-parallel] <drive> [<drive> ...]"
	echo "  drives can be specified as bay, twed, etc"
	exit;
	breaksw;
    case "-parallel":
	set parallel = "yes";
	shift;
	breaksw;
    default:
	break; ## get outta here
    endsw
end

# sanity-check the arg -- we need a drive
if ( $# < 1 ) then
    echo "Please specify a drive to FA!"
    echo "usage: $0 [-parallel] <drive> [<drive> ...]"
    echo "  drives can be specified as bay, twed, etc"
    exit
endif
if ( $# > 1 ) then
    echo "Running drive FA in parallel..."
    echo "Unfortunately, to kill them you will need to 'killall $0'."
    foreach disk ( $* )
	$0 $disk &
	if ("X$parallel" == "Xno") then
	    wait
	endif
    end
    wait
    ## Unfortunately I can't get "onintr" to work with that wait...
    ## so you have to kill all kids yourself.
    exit
endif


if ( "X$0" !~ */* ) then
    set rundir = "";
else
    set rundir = "`dirname $0`/"
endif

set drivearg = "$1"

set DRIVE = `isi_drivenum -t -- $drivearg`

if ( "$DRIVE" !~ "/dev"* ) then
    echo "Couldn't convert $drivearg to usable drive"
    exit
endif


# check that syslogd is running -- we need that
set SYSLOG_OK = `ps -aux | grep -q syslogd || echo NO`
if ( "X$SYSLOG_OK" == "XNO" ) then
    echo "Please start syslogd"
    exit
endif


# generic FA setup -- disable special driver handling
sysctl hw.twe0.try_twereset=0
sysctl hw.twe0.try_ecc_recover=0
sysctl hw.twe0.bio_permafail=0
sysctl hw.twe0.pthru_permafail=0

set BAY   = `isi_drivenum -b -- $drivearg`
set SERNO = `isi_radish -q $BAY | sed -e 's~.*SN:\([^,]*\),.*~\1~'`
## now doable with:
# set SERNO = `isi_drivenum -s -- $drivearg`

set FALOG = "fa_$SERNO.log"

set LOGGREP = "fa.$$.$SERNO.start"


# save off any old log
test ! -e $FALOG || \
    ( echo "preserving old log to ${FALOG}~" && \
      (test ! -e ${FALOG}~ || rm -f ${FALOG}~ ) && \
      mv $FALOG ${FALOG}~ )


# Okay, start the FA...
echo "Running FA for bay $BAY ($SERNO) through $DRIVE to $FALOG..."

echo "Starting FA at `date`" >> $FALOG

# First, put a tag in the log file for future hunting
logger "Starting FA: $LOGGREP"

# First, run radish
echo "running radish on bay $BAY..."
echo "" >> $FALOG
echo "radish snapshot at `date`" >> $FALOG
isi_radish -a $BAY >> $FALOG

# Now run a dd-safe (with at most 2000 errors)
echo "running ${rundir}dd_safe on bay $BAY..."
echo "" >> $FALOG
echo "dd_safe starting at `date`" >> $FALOG
${rundir}dd_safe -e 2000 $DRIVE /dev/null >> $FALOG

### We limit the dd_safe at 2000 errors, but we might want to also
### put in something to kill it if it goes for more than T time?

# Grab another radish
echo "running radish again on bay $BAY..."
echo "" >> $FALOG
echo "radish snapshot at `date`" >> $FALOG
isi_radish -a $BAY >> $FALOG

# Now grep through /var/log/messages for anything of interest
echo "extracting /var/log/messages for FA on bay $BAY..."
echo "" >> $FALOG
echo "/var/log/messages twe output during that run:" >> $FALOG
echo " (You probably only care about messages for $DRIVE)" >> $FALOG
grep -A 200 $LOGGREP /var/log/messages | grep twe >> $FALOG


# Done...
echo "Completed FA log on bay $BAY to $FALOG."
