#!/bin/sh
# vsound - a wrapper script that allows you to record the output of OSS programs
#
# Copyright (C) 2004 Nathan Chantrell <nsc@zorg.org>
# Copyright (C) 2003 Richard Taylor <r.taylor@bcs.org.uk>
# Copyright (C) 2000,2001 Erik de Castro Lopo <erikd@zip.com.au>
# Copyright (C) 1999 James Henstridge <james@daa.com.au>
#
#   Based on the esddsp utility that is part of esound.  Esddsp's copyright:
#   Copyright (C) 1998, 1999 Manish Singh <yosh@gimp.org>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

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

if test $# -le 1; then
	if [ "$1" = "-h" ] || [ "$1" = "--help" ] || \
	   [ "$1" = "-V" ] || [ "$1" = "--version" ] ; then
		echo -n
	else
		# No comand 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 "-t, --timing       add timing delays to allow recording of streaming data"
		echo "-a, --autostop=SEC kill the player after 'SEC' seconds of inactivity."
		echo
		exit 0
		;;

	-V|--version)
		echo "vsound-0.5"
		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
		;;
		
	-d|--dspout)
		export VSOUND_DSPOUT=1
		shift
		;;

        -r|--resample)
                shift
                if test $# -gt 0; then
                        resamplerate="$1"
                else
                        echo "no sample rate specified"
                        exit 1
                fi
                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
		;;

	-t|--timing)
		export VSOUND_TIMING=1
		shift
		;;

	-a|--autostop)
		shift
		if test $# -gt 0; then
			export VSOUND_STOPDELAY="$1"
		else
			echo "no autostop delay specified"
			exit 1
			fi
		shift
		;;

	--)
		shift
		break
		;;

	*)
		break
		;;
	esac
done

export VSOUND_DATA=./vsound$$.au

if [ "$verbose" = 1 ] && [ "$VSOUND_STDOUT" ]; then
	echo "Error : cannot specify both --verbose and --stdout."
	exit 1
fi


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

prefix=/usr
exec_prefix=${prefix}
pkglibdir=/usr/lib64/vsound

if [ ! "$VSOUND_STDOUT" ]; then
	echo "About to start the application. The output will not be available"
	echo "until the application exits."
fi

# 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."
	echo "Here are some the possible reasons :"
	echo " - You are trying to record a stream (RTSP or PNM protocol) from "
	echo "   the internet. You will need to use the --timing option."
	echo " - The program you are trying to run is setuid. You will need to "
	echo "   run vsound as root."
	echo " - Vsound was not properly installed and hence won't work at all."
	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 $resamplerate $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
