#!/bin/bash
def_buffer=230
def_speed=300
#def_buffer=1024
#def_speed=400

help(){
	cat <<END
chkavi--Check an avi file for buffer underruns V1.0.
Usage: chkbuf [options] [<filename.[avi|mov|qt]> ... ]
Options:
        -g       Display graph of buffer fill.
        -b n     Set the the hardware buffer to n KB        ($def_buffer)
        -s n     Set the speed of the drive to n KB/sec     ($def_speed)
        -h       Print this help message.
END
	exit 0
}
gflag=""
buffer=$def_buffer
speed=$def_speed

while getopts gb:s:h name "$@"
do
	case $name in
g)  
	gflag=-g ;;
h|'?'|':')
	help ;;
b)
	buffer=$OPTARG ;;
s)
	speed=$OPTARG ;;
esac
done
let shiftind=$OPTIND-1
shift $shiftind
OPTIND=1
for input in "$@"
do
    case $input in
*.qt)
        base=`echo $input | sed "s/\.qt$//"` ;;
*.avi)
        base=`echo $input | sed "s/\.avi$//"` ;;
*.mov)
        base=`echo $input | sed "s/\.mov$//"` ;;
*)
        help ;;
	esac
#	echo Making index of $input in $base.idx....
	aviindex -i $input -o $base.idx >/dev/null 2>&1
#   echo
#	echo Checking for buffer overflows...
	echo /home/ejolson/video/bin/chkidx $gflag -b $buffer -s $speed $base.idx
	/home/ejolson/video/bin/chkidx $gflag -b $buffer -s $speed $base.idx | 
		tail -1
done
