#!/bin/csh -f

# diagnostics 
set diags=/usr/local/etc/viodiag

# log file and directory information
set host=`/bin/hostname`
set log_dir=/usr/spool/cm
set log=/usr/spool/cm/${host}.vmeio.diag_log

# reset command
set reset="/usr/local/etc/vioconfig -x2"

set my_name=`basename $0`

# make sure we were passed an argument
if ($#argv != 1) then
  echo "You must supply a recipient to send diagnostic output to"
  exit(-1)
endif

# Now find out who we are sending this to
if ($#argv > 0) then
  set recipient=$1
endif

# Check for existance of log directory and ability to write to it
if (! -d $log_dir) then
  echo "Error in ${my_name}: can not find log directory $log_dir"
  exit(-2)
else if (! -x $log_dir) then
       echo "Error in ${my_name}: can not cd to log directory $log_dir"
       echo "                     No execute permissions"
       exit(-3)
endif

# Suppress "No Match" msg in case there are no log files to match the pattern
set nonomatch

# Make sure new log files get created with mode 666
umask 0

# Roll the log files
echo "rolling previous log files"
set backups = 8
while ( $backups > 1)
   @ srcext = $backups - 1
   if (-e $log.$srcext) mv -f $log.$srcext $log.$backups
   @ backups--
end
if (-e $log) mv -f $log $log.1

cat > ${log}  <<EoFa
#
# VMEIO Diagnostic Output from $host
#
EoFa

echo "Done."

# Finally, run diagnostics

echo "Running VMEIO board diagnostics..."
${diags} -f +Ei -Ebl		 >> ${log}

echo "Done."

# Reset the board
$reset

echo "Mailing results to recipient..."
mail $recipient < ${log}
echo "Done."
