From efb5d6557591ef21c27aa46aae220d862c36feeb Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Tue, 29 Sep 2015 18:09:47 +0200
Subject: implemented show show and list


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 09b307b..3957459 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -626,44 +626,15 @@ sub list_groups
   return @groups;
 }
 
-###########################  SHOW handling  ###########################
-
-sub get_showtitle_and_log
-{
-  my ($dbh, $showid) = @_;
-
-  my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr);
-
-  $sth->execute($showid)
-    or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr);
-
-  my ($title, $macros) = $sth->fetchrow_array;
-  $sth->finish();
-
-  unless(defined $title) {
-    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " not found!")
-  }
-  unless(defined $macros) {
-    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has no macro!");
-  }
-
-  unless($macros =~ /^LL 1 ([^ ]+) 0\!$/) {
-    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has invalid macro: '" . $macros . "'");
-  }
-  my $log = $1;
-
-  return ($title, $log, 'OK', 'success');
-}
+###########################  Dropboxes handling  ######################
 
 sub get_dropboxes
 {
   my ($dbh, $username, $groupname, $type) = @_;
 
-  my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME=? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=?;};
+  my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME = ? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME = ?;};
   if(defined $groupname) {
-    $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME=? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=? and GROUPS.NAME=?;};
+    $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME = ? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=? and GROUPS.NAME = ?;};
   }
 
   my $sth = $dbh->prepare($sql)
@@ -694,7 +665,7 @@ sub get_dropboxes
       $entry->{'TYPE'} = 'show';
       $entry->{'SHOWID'} = $to_cart;
 
-      my ($title, $log, $status, $errorstring) = get_showtitle_and_log($dbh, $to_cart);
+      my ($title, $log, $status, $errorstring) = get_show_title_and_log($dbh, $to_cart);
       unless (defined $title && defined $log) {
         return (undef, $status, $errorstring);
       }
@@ -722,27 +693,137 @@ sub get_dropboxes
   return @allowed_dbs;
 }
 
+###########################  SHOW handling  ###########################
+
+sub get_shows_cart_range
+{
+  my ($dbh) = @_;
+  return get_cart_range($dbh, RHRD_ALLSHOWS_GROUP)
+}
+
+sub get_shows_next_free_slot
+{
+  my ($dbh) = @_;
+  return get_next_free_slot($dbh, RHRD_ALLSHOWS_GROUP)
+}
+
+sub list_shows
+{
+  my ($dbh) = @_;
+
+  my $sql = qq{select TO_CART,SET_USER_DEFINED from DROPBOXES where STATION_NAME=?;};
+
+  my $sth = $dbh->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+  $sth->execute(DROPBOX_PSEUDO_STATION_NAME)
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my @show_dbs;
+  while(my ($to_cart, $params, $lowcart, $highcart) = $sth->fetchrow_array()) {
+    my @p = split(';', $params);
+    next if ('S' ne $p[0]);
+
+    my $entry = {};
+    $entry->{'ID'} = $to_cart;
+    my ($title, $log, $status, $errorstring) = get_show_title_and_log($dbh, $to_cart);
+    unless (defined $title && defined $log) {
+      return (undef, $status, $errorstring);
+    }
+    $entry->{'TITLE'} = $title;
+    $entry->{'LOG'} = $log;
+    $entry->{'RHYTHM'} = $p[1];
+    $entry->{'DOW'} = int $p[2];
+    $entry->{'DOW'} = 0 unless $entry->{'DOW'} < 7;
+    substr($p[3], 2, 0) = ':';
+    $entry->{'STARTTIME'} = $p[3];
+    $entry->{'LEN'} = int $p[4];
+
+    push @show_dbs, $entry;
+  }
+  $sth->finish();
+
+  return @show_dbs;
+}
+
+sub get_show_group_carts
+{
+  my ($dbh, $showid) = @_;
+
+  my $sql = qq{select GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART from DROPBOXES, GROUPS where DROPBOXES.TO_CART = ? and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME = ?;};
+  my $sth = $dbh->prepare($sql)
+    or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+  $sth->execute($showid, DROPBOX_PSEUDO_STATION_NAME)
+    or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my ($group_low_cart, $group_high_cart) = $sth->fetchrow_array();
+  unless(defined($group_low_cart) && defined($group_high_cart)) {
+    return (undef, undef, 'ERROR', "Show not found");
+  }
+  return ($group_low_cart, $group_high_cart, 'OK', 'success');
+}
+
+sub get_show_title_and_log
+{
+  my ($dbh, $showid) = @_;
+
+  my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;};
+  my $sth = $dbh->prepare($sql)
+    or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+  $sth->execute($showid)
+    or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my ($title, $macros) = $sth->fetchrow_array;
+  $sth->finish();
+
+  unless(defined $title) {
+    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " not found!")
+  }
+  unless(defined $macros) {
+    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has no macro!");
+  }
+
+  unless($macros =~ /^LL 1 ([^ ]+) 0\!$/) {
+    return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has invalid macro: '" . $macros . "'");
+  }
+  my $log = $1;
+
+  return ($title, $log, 'OK', 'success');
+}
+
 sub get_show_carts
 {
-  my ($dbh, $logname, $group_low_cart, $group_high_cart) = @_;
+  my ($dbh, $showid) = @_;
+
+  my ($group_low_cart, $group_high_cart, $status, $errorstring) = get_show_group_carts($dbh, $showid);
+  unless (defined $group_low_cart && defined $group_high_cart) {
+    return (undef, $status, $errorstring);
+  }
+
+  (undef, my $log, $status, $errorstring) = get_show_title_and_log($dbh, $showid);
+  unless (defined $log) {
+    return (undef, $status, $errorstring);
+  }
 
   my $sql = qq{select LOG_EXISTS from LOGS where NAME = ?;};
   my $sth = $dbh->prepare($sql)
     or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
 
-  $sth->execute($logname)
+  $sth->execute($log)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
 
-  my $log_exists = $sth->fetchrow_array;
+  my $log_exists = $sth->fetchrow_array();
   $sth->finish();
 
   if(!defined $log_exists || $log_exists ne 'Y') {
-    return (undef, 'ERROR', "Log with name '$logname' does not exist")
+    return (undef, 'ERROR', "Log with name '$log' does not exist")
   }
 
-  $logname=~s/ /_/g;
-  $logname = $dbh->quote_identifier($logname . '_LOG');
-  $sql = qq{select COUNT,CART_NUMBER from $logname order by COUNT;};
+  $log=~s/ /_/g;
+  $log = $dbh->quote_identifier($log . '_LOG');
+  $sql = qq{select COUNT,CART_NUMBER from $log order by COUNT;};
 
   $sth = $dbh->prepare($sql)
     or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
@@ -761,18 +842,6 @@ sub get_show_carts
   return @carts;
 }
 
-sub get_shows_cart_range
-{
-  my ($dbh) = @_;
-  return get_cart_range($dbh, RHRD_ALLSHOWS_GROUP)
-}
-
-sub get_shows_next_free_slot
-{
-  my ($dbh) = @_;
-  return get_next_free_slot($dbh, RHRD_ALLSHOWS_GROUP)
-}
-
 ###########################  MUSICPOOL handling  ###########################
 
 sub get_musicpools_cart_range
diff --git a/utils/rd-show b/utils/rd-show
index 4fe9def..d2e69c4 100755
--- a/utils/rd-show
+++ b/utils/rd-show
@@ -21,38 +21,121 @@
 #
 
 use strict;
+use lib "../lib/";
 use RHRD::rddb;
 
 # this is ridiculous but makes it a little harder to create/remove users...
-if ($> != 0 ) {
-  print STDERR "this must be run as root!\n";
-  exit 1;
+# if ($> != 0 ) {
+#   print STDERR "this must be run as root!\n";
+#   exit 1;
+# }
+
+sub print_usage
+{
+  print STDERR "Usage: rd-show list\n" .
+               "       rd-show (show|remove) <show-id>\n" .
+               "       rd-show add <shortname> <title> <num-carts> <rhythm> <dow> <starttime> <len>\n";
 }
 
-my $num_args = $#ARGV + 1;
-if ($num_args != 2 && $num_args !=3) {
-  print STDERR "Usage: rd-show (add|remove) ??\n";
-  exit 1;
+sub list_shows
+{
+  my ($dbh) = @_;
+
+  my @shows = RHRD::rddb::list_shows($dbh);
+  if(!defined $shows[0] && defined $shows[1]) {
+    print STDERR "$shows[1]: $shows[2]";
+    return 1;
+  }
+  for my $href (@shows) {
+    print $href->{'ID'} . ": " . $href->{'TITLE'} . "\n";
+  }
+  return 0;
+}
+
+sub show_show
+{
+  my ($dbh, $show_id) = @_;
+
+  my @carts = RHRD::rddb::get_show_carts($dbh, $show_id);
+  if(!defined $carts[0] && defined $carts[1]) {
+    print STDERR "$carts[1]: $carts[2]\n";
+    return 1;
+  }
+  my ($title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($dbh, $show_id);
+  unless (defined $title) {
+    print STDERR "$errorstring\n";
+    return 1;
+  }
+  print $title . ":\n";
+  for my $cart (@carts) {
+    print " > " . $cart . "\n";
+  }
+  return 0;
 }
 
+sub add_show
+{
+  my ($dbh, $shortname, $title, $numcarts, $rhythm, $dow, $starttime, $len) = @_;
+
+  print "add show $shortname, $title, $numcarts, $rhythm, $dow, $starttime, $len not yet implemented!\n";
+  return 0;
+}
+
+sub remove_show
+{
+  my ($dbh, $show_id) = @_;
+
+  print "removing show " . $show_id . ", not yet implemented!\n";
+  return 0;
+}
+
+my $num_args = $#ARGV + 1;
+if($num_args < 1) {
+  print_usage();
+  exit(1);
+}
 my $cmd = $ARGV[0];
+my $ret = 0;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
+my ($dbh, $status, $errorstring) = RHRD::rddb::opendb();
 if(defined $dbh) {
-  if($cmd eq "add") {
-    # TODO: implement this
-  } elsif($cmd eq "remove") {
-    # TODO: implement this
-  } else {
-    print STDERR "unknown command\n";
+  if($cmd eq "list") {
+    if($num_args != 1) {
+      print_usage();
+      exit(1);
+    }
+    $ret = list_shows($dbh)
+  }
+  elsif($cmd eq "show") {
+    if($num_args != 2) {
+      print_usage();
+      exit(1);
+    }
+    $ret = show_show($dbh, $ARGV[1])
+  }
+  elsif($cmd eq "remove") {
+    if($num_args != 2) {
+      print_usage();
+      exit(1);
+    }
+    $ret = remove_show($dbh, $ARGV[1]);
+  }
+  elsif($cmd eq "add") {
+    if($num_args != 8) {
+      print_usage();
+      exit(1);
+    }
+    $ret = add_show($dbh, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7]);
+  }
+  else {
+    print_usage();
     RHRD::rddb::closedb($dbh);
     exit 1;
   }
-
   RHRD::rddb::closedb($dbh);
 } else {
   print STDERR "$errorstring\n";
   exit 1;
 }
 
-exit 0
+exit $ret;
-- 
cgit v0.10.2