#! /bin/zsh

# 97.02.6 made by hwang
# detect sound format automatically and run the adequate player.

program=$0
usage () {
echo $program sound_data_file
exit 1
}
notfound () {
echo $program: $* is not found or is not permitted to read.
}
vocplay () {
	tmp=/tmp/$$sox.wav
	sox $1 $tmp
	wavplay $tmp
	rm -f $tmp
}

[[ $# -eq 0 ]] && usage;

integer repeat=1;
[[ $0 == "playr" ]] && repeat=100;
getopts r re 
if [ "$re" = "r" ]; then
    shift;repeat=$1;shift
fi

[[ $repeat -le 0 ]] && repeat=100

while [[ $repeat -gt 0 ]]; do
for sd 
do
	dir=/usr/lib/sound
	case "$sd" in
		*.mp3) 	soundprogram=amp;
			dir=$dir/mp3;;
                *.mpg)  soundprogram=(mtvp -ac0 -amv-1 -amb-1 -d0);;
		*.wav) 	soundprogram=wavplay;
			dir=$dir/wav;;
		*.au) 	soundprogram=bplay;
			dir=$dir/wav;;
		*.voc) 	soundprogram=vocplay;
			dir=$dir/voc;;
		*.mid | *.rmi | *.mid.*) 	
			soundprogram=(timidity -Od -id -p 20 -s 44100);
			dir=$dir/midi;;
		*.s3m | *.mod | *.xm | *.mtm | *.669) 
			soundprogram=(mikmod -s -i);
			dir=$dir/mod;;
		*) 	echo can\'t find the correct player for $sd; 
			exit 1;; 
	esac

	if [[ -r "$sd" ]]; then
		$soundprogram "$sd"
	elif [[ -r $dir/$sd ]]; then
		$soundprogram $dir/"$sd"
	else
		notfound $sd
	fi 
done
((repeat--))
done
