#!/bin/sh
#this checks the /usr tree for I/O errors, also 
#copies a big tree of files from the first data disk to the other
#disks and then checks for corruption.
if [ ! -e /etc/burnin_on ] 
then
	exit
fi
if ! [ -x /etc/burnin.conf ] 
then
	exit
else
	. /etc/burnin.conf
fi
if [ "$RUN_ZIOERROR" == "YES" ]
then
#
#  Check if lock file is there
#
    if [ -e /etc/zioerror.lock ]
    then
	exit
    else
	touch /etc/zioerror.lock
    fi
#
#
#  Check for I/O damage in the /usr system
#
    find /usr \( -path '/usr/local' \) -prune -o -type f -exec ls -l {} \; | grep 'Input/output error'
    if [ -e /etc/bonnie.conf ] 
    then
	BONNIEFILE="`cat /etc/bonnie.conf`"
	FIRSTDATA=""
	SECONDDATA=""
	for line in $BONNIEFILE
	do
	    TYPE="$(echo $line | cut -d ':' -f1)"
	    BDIR="$(echo $line | cut -d ':' -f2)"
	    BSIZE="$(echo $line | cut -d ':' -f3)"
	    if [ "$TYPE" = "sys" ] 
	    then
		SYSDIR="$BDIR"
	    fi
	    if [ "$TYPE" = "data" ]
	    then
		if [ "$FIRSTDATA" = "" ]
		then
		    FIRSTDATA="$BDIR"
		else
		    SECONDDATA="$BDIR"
		fi
	    fi
	done
    else
	echo "/etc/bonnie.conf not found" 
    fi

    if [ -d $FIRSTDATA/zioerror ] && [ "$FIRSTDATA" != "" ]
    then 
	SYSLS="`ls $FIRSTDATA/zioerror`"
	if [ "$SYSLS" != "" ] 
	then
	    ls -lR $FIRSTDATA/zioerror | grep 'Input/output error'
	    if [ -e $SYSDIR ]
	    then
		if [ -e $SYSDIR/zioerror ]
		then
		    rm -rf $SYSDIR/zioerror/*
		    cp -pr $FIRSTDATA/zioerror/* $SYSDIR/zioerror
		    ls -lR $SYSDIR/* | grep 'Input/output error'
		else
		    mkdir $SYSDIR/zioerror
		fi
	    fi
	    if [ "$SECONDDATA" != "" ] && [ -e $SECONDDATA ]
	    then
		if [ -e $SECONDDATA/zioerror ]
		then
		    rm -rf $SECONDDATA/zioerror/*
		    cp -pr $FIRSTDATA/zioerror/* $SECONDDATA/zioerror
		    ls -lR $SECONDDATA/zioerror | grep 'Input/output error'
		else
		    mkdir $SECONDDATA/zioerror
		fi
	    fi
	else
	    /root/bin/init.zioerror
	fi
    fi
    rm /etc/zioerror.lock
fi
