#!/bin/tcsh

### [This was original scan-and-notify-on-eas, but was modified to run on SN, which can't
### connect to the Myth database 'cause it doesn't even have MySQL on it.]

### This is a handler called by notice-new-files-dispatcher.

### This is what actually ties everything together---it runs $1
### through the EAS detector, and sends a notification if it finds one.
### A later version might attempt to automatically reschedule as well.

## We roll our own version of descriptive-name.pl with one that pulls from program, and not
## oldrecorded, precisely because we expect it will often be run to report on an -in-progress-
## recording, which oldrecorded can't know about yet.  Unfortunately, at least in mythtv 0.18,
## recorded doesn't have the pre/postroll offsets in its keys (and hence the insertion almost
## always just fails), and we can't look for the showing in program in a straightforward manner,
## because program (of course) won't have the pre/postrolls, either.  So instead, we do something
## truly pukeworthy (really, don't read farther if you don't want to barf on your shoes!), which
## is to keep asking with ever increasing fuzziness around the starttime until we either get a
## response, or we exceed even -our- boundless patience with kluges.  This will work so long as
##  the amount of preroll isn't too horribly large; if it fails, then we don't get a descriptive
##  name, but at least the rest of the alert is logged.  (If we're trying to do real rescheduling,
## this will probably have to be made better, but by then, maybe we'll be using a more-recent myth,
## and maybe that more-recent myth will handle the damned record table better in the presence
## of padding!)  Note carefully that we both add -and- subtract possible preroll amounts, since
## it's possible that a showing will have a negative preroll---this is quite rare, but -does-
## happen if I'm short on tuners and know that some particular channel always starts late (or
## really don't care about the first minute of something).  This will definitely fail if thre
## are back-to-back recordings with the same chanid whose actual lengths are shorter than the
## preroll applied, but hopefully that's really rare, if not unheard-of, right?

## (Note that we don't actually call out to any version of descriptive-name.pl, even one with
## a different able in it, since it does lots of work [such as making all shellmetas safe)
## that we don't need here.)

## Definitions.  We won't need these unless we do an alert, which should be rare, but it takes
## virtually no runtime to define them, so we'll put 'em here where they're easy to find.

# set verbose
# set echo

# +++ Private to this file.
setenv PERMLOG ~/EAS.log

alias eas_notify 'cat'
# ---

## Now actually do some work.

set input = $1
set tmp = /dev/shm
set log = $tmp/`basename $input`.log
set out = $log.out

pcm-tail-follow $input $log
set eas_status = $status

if ($eas_status != 0) then
  set infile = `basename $input`
  ## Make a permanent log entry so we can try to deduce the most likely times/channels for alerts.
  echo "$infile\t\t`date`" >>! $PERMLOG
  sed -e '/^$/d' -e "s/.*/$infile\t\t&/" $log >>! $PERMLOG
  echo "" >>! $PERMLOG
  ## Get rid of our temporary stuff.  Note that this is -inside- the if,
  ## because (a) $log won't exist if we didn't find an EAS, and (b) $out
  ## is created inside this if.
  rm -f $log $out
endif

# End of file.
