#!/bin/ksh
#
#ident	"@(#)largedisk.sh	1.3	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# by Terry Hu

pdir=../../../../prgs

if [ $# -ne 2 ]
then
	echo "!ERROR: Usage: $0 <device> <size>"
	exit 1
fi
device=$1
size=$2

HANDLE_FOR_SIG_16()
{
    PID_TO_KILL=`pgrep largedisk.sh`

    echo "!PASS: No errors found in Large Disk Test"

    for pid in $PID_TO_KILL
    do
         kidpid=`$pdir/child ${pid}`
         # kill test parent
         kill -9 ${pid} > /dev/null 2>&1
         # kill test children
         kill -9 $kidpid > /dev/null 2>&1
    done

    while [ -d /proc/$sleepid ]
    do
        sleep 5
    done
    exit 0
}

trap 'HANDLE_FOR_SIG_16' 16

device=/dev/rdsk/$device
machinesize=`fdisk -G $device | sed -n "3p" | awk '
BEGIN {
}
NR == 1 { ncyl = $2
          nhead = $5
          nsect = $6
          secsiz = $7
          next
         }
END {
printf "%s", ncyl*nhead*nsect*secsiz
}'`
#if [ `expr match $size "K$"` -eq 1 ]
echo $size | grep "B$"
if [ $? -eq 0 ]
then
    inputsize=`echo $size | sed -e "s/B//g"`
    #((inputsize=$inputsize*1000))
fi
echo $size | grep "K$"
if [ $? -eq 0 ]
then
    inputsize=`echo $size | sed -e "s/K//g"`
    ((inputsize=$inputsize*1000))
fi
#if [ `expr match $size "M$"` -eq 1 ]
echo $size | grep "M$"
if [ $? -eq 0 ]
then
    inputsize=`echo $size | sed -e "s/M//g"`
    ((inputsize=$inputsize*1000*1000))
fi
#if [ `expr match $size "G$"` -eq 1 ]
echo $size | grep "G$" >/dev/null
if [ $? -eq 0 ]
then
    inputsize=`echo $size | sed -e "s/G//g"`
    ((inputsize=$inputsize*1000*1000*1000))
fi

minsize=`echo $machinesize*0.95 | bc`
maxsize=`echo $machinesize*1.05 | bc`
bcline=`echo "define c(a,b,c){;\
if(a>b){;\
    if(a<c){;\
       return(1);\
    };\
};\
return(2);\
};\
c($inputsize, $minsize, $maxsize)" | bc`
if [ $bcline -eq 2 ]; then
    echo "Input disk size is $size."
    echo "Disk size recognized is $machinesize B."
    echo "Disk size is wrong."
    exit 1
fi

echo "Disk size inputted is $size"
echo "Disk size recognized is $machinesize B"
echo "!PASS: finale No errors found in large disk"
return 0
