From 36a30c688eea7a9a14823de1ae0ec3d5128d8a72 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Thu, 15 Sep 2016 16:00:45 +0200
Subject: rhrd-schedules show support multi-shows now


diff --git a/utils/rhrd-schedules b/utils/rhrd-schedules
index aab258f..9147e64 100755
--- a/utils/rhrd-schedules
+++ b/utils/rhrd-schedules
@@ -22,6 +22,7 @@
 
 use strict;
 
+use lib '../lib';
 use RHRD::rddb;
 use RHRD::utils;
 use DateTime;
@@ -45,6 +46,14 @@ sub generate
     return 1;
   }
 
+  my ($showid_min, $showid_max, $errorstring) = RHRD::rddb::get_showid_range($ctx);
+  unless(defined $showid_min) {
+    print STDERR "$showid_max: $errorstring\n";
+    return 1;
+  }
+
+  ## TODO: get multi-show id range as well
+
   my $errcnt = 0;
   my @shows = ();
   for my $entry (@{$data}) {
@@ -63,19 +72,17 @@ sub generate
       $errcnt++;
       next;
     }
-    my ($showid_min, $showid_max, $errorstring) = RHRD::rddb::get_showid_range($ctx);
-    unless(defined $showid_min) {
-      print "$showid_max: $errorstring\n";
-      return 1;
-    }
+    ## include multi-show ranges for this check
     if ($showid < $showid_min || $showid > $showid_max) {
       print "WARNING: skipping entry whith out of range automation id ($showid) -> $start: $title ($pvid)\n";
       next;
     }
 
+    ## if is-multi-show: get real show-id from multi-show...
+
     (my $exists, my $status, $errorstring) = RHRD::rddb::check_show_exists($ctx, $showid);
     unless(defined $exists) {
-      print "$status: $errorstring\n";
+      print STDERR "$status: $errorstring\n";
       return 1;
     }
     if($exists != 1) {
@@ -92,9 +99,9 @@ sub generate
     $show->{'START_TIME'} = ($start->hour * 3600) + ($start->minute * 60) + $start->second;
     push @shows, $show;
   }
-  my ($result, $status, $errorstring) = RHRD::rddb::create_or_update_schedule_log($ctx, $year, $month, $day, @shows);
+  (my $result, my $status, $errorstring) = RHRD::rddb::create_or_update_schedule_log($ctx, $year, $month, $day, @shows);
   unless(defined $result) {
-    print "$status: $errorstring\n";
+    print STDERR "$status: $errorstring\n";
     return 1;
   }
   return $errcnt;
@@ -116,6 +123,42 @@ sub get
   return 0;
 }
 
+sub show__day_entry
+{
+  my ($ctx, $week, $dow, $duration, $start_short, $pvid, $showid, $title) = @_;
+
+  my ($show, $status, $errorstring) = RHRD::rddb::get_show_info($ctx, $showid);
+  if(!defined $show) {
+    print "$status: $errorstring\n";
+    return 1;
+  }
+
+  ${$show}{'DOW'} = 7 if ${$show}{'DOW'} == 0;
+  my @weeks = split('', ${$show}{'RHYTHM'});
+  if ($title ne ${$show}{'TITLE'} && ($title . " (Wiederholung)") ne ${$show}{'TITLE'}) {
+    print "WARNING: title mismatch (PV: '$title' != RD: '" . ${$show}{'TITLE'} . "') -- PV-id: $pvid\n";
+    return 1;
+  }
+  if ($dow != ${$show}{'DOW'}) {
+    print "WARNING: wrong day of week (PV: " . Date::Calc::Day_of_Week_to_Text($dow) . " != RD: " . Date::Calc::Day_of_Week_to_Text(${$show}{'DOW'}) . ") -- PV-id: $pvid\n";
+    return 1;
+  }
+  if ($weeks[$week-1] != '1') {
+    print "WARNING: this is week $week but show rhythm is: " . ${$show}{'RHYTHM'} . " -> show shouldn't air in this week. -- PV-id: $pvid\n";
+    return 1
+  }
+  if ($duration->{'minutes'} != ${$show}{'LEN'}) {
+    print "WARNING: wrong show length (PV: " . $duration->minutes . " != RD: " . ${$show}{'LEN'} . ") -- PV-id: $pvid\n";
+    return 1
+  }
+  if ($start_short ne ${$show}{'STARTTIME'}) {
+    print "WARNING: wrong show start-time (PV: " . $start_short . " != RD: " . ${$show}{'STARTTIME'} . ") -- PV-id: $pvid\n";
+    return 1
+  }
+  print "OK: $title\n";
+  return 0;
+}
+
 sub show__day
 {
   my ($ctx, $year, $month, $day) = @_;
@@ -131,6 +174,17 @@ sub show__day
   my $dow = Date::Calc::Day_of_Week($year, $month, $day);
   my $week = RHRD::utils::get_rd_week(DateTime->new(year => $year, month => $month, day => $day, hour => 12));
 
+  my ($showid_min, $showid_max, $errorstring) = RHRD::rddb::get_showid_range($ctx);
+  unless(defined $showid_min) {
+    print STDERR $showid_max . ": " . $errorstring . "\n";
+    return undef;
+  }
+  (my $mshowid_min, my $mshowid_max, $errorstring) = RHRD::rddb::get_multi_showid_range($ctx);
+  unless(defined $mshowid_min) {
+    print STDERR $mshowid_max . ": " . $errorstring . "\n";
+    return 1;
+  }
+
   my $errcnt = 0;
   for my $entry (@{$data}) {
     my $start = DateTime::Format::Strptime::strptime("%Y-%m-%d_%H:%M:%S", ${$entry}{'start'});
@@ -146,36 +200,37 @@ sub show__day
     # start times matter here.
     next if ($start->year != $year || $start->month != $month || $start->day != $day);
 
-    print " " . DateTime::Format::Strptime::strftime("%H:%M:%S", $start) . ": ($showid) -> ";
+    print " " . DateTime::Format::Strptime::strftime("%H:%M:%S", $start) . ": ";
     if($showid > 0) {
-      my ($show, $status, $errorstring) = RHRD::rddb::get_show_info($ctx, $showid);
-      if(!defined $show) {
-        print "$status: $errorstring\n";
-        $errcnt++;
-      } else {
-        ${$show}{'DOW'} = 7 if ${$show}{'DOW'} == 0;
-        my @weeks = split('', ${$show}{'RHYTHM'});
-        if ($title ne ${$show}{'TITLE'} && ($title . " (Wiederholung)") ne ${$show}{'TITLE'}) {
-          print "WARNING: title mismatch (PV: '$title' != RD: '" . ${$show}{'TITLE'} . "') -- PV-id: $pvid\n";
-          $errcnt++;
-        } elsif ($dow != ${$show}{'DOW'}) {
-          print "WARNING: wrong day of week (PV: " . Date::Calc::Day_of_Week_to_Text($dow) . " != RD: " . Date::Calc::Day_of_Week_to_Text(${$show}{'DOW'}) . ") -- PV-id: $pvid\n";
-          $errcnt++;
-        } elsif ($weeks[$week-1] != '1') {
-          print "WARNING: this is week $week but show rhythm is: " . ${$show}{'RHYTHM'} . " -> show shouldn't air in this week. -- PV-id: $pvid\n";
-          $errcnt++;
-        } elsif ($duration->{'minutes'} != ${$show}{'LEN'}) {
-          print "WARNING: wrong show length (PV: " . $duration->minutes . " != RD: " . ${$show}{'LEN'} . ") -- PV-id: $pvid\n";
-          $errcnt++;
-        } elsif ($start_short ne ${$show}{'STARTTIME'}) {
-          print "WARNING: wrong show start-time (PV: " . $start_short . " != RD: " . ${$show}{'STARTTIME'} . ") -- PV-id: $pvid\n";
+      if ($showid >= $showid_min && $showid <= $showid_max) {
+        print "($showid)      -> ";
+        $errcnt += show__day_entry($ctx, $week, $dow, $duration, $start_short, $pvid, $showid, $title);
+      } elsif ($showid >= $mshowid_min && $showid <= $showid_max) {
+        my ($show, $status, $errorstring) = RHRD::rddb::get_multi_show_info($ctx, $showid);
+        unless (defined $show) {
+          print "($showid)      -> $status: $errorstring\n";
           $errcnt++;
         } else {
-          print "OK: $title\n";
+          if ($title ne ${$show}{'TITLE'} && ($title . " (Wiederholung)") ne ${$show}{'TITLE'}) {
+            print "($showid)       -> WARNING: title mismatch (PV: '$title' != RD: '" . ${$show}{'TITLE'} . "') -- PV-id: $pvid\n";
+            $errcnt++;
+          } else {
+            my $realshowid = $show->{'SHOWS'}->{$week};
+            print "($showid:$realshowid) -> ";
+            unless($realshowid > 0) {
+              print "WARNING: multi-show $showid has no entry for Week $week -- PV-id: $pvid\n";
+              $errcnt++;
+            } else {
+              $errcnt += show__day_entry($ctx, $week, $dow, $duration, $start_short, $pvid, $realshowid, $title);
+            }
+          }
         }
+      } else {
+        print "($showid)      -> ERROR: show '$pvid|$title' has invalid show-id: $showid\n";
+        $errcnt++;
       }
     } else {
-      print "ERROR: show '$pvid|$title' not configured\n";
+      print "($showid)      -> ERROR: show '$pvid|$title' not configured\n";
       $errcnt++;
     }
   }
@@ -291,6 +346,7 @@ sub orphans
     return -1;
   }
 
+  # TODO: add multi-show-ids to the hash and omit showids who are part of a multi-show
   my %shows;
   for my $showid (@showids) {
     $shows{$showid} = 0;
@@ -324,6 +380,7 @@ sub orphans
   for my $showid (sort keys %shows) {
     if($shows{$showid} == 0) {
 
+      ## TODO: if $showid is inside multi-range -> get info from there
       my ($show, $status, $errorstring) = RHRD::rddb::get_show_info($ctx, $showid);
       if(!defined $show) {
         print "$status: $errorstring\n";
-- 
cgit v0.10.2