#!/usr/bin/perl

# If you want the script to send email when the signal is lost or communciation
# problems set the sendmail variable. If the script is running as root 
# the -f flag set the user to send the mail as
# example:
# my $sendmail = '/usr/sbin/sendmail -t -fuser1';
my $sendmail = '';

# Set to address to send email to. Leave blank to not send any mail
# example: 
# my $email_address = "example user1@example.com,user2@example.com"
my $email_address = '';

# Set to directv status script command for your box
my $directv_cmd = '/var/lib/mythtv/directv.pl port /dev/ttyUSB0 box_type D10-200 get_signal get_channel';

# Set to directory to keep statistics in
my $log_dir = '/var/log';

my $fail_count = 0;
my $sig_count = 0;
my $sigloss_len = 0;
my $tot_good, $tot_sigloss, $tot_fail, $total, $tot_sigevents, $max_sigloss;

if (open(INFO,"$log_dir/directv_stats.txt")) {
   ($tot_good,$tot_sigloss,$tot_fail, $total, $tot_sigevents, $max_sigloss) = split / /,<INFO>;
   close(INFO);
} else {
   $tot_good = 0;
   $tot_sigloss = 0;
   $tot_fail = 0;
   $total = 0;
   $tot_sigevents = 0;
   $max_sigloss = 0;
};
for (;;) {
   # my @ret = split " ", `$directv_cmd 2>> /tmp/derrors`;
   my @ret = split " ", `$directv_cmd 2>> /tmp/derrors`;

   $total++;
   #open (OUT,">>/tmp/derrors");
   #print OUT "RC $? ret $#ret: $ret[0], $ret[1], $ret[2], $ret[3]\n";
   #close(OUT);
   if (($? >> 8) == 2 || (($? >> 8) == 0 && $ret[1] == 0) ) {
      $r = `/usr/bin/rrdtool update $log_dir/directv.rrd "N:0:0"`;
      $tot_sigloss++; 
      $sigloss_len++;
      if ($good_count > 5) {
         $tot_sigevents++;
         $good_count = 0;
         if ($sendmail ne "" && $email_address ne "") {
            open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; 
            print SENDMAIL "Subject: Directv has lost signal\n";
            print SENDMAIL "To: $email_address\n";
            print SENDMAIL "\nRain rain go away, come again another day.\n";
            close(SENDMAIL);
         }
      }
      open(SIG,">>$log_dir/sigloss.txt");
      print SIG "$tot_sigloss $total ", scalar localtime,"\n";
      close(SIG);
   } elsif (($? >> 8) == 0 && $#ret == 3) {
      $r = `/usr/bin/rrdtool update $log_dir/directv.rrd "N:$ret[1]:$ret[3]"`;
      $tot_good++;
      if ($sigloss_len > $max_sigloss) {
         $max_sigloss = $sigloss_len
      }
      $sigloss_len = 0;
      $fail_count = 0;
      $sig_count = 0;
      $good_count++;
   } else {
      $good_count = 0;
      $tot_fail++; 
      if ($fail_count++ == 2) {
         if ($sendmail ne "" && $email_address ne "") {
            open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; 
            print SENDMAIL "Subject: Directv box failed\n";
            print SENDMAIL "To: $email_address\n";
            print SENDMAIL "\nPlease fix me\n";
            close(SENDMAIL);
         }
      }
   }
   open(INFO,">$log_dir/directv_stats.txt");
   print INFO "$tot_good $tot_sigloss $tot_fail $total $tot_sigevents $max_sigloss\n";
   close(INFO);
   ($sec) = gmtime(time);
   if ($sec < 15) {
      sleep 15 - $sec + 30;
   } elsif ($sec >= 45) {
      sleep 60 - $sec + 15 + 60; 
   } else {
      sleep 45 - $sec + 60;
   }
}
