#!/bin/sh
# Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
# 
# Permission to use, copy, modify, and distribute this material 
# for any purpose and without fee is hereby granted, provided 
# that the above copyright notice and this permission notice 
# appear in all copies, and that the name of Bellcore not be 
# used in advertising or publicity pertaining to this 
# material without the specific, prior written permission 
# of an authorized representative of Bellcore.  BELLCORE 
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Conversion from C shell to Bourne shell by Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp.
# Permission to use, copy, modify, and distribute this material
# for any purpose and without fee is hereby granted, provided
# that the above copyright notice and this permission notice
# appear in all copies, and that the name of Z-Code Software not
# be used in advertising or publicity pertaining to this
# material without the specific, prior written permission
# of an authorized representative of Z-Code.  Z-CODE SOFTWARE
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Brought into line with metamail 2.7 beta release Csh version
# Added sox support
#    Dave Shield	February 1994

if test -z "${METAMAIL_TMPDIR:-}"
then
        METAMAIL_TMPDIR=/tmp
fi

thishost=`hostname`

AUDIOBINDIR=/u/andrew/phone-sau/bin
AUDIOPHONEHOST=greenbush
AUDIOPHONEHOSTLONG=greenbush.bellcore.com
AUDIOLOGMAIL=greenbush.bellcore.com
ORG=Bellcore
STDINPUT=0

if test -x /usr/bin/remsh
then
			# Probably a System V based system
			#  "rsh" is likely to be "restricted shell"
	RSH=remsh
else
	RSH=rsh
fi
# SOX=/usr/remote/bin9/sox

if test "$1" = "-p"
then
	AUDIOPHONE=$2
	shift
	shift
fi

if test "$1" = "-s"
then
	AUDIOSPEAKERFORCE=1
	shift
fi

playphone=0
if test ! -z "${AUDIOPHONE:-}" -o ! -z "${AUDIOPHONEFORCE:-}"
then
	playphone=1
fi

if test $playphone -eq 0
then
	if test ! -d "$AUDIOBINDIR"
	then
		AUDIOSPEAKERFORCE=1
	fi

	if test "$1" = "-"
	then
		STDINPUT=1
		shift
	fi
	if test $# -eq 0
	then
		STDINPUT=1
	fi

	if test $STDINPUT -eq 0 -a -z "${AUDIOSPEAKERFORCE:-}" -a -z "${AUDIOPHONEFORCE:-}"
	then
		if test ! -z "${MM_NOTTTY:-}"
		then
			if test $MM_NOTTTY -eq 1
			then
				MM_NOTTTY=0; export MM_NOTTTY
				xterm -e showaudio $*
				exit 0
			fi
		fi
		looping=1
		while test $looping -eq 1
		do
		    echo This program can display audio on the speakers of some workstations, 
		    echo "or (at some sites) it can call you on the telephone.  Please choose one:"
		    echo ""
		    echo "1 -- Use the computer's speaker"
		    echo "2 -- Call me on the telephone"
		    echo ""
		    echo -n "Which do you prefer (1 or 2)? [1] "
		    read ans
		    if test "$ans" -eq 2
		    then
			echo "OK, we'll use the telephone..."
			AUDIOPHONEFORCE=1
			echo "In the future, you can avoid this question by setting the environment variable"
			echo "AUDIOPHONEFORCE to 1"
			playphone=1
			looping=0
		    elif test "$ans" -eq 1
		    then
			echo "OK, Attempting to play the audio using your computer's speaker.."
			AUDIOSPEAKERFORCE=1
			echo "In the future, you can avoid this question by setting the environment variable"
			echo "AUDIOSPEAKERFORCE to 1"
			looping=0
		    else
			echo "That is NOT one of your choices."
		    fi
		done
	fi
fi

if test $playphone -eq 0
then
	audiohost=$thishost
	if test ! -z "${DISPLAY:-}"
	then
		audiohost=`echo $DISPLAY | sed -e 's/:.*//'`
		if test "$audiohost" = unix -o "$audiohost" = localhost
		then
			audiohost=$thishost
		fi
		if test -z "$audiohost"
		then
			audiohost=$thishost
		fi
	fi
	if test ! -z "${AUDIOHOST:-}"
	then
		audiohost=$AUDIOHOST
	fi

	if test ! "$audiohost" = "$thishost"
	then
		echo Sending audio to $audiohost...
		if test ! -z "${MMS_AUDIO}"
		then
			if test $STDINPUT -eq 1
			then
				cat | mms -host $audiohost "show audio/basic"
			else
				cat $* | mms -host $audiohost "show audio/basic"
			fi
			exit 0
		fi
		thisprog=`(cd; which showaudio)`
		if test $STDINPUT -eq 1
		then
			cat | $RSH $audiohost $thisprog -s
		else
			cat $* | $RSH $audiohost $thisprog -s
		fi
		exit 0
	fi


	if test -f /usr/sbin/sfplay
	then
		if test $# -ne 0
		then
			exec /usr/sbin/sfplay -i mu-law chan 1 rate 8000 end $*
		else
			cat > ${METAMAIL}/audio.$$
			/usr/sbin/sfplay -i mu-law chan 1 rate 8000 end ${METAMAIL}/audio.$$
			rm ${METAMAIL}/audio.$$
			exit 0
		fi
	elif test -f /usr/bin/audioplay
	then
	# Solaris 2.0
		/usr/bin/audioconvert -i raw,voice -f sun $* | /usr/bin/audioplay
		exit $?
	elif test -f /bin/audioplay
	then
	# Solaris 2.0
		/bin/audioconvert -i raw,voice -f sun $* | /bin/audioplay
		exit $?
	elif test -d /usr/sony
	then
		dev=/dev/sb0
	else
		dev=/dev/audio
	fi

	if test -w $dev
	then
		echo Playing audio on $thishost using $dev, one moment please...
		if test $# -ne 0
		then
			if test ! -z "${SOX:-}"
			then
			    for ii in $*
			    do
#
#  The "approved magic" to translate arbitary sounds
#	to AU format is as follows:
#
#				$SOX -t auto $ii -c 1 -t aiff - | \
#				$SOX -t aiff - -U -b -t au -c 1 -r 8000 $dev
#
#  But the following "direct" translation also seems to work
				$SOX -t auto $ii -c 1 -t au -c 1 -r 8000 $dev
			    done
			else
				cat $* > $dev
			fi
		else
			if test ! -z "${SOX:-}"
			then
				cat > ${METAMAIL}/audio.$$
#
#  See also above
#
				$SOX -t auto ${METAMAIL}/audio.$$ -c 1 -t au -c 1 -r 8000 $dev
			else
				cat > $dev
			fi
		fi
		exit 0
	fi
fi

if test -d "$AUDIOBINDIR"
then
	thisprog=`which showaudio`
	if test -z "${AUDIOPHONE:-}"
	then
		if test ! -z "${MM_NOTTTY:-}"
		then
			if test $MM_NOTTTY -eq 1
			then
				MM_NOTTTY=0; export MM_NOTTTY
				xterm -e $thisprog $*
				exit 0
			fi
		fi
		echo This message contains audio, which can be sent to your telephone.
		echo Please enter the telephone number at which you would like to hear this
		echo -n "audio message as you would dial it from inside ${ORG}: "
		read AUDIOPHONE
	fi

	if test "$thishost" == "$AUDIOPHONEHOST" \
		-o "$thishost" == "$AUDIOPHONEHOSTLONG"
	then
		echo Calling Phone number $AUDIOPHONE
		echo "You MUST SAY HELLO when you answer the phone, or you will not hear the message."
		mail -s "showaudio: `whoami` called $AUDIOPHONE" $AUDIOLOGMAIL < /dev/null
		if test $STDINPUT -eq 1
		then
			cat $AUDIOBINDIR/../GREET.au $* - | $AUDIOBINDIR/play -\# $AUDIOPHONE -
		else
			cat $AUDIOBINDIR/../GREET.au $* | $AUDIOBINDIR/play -\# $AUDIOPHONE -
		fi
		echo All done
		exit 0
	else
		echo Trying to rsh to $AUDIOPHONEHOST to send audio via telephone
		if test $STDINPUT -eq 1
		then
			cat $* - | $RSH $AUDIOPHONEHOST $thisprog -p $AUDIOPHONE -
		else
			cat $* | $RSH $AUDIOPHONEHOST $thisprog -p $AUDIOPHONE -
		fi
		exit 0
	fi
fi
echo ""
echo "This message contains an audio mesage, which can not currently be"
echo "played on this type of workstation.   If you log into an appropriate"
echo "machine (currently a SPARCstation or Sony News workstation)"
echo "and read this message there, you should be able to hear the audio"
echo "message."
echo ""

if test -z "${MM_NOTTY:-}"
then
	MM_NOTTY=0
fi
if test $MM_NOTTY -eq 1
then
	fname=/tmp/mm.aud.$$
else
	echo -n "Do you want to write the audio out to a file [y] ? "
	read ANS
	case "$ANS" in
		[Nn]*)	exit 0 ;;
	esac

	echo -n "File name:"
	read fname
fi
cp $1 $fname
if test $? -eq 0
then
	echo Wrote raw audio file: $fname
fi
exit 0
