#!/bin/sh
# vsound - a wrapper script that allows you to record the output of
# OSS programs

verbose=0
keep_temps=0
resamplerate=0
outfile=./vsound.wav
convert_to_wav=1

if test $# -le 1; then
	if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
		echo -n
	else
		# No coomand line args so recall ourself with -h option.
		$0 -h
		exit 0
		fi
	fi

while test $# -gt 0; do
	case "$1" in
	-h|--help)
		echo "vsound - digitally record output of an OSS audio program"
		echo ""
		echo "vsound [options] program arguments"
		echo ""
		echo "options:"
		echo "-f, --file=FILE    output file name"
		echo "-v, --verbose      set to verbose output"
		echo "-k, --keep-temps   don't delete temporary files"
		echo "-h, --help         this help message"
		echo "-V, --version      show program version"
		echo "-r, --resample     resample the output file to the given sample rate"
		echo "                     eg. vsound -r 44100 realplay file.rm"
		echo "-d, --dspout       enable simulateous output to /dev/dsp and file"
		echo "                     (may be required for some programs)"
		echo "-s, --stdout       write the intermediate (Sun AU format) file to stdout"
		echo "                     (no other output file is generated)"  
		echo "-n, --no-convert   do not convert the AU file to WAV"
		echo
		exit 0
		;;

	-V|--version)
		echo "vsound-0.4"
		exit 0
		;;

	--file=*)
		outfile="`echo $1 | sed -e 's/^[^=]*=//g'`"
		shift
		;;

	-f|--file)
		shift
		if test $# -gt 0; then
			outfile="$1"
		else
			echo "no output file specified"
			exit 1
			fi
		shift
		;;
		
	-r|--resample)
		shift
		if test $# -gt 0; then
			resamplerate="$1"
		else
			echo "no sample rate specified"
			exit 1
			fi
		shift
		;;
		
	-d|--dspout)
		export VSOUND_DSPOUT=1
		shift
		;;

	-s|--stdout)
		export VSOUND_STDOUT=1
		shift
		;;

	-v|--verbose)
		verbose=1
		shift
		;;

	-k|--keep-temps)
		keep_temps=1
		shift
		;;

	-n|--no-convert)
		convert_to_wav=0
		outfile=./vsound.au
		shift
		;;

	--)
		shift
		break
		;;

	*)
		break
		;;
	esac
done

export VSOUND_DATA=./vsound$$.au

if test "$verbose" = 1; then
	echo "Output file:   $outfile"
	echo "Temp AU file:  $VSOUND_DATA"
fi

prefix=/usr/local
exec_prefix=${prefix}
pkglibdir=${exec_prefix}/lib/vsound

# run the program with the vsound library preloaded
LD_PRELOAD="$pkglibdir/libvsound.so" "$@"


if test "$VSOUND_STDOUT" = 1; then
	# Output went to stdout so there's no further processing to be done.
	exit 0
	fi

if [ ! -e "$VSOUND_DATA" ]; then
	echo "Missing file $VSOUND_DATA."
	echo "This means that the libvsound wrapper did not work correctlty. A "
	echo "possible reason is that the program you are trying to run is "
	echo "setuid. In this case you will need to run vsound as root."
	exit
	fi

if test "$resamplerate" != 0; then
	# Use SoX to convert sample rate and file types simulatneously.
	echo -n "Resampling file to $resamplerate Hz : "
	sox $VSOUND_DATA -r 44100 $outfile resample
	echo "Done."
elif test "$convert_to_wav" = 1; then
	# Use SoX to change from AU file to WAV file.
	if test "$verbose" = 1; then
		echo "Converting to WAV file."
		fi
	sox $VSOUND_DATA $outfile
else
	# Rename the file to remove pid portion of name.
	mv $VSOUND_DATA $outfile
	exit 0
	fi	

if test "$keep_temps" = 0; then
	if test "$verbose" = 1; then
		echo "Deleting temporary files."
		fi
	rm -f $VSOUND_DATA
	fi
