#!/bin/csh -f
# 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.
#

# First, figure out which machine to play it on!
echo Attempting to play audio, one moment please...
set playcmd="cat > /dev/audio"
set thishost=`hostname`
set audiohost=$thishost
if ($?DISPLAY) then
    set audiohost=`echo $DISPLAY | sed -e 's/:.*//'`
    if ($audiohost == "unix") set audiohost=$thishost
    if ($audiohost == "") set audiohost=$thishost
endif

if ($audiohost != $thishost) then
    echo Checking audio capability of $audiohost...
    rsh $audiohost csh  > /tmp/audiotmp.$$ <<!
if (-w /dev/audio) echo yes
!
    set hasaudio=`cat /tmp/audiotmp.$$`
    rm /tmp/audiotmp.$$
else if (-w /dev/audio) then
# This next stupid test is for sun4 servers with non-functional /dev/audio devices, sigh...
    cat /dev/null > /dev/audio
    if ($status) then
        set hasaudio="no"
    else
        set hasaudio="yes"
    endif
else
    set  hasaudio="no"
endif
if ("$hasaudio" != "yes") then
    echo ""
    echo This message contains an audio mesage, which can currently only
    echo be played on a SPARCstation.    $audiohost does not seem to be
    echo a SPARCstation.  If you log into a SPARCstation and
    echo read this message there, you should be able to hear the audio
    echo message.  
    if ($1 == "file") then
        echo The text portion of the message, if any, follows below.
        echo " ---------------- "
        tail +2 $2
    endif
    exit 0
endif

if ($1 == "uu") then
    onintr cleanup
    (echo begin 0666 /tmp/showaudio.$$ ; tail +2 $2) | uudecode
    if ($audiohost == $thishost) then
        cat   /tmp/showaudio.$$ >/dev/audio
    else
        echo Sending audio to $audiohost...
        rsh $audiohost cat \> /dev/audio < /tmp/showaudio.$$
    endif
cleanup:
    rm  /tmp/showaudio.$$
else if ($1 == "file") then
    echo ""
    tail +2 $2
    if ($audiohost == $thishost) then
        head -1 $2 > /dev/audio
    else
        echo Sending audio to $audiohost...
        head -1 $2 | rsh $audiohost cat \> /dev/audio
    endif
else if ($#argv == 2) then
    if ($audiohost == $thishost) then
        cat $2 > /dev/audio
    else
        echo Sending audio to $audiohost...
        rsh $audiohost cat \> /dev/audio < $2
    endif
else if ($#argv == 1) then
    if ($audiohost == $thishost) then
        cat $1 > /dev/audio
    else
        echo Sending audio to $audiohost...
        rsh $audiohost cat \> /dev/audio < $1
    endif
else
    if ($audiohost == $thishost) then
        cat > /dev/audio
    else
        echo Sending audio to $audiohost...
        rsh $audiohost cat \> /dev/audio
    endif
endif
