#! /bin/sh

#
# total disksize 20e9 =~ 19 * 1024 megabytes
# total disksize: 39560c/16h/63s = 39876480 [blocks] = 20416757760 [bytes]
#	= 19938240 [kbytes] = 19470.9375 [Mbytes] = 19.01458740234375 [Gbytes]
#

# disksize in blocks
total=`expr 39560 '*' 16 '*' 63`
max=$total

disk=/dev/rad0
dname=master
case x$1 in
	xad1) disk=/dev/rad1; dname=slave ;;
esac

echo ''
echo "check primary $dname"
echo -n 'Ok? [y/n]  '

read x
case $x in
	y) ;;
	*) echo 'Aborted'; exit 1;;
esac

echo ''
echo '* STAGE 1 of 6'
echo -n reading boot sector...
dd if=$disk bs=512 count=1 of=/tmp/x 2> /dev/null
case $? in
	0) echo ' done' ;;
	*) echo ''; echo 'READ FAILED' ; exit 1;;
esac

echo -n checking boot sector...
check < /tmp/x > /dev/null
case $? in
	0) echo ' OK, erased' ;;
	1) echo ' May be erased, but not ZERO' ;;
	*) echo ' NOT ERASED' ;;
esac


echo ''
echo '* STAGE 2 of 6'
echo -n checking the first track...
n=1
m=63
z=0
e=0
u=0
while [ $n -lt $m ] ; do
	dd if=$disk bs=512 count=1 skip=$n of=/tmp/x 2> /dev/null
	check < /tmp/x > /dev/null
	case $? in
		0) z=`expr $z + 1` ;;
		1) e=`expr $e + 1` ;;
		*) u=`expr $u + 1` ;;
	esac
	n=`expr $n + 1`
done

echo ''
echo "$z zero-erased sectors, $e nonzero-erased sectors, $u unerased sectors"


echo ''
echo '* STAGE 3 of 6'
echo -n checking the 2nd, 3rd, 4th tracks...
n=63
m=190
z=0
e=0
u=0
while [ $n -lt $m ] ; do
	dd if=$disk bs=512 count=1 skip=$n of=/tmp/x 2> /dev/null
	check < /tmp/x > /dev/null
	case $? in
		0) z=`expr $z + 1` ;;
		1) e=`expr $e + 1` ;;
		*) u=`expr $u + 1` ;;
	esac
	n=`expr $n + 1`
done

echo ''
echo "$z zero-erased sectors, $e nonzero-erased sectors, $u unerased sectors"


echo ''
echo '* STAGE 4 of 6'
echo checking some of start sectors of first partition...
n=189
m=65536
step=3
z=0
e=0
u=0
r=0
while [ $n -lt $m ] ; do
	case $r in
		0) echo -n "
cheking $n of $m ..." ; r=97 ;;
	esac
	r=`expr $r - 1`
	dd if=$disk bs=512 count=1 skip=$n of=/tmp/x 2> /dev/null
	check < /tmp/x > /dev/null
	case $? in
		0) z=`expr $z + 1` ;;
		1) e=`expr $e + 1` ;;
		*) u=`expr $u + 1` ;;
	esac
	n=`expr $n + $step`
done

echo ''
echo "$z zero-erased sectors, $e nonzero-erased sectors, $u unerased sectors"


echo ''
echo '* STAGE 5 of 6'
echo fast checking of data sectors...
n=64
step=10249
z=0
e=0
u=0
r=0
while [ $n -lt $max ] ; do
	case $r in
		0) echo -n "
cheking $n of $max ..." ; r=97 ;;
	esac
	r=`expr $r - 1`
	dd if=$disk bs=512 count=1 skip=$n of=/tmp/x 2> /dev/null
	check < /tmp/x > /dev/null
	case $? in
		0) z=`expr $z + 1` ;;
		1) e=`expr $e + 1` ;;
		*) u=`expr $u + 1` ;;
	esac
	n=`expr $n + $step`
done

echo ''
echo "$z zero-erased sectors, $e nonzero-erased sectors, $u unerased sectors"

	
echo ''
echo '* STAGE 6 of 6'
echo final checking of data sectors...
n=201
step=2049
z=0
e=0
u=0
r=0
while [ $n -lt $max ] ; do
	case $r in
		0) echo -n "
cheking $n of $max ..." ; r=99 ;;
	esac
	r=`expr $r - 1`
	dd if=$disk bs=512 count=1 skip=$n of=/tmp/x 2> /dev/null
	check < /tmp/x > /dev/null
	case $? in
		0) z=`expr $z + 1` ;;
		1) e=`expr $e + 1` ;;
		*) u=`expr $u + 1` ;;
	esac
	n=`expr $n + $step`
done

echo ''
echo "$z zero-erased sectors, $e nonzero-erased sectors, $u unerased sectors"
	
echo ''
echo -n 'Press ENTER to continue... '
read x

exit 0





