#!/bin/bash

# take the path of the storage device and test is it a block device.

function run_bonnie() {
    echo "Running bonnie++ on $1..."

    # Determine where to put the scratchdisk
    mount_point=$(df -h | grep $1 | awk '{print $6}')
    echo "Putting scratch disk at $mount_point"
    mkdir -p "$mount_point/tmp/scratchdir"
    bonnie++ -d "$mount_point/tmp/scratchdir" -u root
}

disk=/dev/$1

if [ -b $disk ]
then
    echo "$disk is a block device"
    size=`parted -l | grep $disk | awk '{print $3}'`

    if [ -n "$size" ]
    then
        echo "$disk reports a size of $size."
        # Have to account for the end of the size descriptor
        size_range=${size:(-2)}

        if [ $size_range == "KB" ]
        then
            echo "$disk is too small to be functioning."
            exit 1
        elif [ $size_range == "MB" ]
        then
            size_int=${size::${#size}-2}

            if [ $size_int -gt 10 ]
            then
                run_bonnie $disk
            else
                echo "$disk is too small to be functioning."
                exit 1
            fi
        else
            run_bonnie $disk
        fi
    else
       echo "$disk doesn't report a size."
       exit 1
    fi
else
    echo "$disk is not listed as a block device."
    exit 1
fi
