#!/bin/bash

### Transcodes the file named in its first arg to the appropriate format for use by eas-detect,
### then runs it through.  Redirects stderr to /dev/null so if you get anything at all, it's an EAS.
### NOTE!  This means that any mplayer errors will be silently discarded!  The point of doing it this
### way (instead of just waiting for and checking the return value of eas-detect) is that this way we
### can know long before the end of the program if an EAS was present near the beginning.  (Another
### way would be for eas-detect to simply bomb out early, of course...)

### NOTE ALSO that the -clean- way of doing this, namely to just return whatever output eas-detect
### emits (so our caller, pcm-tail-follow, can pass it through itself for later capture and emission
### by scan-and-notify-on-eas to the popup notification and/or logfile) WILL NOT WORK!  Why?  Because
### for -some- godforsaken reason I can't figure out, trying to do, e.g. ,"`pcm-tail-follow ...`" in
### EITHER tcsh or bash will HANG!  What's going on is that, even though eas-detect returns early (and
### hence so do we), the "tail -f --pid=..." in pcm-tail-follow does NOT return!  At least, not promptly:
### it waits until the indicated pid dies, and -then- returns, which means at the very -end- of a recording,
### even if we detected an EAS at the beginning.  This is totally nuts; calling pcm-tail-follow directly
### (not wrapped in "`...`") works just FINE in this circumstance, and I can't figure out how the "tail -f"
### has any idea we're even DOING that!  That fact that it's consistent across both tcsh and bash makes me
### believe that this is not a bug per se, but instead that "`...`" tries to wait for all possible output
### before returning, such as might be emitted by the tail directly (bypassing the pipe to pcm-eas), but
### I can't figure out why that makes sense or how the interaction between the tail (running in a script
### in a different process -entirely- from the one executing the "`...`" line) even happens.
###
### So because of all this lossage, we actually take care to write all of eas-detect's output directly
### to a logfile in $tmp, where our eventual caller can just read it directly.  It'd be great if the
### caller also arranged to delete it when done, but since this is supposed to be a rare event and $tmp
### is a ramdisk, it's not going to present a major clutter problem.  [But since bash creates the file
### whether or not any output goes to it, we take care to automatically delete it again if it's still
### empty when we finish.]
###
### Also, because of this lossage, we need to KNOW the name of the relevant file, so we can construct
### the logfile!  Ordinarily, we'd just be called via a pipeline with "-" as the pathname (since we're
### just receiving the transcoded-to-8K-wav format the eas-detect can digest), but that doesn't help us
### with the logfile.  So the second arg must be the logfile we should be putting our output in.

### Any args after the first two will be passed directly to eas-detect.

input=$1
log=$2

pcm-quiet $input | eas-detect ${*:3} >> $log 2>&1
eas_status=$?

### This is disgusting:  apparently, eas-detect's early exit causes mplayer to just hang, rather than terminating.
### So clean up -all- mplayers that are hanging around that are now owned by init and which seem to be running
### our command line.  Needless to say, this is (a) nonportable and (b) incredibly brittle, since it depends on
### us knowing what our command line is!  Blecch.  Note that we'll only have a hung mplayer if we detected an
### alert, since otherwise we'll have been forced to read all the way to the end of the input stream anyway,
### so maybe this isn't really all that necessary since alerts (outside of testing) are supposed to be rare...
### Note that we're only cleaning up those mplayers which are owned by init---this means the hung ones, rather
### than any mplayers that might belong to other streams which are still doing actual work...

hung=`ps -A -opid,ppid,cmd | egrep "    1 [m]player -really-quiet -noconsolecontrols -nojoystick -nolirc -nomouseinput -vc dummy -vo null -af resample=8000:0:0,channels=1 -ao pcm:nowaveheader:file=/dev/fd/3" | awk '{print $1}'`

if [ "$hung" != "" ]
  then kill $hung
fi

### If the logfile didn't wind up with any output in it, delete it.

[ -s $log ] || rm $log

### Make sure our caller knows whether we found an alert.

exit $eas_status

# End of file.
