#!/bin/csh
#
# given any command, run it on all drives.
# in your command
#  %bay% is replaced with the bay#
#  %dev% is replaced with the full disk name
#
# examples:
#  serialized disklabel reads:
#    driveall "disklabel %dev%s1c"
#  parallelized per-bay radish (useless!):
#    driveall "isi_radish -q %bay% &"

set noisy = 1

if ( "X$1" == "X-q" ) then
	set noisy = 0
	shift
endif

if ( "X$1" == "X-h" || $# <= 0 ) then
	echo "usage: $0 <cmd>"
	echo "  cmd can include %bay% or %dev% for substitution"
	exit
endif

set maxbay = `isi_drivenum -Mb`
set cmd = "$*"

@ BAY = 1
while ( $BAY <= $maxbay )
	set DEV = "`isi_drivenum -d ${BAY}`"
	set SHORTDEV = "`isi_drivenum -D ${BAY}`"
	if ( -e $DEV ) then
		set runcmd = `echo "$cmd" | sed -e "s~%bay%~${BAY}~g" -e "s~%dev%~${DEV}~g"`
		if ( $noisy == 1 ) then
			echo "## Bay ${BAY}/${SHORTDEV} ## $runcmd"
		endif
		eval $runcmd
	endif
	@ BAY = ($BAY + 1)
end
