#!/bin/csh
#
# repeatn: periodically repeat a command
#

set partarg = no
set partcmd = no
set period = 10
set limit = 10
set print = regular
set command = ""
set endargs = "false"
set exit_on_error = "false"

foreach linearg ($*)
# catch period or limit number after flag
	if ("$partarg" == "period") then
		set period = $linearg
		set partarg = no
	else if ("$partarg" == "limit") then
		set limit = $linearg
		set partarg = no
# catch "end of args" flag
	else if (("$linearg" == "-") && ("$endargs" == "false")) then
		set endargs = true
# catch regular flags
	else if (("$linearg" =~ "-l"*) && ("$endargs" == "false")) then
		set print = loud
		set partcmd = no
	else if (("$linearg" =~ "-r"*) && ("$endargs" == "false")) then
		set print = regular
		set partcmd = no
	else if (("$linearg" =~ "-q"*) && ("$endargs" == "false")) then
		set print = quiet
		set partcmd = no
	else if (("$linearg" == "-t") && ("$endargs" == "false")) then
		set partarg = period
		set partcmd = no
	else if (("$linearg" == "-n") && ("$endargs" == "false")) then
		set partarg = limit
		set partcmd = no
	else if (("$linearg" =~ "-x"*) && ("$endargs" == "false")) then
		set exit_on_error = true
		set partcmd = no
# catch illegal flag
	else if (("$linearg" =~ "-h"*) || (("$command" != "") && ("$partcmd" == "no"))) then
echo "repeatn [-loud/-regular/-quiet] [-t secs] [-n limit] [-] command"
echo "        -loud      display status and i/o"
echo "        -regular   display i/o but no status"
echo "        -quiet     don't display any status or i/o"
echo "        -t secs    number of seconds between executions"
echo "        -n limit   maximum number of executions"
echo "        -x         exit on error"
echo "        (default is -regular -t 10 -n 10)"
		exit
	else
# catch command arg
		set command = "$command $linearg"
		set partcmd = yes
	endif
end

# catch missing or illegal args
if (("$command" == "") || ("$period" == "-1") || ($limit < 0)) then
echo "repeatn [-loud/-regular/-quiet] [-t secs] [-n limit] [-] command"
echo "        -loud      display status and i/o"
echo "        -regular   display i/o but no status"
echo "        -quiet     don't display any status or i/o"
echo "        -t secs    number of seconds between executions"
echo "        -n limit   maximum number of executions"
echo "        -x         exit on error"
echo "        (default is -regular -t 10 -n 10)"
	exit
endif
# character periods will get through (must allow t = 0) but just to be safe:
##if ($period <= 0) set period = 0

if ("$print" == "loud") then
	echo "repeatn: repeating $command  every $period seconds ($limit times)"
endif
@ lim = $limit
@ loopn = 1
# want more control than standard "repeat command"
while (1)
	if ("$print" == "quiet") then
		eval "($command)" > /dev/null
		set retval = "$?"
	else if ("$print" == "loud") then
		echo -n "repeatn $loopn output:"
		eval "($command)"
		set retval = "$?"
	else
		eval "($command)"
		set retval = "$?"
	endif

	if (("$exit_on_error" == "true") && ("$retval" != 0)) then
		echo "repeatn: Exiting due to command error return with -x"
		exit
	endif

	if ($lim > 0) then
	    @ lim = $lim - 1
	    if ($lim <= 0) break
	endif
	@ loopn = $loopn + 1
	sleep $period
end
if ("$print" == "loud") then
	echo "repeatn terminating"
endif

exit
#
# $Author: jwalton
# $Written: 10/15/92

