#!/bin/sh
# $Id: play123,v 1.3 2008/05/22 08:18:37 abs Exp $

# Simple script to confirm audio device clear, then run player with arguments

PLAYER=$1
shift
audio=/dev/audio
for arg ; do
    case "$arg" in
	# ogg123 uses options of the form '-o dev:/dev/audio1'
	*/dev/audio*) audio=$(echo $arg|sed 's:.*/dev/:/dev/:');;
    esac
done

# Small sleep to let the audio device clear
#
pkill -9 mpg123
sleep 0.3

# Make sure the audio device is closed. Grr
#
RETRYS=40
for f in $(jot $RETRYS) ; do
    active=$(audioctl -d $audio -n play.active 2>&1)
    if [ "$active" = 0 ]; then
	break
    fi
    sleep 0.1
    echo -n " $f"
done
if [ $f -gt 1 ] ; then
    echo ""
fi
if [ $f = $RETRYS ] ; then
    echo "Audio device $audio busy"
    exit 2
fi

exec $PLAYER "$@"
