#!/bin/sh

#
# bbrun
#
# Sean MacGuire
# Version 1.4g
# May 26th, 2000
#
# This program is Copyright (c) 1997-2001
# BB4 technologies inc
# All Rights Reserved
#
# Execute BB shell scripts
# loop them forever
#

if test ! -x "$1"
then
	echo "Can't execute $1, exiting"
	exit 1
fi

if test "$BBSLEEP" = ""
then
	echo "BBSLEEP not set, exiting"
	exit 2
fi

# Save the pid of this bbrun for runbb.sh stop processing
echo "$$" >> $BBTMP/BBPID

while true
do
	START=`$BBHOME/bin/touchtime -e`
	$1
	END=`$BBHOME/bin/touchtime -e`
	BBTIME=`$EXPR $END - $START`

	if test "$BBTIME" -lt "$BBSLEEP"
	then
		# echo "BBTIME is $BBTIME"
		SLEEPYTIME=`$EXPR $BBSLEEP - $BBTIME`
		# echo "SLEEPYTIME is $SLEEPYTIME"
		sleep $SLEEPYTIME
	else
		echo "bbrun: ($1) Runtime $BBTIME longer than Sleeptime $BBSLEEP"
	fi
done
