#!/bin/bash -e

CONF_FILES=(/usr/lib/ved/ved.conf /etc/ved.conf "$HOME"/.ved.conf "$VED_CONF")

# source config files
HAVE_CONF=0
for F in "${CONF_FILES[@]}"; do
	[ -e "$F" ] && {
		. "$F"
		HAVE_CONF=1
	}
done
if [ "$HAVE_CONF" = 0 ]; then
	echo >&2 $0: cannot find a ved config file
	exit 1
fi

if [ "$1" = "-stretch" ]; then
	playback_stretch="$2"
	shift 2
fi

[ "$#" -eq 1 -o "$#" -eq 2 ] || {
	echo syntax: ved-play [-stretch n] in-dir [out.wav] >&2
	echo "out.wav" can be "-" for stdout
	exit 1
}
IN=$1; OUT=$2

stretch=
if [ -n "$playback_stretch" -a "$playback_stretch" != 1 -a "$playback_stretch" != 1.0 ]; then
        stretch="stretch $playback_stretch"
fi

find "$IN" -type l | sort | xargs cat |
if [ -z "$OUT" ]; then
	sox -t raw -r $sample_rate -s -w - -t ossdsp /dev/dsp $stretch
else
	sox -t raw -r $sample_rate -s -w - -t wav "$OUT" $stretch
fi
