#!/bin/sh
#	BSDI showaudio,v 1.4 1996/01/09 07:41:07 sanders Exp
#
# 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.
#

unset IFS

usage() { echo "Usage: showaudio mimetype [file]" 1>&2; }

if [ $# -lt 1 -o $# -gt 2 ]; then usage; exit 1; fi

# Defaults
RECORD_AUDIO=${RECORD_AUDIO:-vrec}
PLAY_AUDIO=${PLAY_AUDIO:-vplay}
EDIT_AUDIO=${EDIT_AUDIO:-mxv}
METAMAIL_TMPDIR=${METAMAIL_TMPDIR:-${TMPDIR:-/tmp}}

AudioType=$1
AudioFile=${2:-/dev/stdin}

if [ "$AudioFile" = "-" ]; then
    AudioFile=/dev/stdin
fi

if [ "$DISPLAY" -a ! "$XPLAYGIZMO" ]; then
    XPLAYGIZMO=true xplaygizmo -p showaudio $AudioType $AudioFile
    exit 0
fi

if [ ! "$AUDIOHOST" ]; then
    if [ "$DISPLAY" ]; then
	AUDIOHOST=`echo $DISPLAY | sed -e 's/:.*//'`
	if [ "$AUDIOHOST" = "unix" ]; then AUDIOHOST=`hostname`; fi
    fi
    export AUDIOHOST
fi

if [ "$AUDIOHOST" -a "$AUDIOHOST" != "localhost" -a "$AUDIOHOST" != `hostname` ]; then
    # send it over
    cat $AudioFile | rsh $AUDIOHOST /usr/contrib/bin/showaudio $AudioType -
else
    # play it locally
    case $AudioType in
	audio/basic) cat $AudioFile > /dev/audio; ;;
	audio/au) cat $AudioFile > /dev/audio; ;;
	audio/x-wav) ${PLAY_AUDIO} -T wav $AudioFile; ;;
	audio/wav) ${PLAY_AUDIO} -T wav $AudioFile; ;;
	*) echo "$0: Invalid mime type: $AudioType" 1>&2; exit 1; ;;
    esac
fi
exit 0

echo ""
echo This message contains an audio mesage which could not be played.
echo ""
if (! $?MM_NOTTTY) set MM_NOTTTY=0
if ($MM_NOTTTY == 1) then
    set fname=/tmp/mm.aud.$$
else
    echo -n "Do you want to write the audio out to a file [y] ? "
    set ANS=$<
    if ("$ANS" =~ n* ||  "$ANS" =~ N* ) exit 0
    echo -n "File name:"
    set fname=$<
endif
cp $1 $fname
if ($status == 0) echo Wrote raw audio file: $fname
exit 0

