#! /bin/bash
###############################################################################
#
# $Id$ $Name$
#
# The contents of this file are subject to the AAF SDK Public Source
# License Agreement Version 2.0 (the "License"); You may not use this
# file except in compliance with the License.  The License is available
# in AAFSDKPSL.TXT, or you may obtain a copy of the License from the
# Advanced Media Workflow Association, Inc., or its successor.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
# the License for the specific language governing rights and limitations
# under the License.  Refer to Section 3.3 of the License for proper use
# of this Exhibit.
#
# WARNING:  Please contact the Advanced Media Workflow Association,
# Inc., for more information about any additional licenses to
# intellectual property covering the AAF Standard that may be required
# to create and distribute AAF compliant products.
# (http://www.amwa.tv/policies).
#
# Copyright Notices:
# The Original Code of this file is Copyright 1998-2009, licensor of the
# Advanced Media Workflow Association.  All rights reserved.
#
###############################################################################

usage()
{
	echo `basename $0` exit_status count arguments ...
	echo Repeat a command at most \"count\" times while the exit status
	echo of the command is \"exit_status\". If \"count\" is reached,
	echo the last exit status of the command is returned \(which will
	echo be \"exit_status\"\).  The script sleeps for 1 second between
	echo retries.
	exit 1;
}

if [ $# -eq 0 ]; then
	usage
fi

EXIT_STATUS=$1
shift
if [ $# -eq 0 ]; then
	usage
fi

COUNT=$1
shift
if [ $# -eq 0 ]; then
	usage
fi

I=0

until [ $I -eq $COUNT ]
do
	$*
	STATUS=$?
	if [ $STATUS -ne $EXIT_STATUS ]; then
		exit $EXIT_STATUS
	fi
	sleep 1
	let I++
done

exit $STATUS

