#!/bin/csh
#
# drive_5v:
#  run a single sequential read stream off the entire drive.  This should
#  hit maximum throughput, and draw a lot of power on the 5v rail.

set bays      = `isi_drivenum -Mb`

set mode="help";
if ($# == 1) then
    switch ($1)
    case "start":
    case "-start":
	set mode="start";	breaksw;
    case "stop":
    case "-stop":
	set mode="stop";	breaksw;
    case "status":
    case "-status":
	set mode="status";	breaksw;
    #default -- leave mode at help
    endsw
endif

if ($mode == "help") then
    echo "drive_5v [start|stop|status]"
    echo "start -- start sequential-large-block-read dd's on all drives"
    echo "stop  -- kill all the processes started above."
    echo "status-- print ps output to help show system state."
    exit 1
endif

### If status, check and exit
if ($mode == "status") then
    ps -axwww | grep " dd " | grep -v grep
    exit 0
endif

## whether it's start or stop, kill everything already running
set checkpids = "ps -axwww | grep -e dd -e of=/dev/null | grep -v grep | awk '{ print "'$1'" }'"
set runningpids="`$checkpids`"

if ("X$runningpids" != "X") then
	echo "stopping drive_5v processes $runningpids"
	kill $runningpids
endif

if ($mode == "stop") then
    ### and if it really was stop, we're done.
    exit 0
endif

## otherwise it was start, so start up a reader on all drives
@ BAY = 1
while ( $BAY <= $bays )
	set DEV = "`isi_drivenum -t ${BAY}`"
	if ( -e $DEV ) then
		echo "starting sequential read on $DEV"
		(dd if=$DEV of=/dev/null bs=1m &) >& /dev/null
	endif
	@ BAY = ($BAY + 1)
end
