#!/bin/sh

# provide a pattern (escaped from the shell as appropriate), and this will
# run a command over each of the files found by the pattern. Expands as
# playing, so that as new files appear (are the music files beng ripped?)
# they will be detected and run.

if [ $# != 2 ]; then
	echo "Usage: $0 <command> <pattern>"
	exit 1
fi

command="$1"
pattern="$2"

while : 
do
	foundlocal=1
	for i in `find . -maxdepth 1 -name "$pattern"`
	do
		foundlocal=0
		for j in $run
		do
			if [ "$j" = "$i" ]
			then
				foundlocal=1
				break
			fi
		done
#echo $foundlocal
		if [ $foundlocal = 1 ]
		then
			# the last time we searched for new files, we now
			# find nothing new
			break
		fi
		run="$run $i"
#echo $command $i $run
		$command $i
	done
	if [ $foundlocal = 1 ]
	then
		# could not find any new files matching the pattern, so
		# finish
		break
	fi
done
