#!/usr/bin/perl -w # # rhrdlibs # # Copyright (C) 2015-2016 Christian Pointner # # This file is part of rhrdlibs. # # rhrdlibs is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # rhrdlibs is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with rhrdlibs. If not, see . # use strict; use RHRD::rddb; use RHRD::utils; use Date::Calc; sub print_usage { print STDERR "Usage: rhrd-show list [ ]\n" . " rhrd-show search \n" . " rhrd-show (show|remove) \n" . " rhrd-show add <num-carts> <rhythm> <dow> <starttime> <len> <type>\n" . " rhrd-show edit <show-id> <title> <rhythm> <dow> <starttime> <len> <type>\n" . "\n" . " multi show handling:\n" . " rhrd-show multi-list\n" . " rhrd-show (multi-show|multi-remove) <multi-show-id>\n" . " rhrd-show multi-add <title> <week>=<show-id> [ <week>=<show-id> [ .. ] ]\n" . " rhrd-show multi-edit <multi-show-id> <title> <week>=<show-id> [ <week>=<show-id> [ .. ] ]\n"; } sub list { my ($ctx, $group) = @_; my @shows = RHRD::rddb::list_shows($ctx, $group); if(!defined $shows[0] && defined $shows[1]) { print STDERR "$shows[1]: $shows[2]"; return 1; } my @sorted = sort { lc($a->{'TITLE'}) cmp lc($b->{'TITLE'}) } @shows; for my $href (@sorted) { print $href->{'ID'} . ": " . $href->{'TITLE'} . "\n"; } return 0; } sub search { my ($ctx, $searchexp) = @_; my @shows = RHRD::rddb::list_shows($ctx); if(!defined $shows[0] && defined $shows[1]) { print STDERR "$shows[1]: $shows[2]"; return 1; } my @sorted = sort { lc($a->{'TITLE'}) cmp lc($b->{'TITLE'}) } @shows; for my $show (@sorted) { next unless $show->{'TITLE'} =~ /$searchexp/i; $show->{'DOW'} = 7 if $show->{'DOW'} == 0; my $showtype = RHRD::utils::dropbox_param_showtype_to_string($show->{'TYPE'}); print $show->{'ID'} . ": " . $show->{'TITLE'} . ", "; print join(", ", $show->{'RHYTHM'}, Date::Calc::Day_of_Week_to_Text($show->{'DOW'}), $show->{'STARTTIME'}, ($show->{'LEN'} . " min"), $showtype); print ", group: " . $show->{'GROUP'} . "\n"; } return 0; } sub show { my ($ctx, $showid) = @_; my @carts = RHRD::rddb::get_show_carts($ctx, $showid); if(!defined $carts[0] && defined $carts[1]) { print STDERR "$carts[1]: $carts[2]\n"; return 1; } my ($show, $status, $errorstring) = RHRD::rddb::get_show_info($ctx, $showid); unless (defined $show) { print STDERR "$errorstring\n"; return 1; } $show->{'DOW'} = 7 if $show->{'DOW'} == 0; my $showtype = RHRD::utils::dropbox_param_showtype_to_string($show->{'TYPE'}); print $show->{'TITLE'} . ": " . join(", ", $show->{'RHYTHM'}, Date::Calc::Day_of_Week_to_Text($show->{'DOW'}), $show->{'STARTTIME'}, ($show->{'LEN'} . " min"), $showtype) . "\n"; print " group: " . $show->{'GROUP'} . ", carts(" . scalar(@carts) . "): " . join(", ", @carts) . "\n"; return 0; } sub add__check_arguments { my ($groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len, $type) = @_; if($groupname !~ m/^[-a-zA-Z0-9_]{1,10}$/) { print STDERR "name '" . $groupname . "' contains illegal characters or is too long/short\n"; print STDERR " only a-z, A-Z, 0-9 and _,- are allowed and the length must be between 1 and 10\n"; return 1; } if($name !~ m/^[a-zA-Z0-9_]{1,10}$/) { print STDERR "name '" . $name . "' contains illegal characters or is too long/short\n"; print STDERR " only a-z, A-Z, 0-9 and _ are allowed and the length must be between 1 and 10\n"; return 1; } if($num_carts <= 0) { print STDERR "num-carts '" . $num_carts . "' must be > 0\n"; return 1; } my ($result, $err, $hint) = RHRD::utils::dropbox_param_rhythm_ok($rhythm); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::cmdline_dow($dow); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_starttime_ok($starttime); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_len_ok($len); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_showtype_ok($type); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } return 0; } sub add { my ($ctx, $groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len, $type) = @_; my $ret = add__check_arguments($groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len, $type); if($ret) { return $ret; } ($dow, undef, undef) = RHRD::utils::cmdline_dow($dow); print " * creating show: " . $title . " (" . $name . ") for group '" . $groupname . "'\n"; my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } my $low_cart = 0; if($result) { print " * using existing group '" . $groupname . "'\n"; ($low_cart, $status, $errorstring) = RHRD::rddb::get_next_free_show_group_carts($ctx, $groupname, $num_carts); } else { print " * '" . $groupname . "' does not exist - creating it\n"; ($low_cart, $status, $errorstring) = RHRD::rddb::create_show_group($ctx, $groupname); } unless(defined $low_cart) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } my $high_cart = $low_cart + $num_carts - 1; print " * will be using carts: " . $low_cart . " - " . $high_cart . "\n"; ($result, $status, $errorstring) = RHRD::rddb::create_show_log($ctx, $name, $low_cart, $high_cart); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " * created log with name: " . $name . "\n"; (my $showid, $status, $errorstring) = RHRD::rddb::create_show_macro_cart($ctx, $name, $title); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " * created macro cart -> new show-id = " . $showid . "\n"; ($result, $status, $errorstring) = RHRD::rddb::create_show_dropbox($ctx, $groupname, $showid, $rhythm, $dow, $starttime, $len, $type); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " * created dropbox for show .. " . $result . " rows affected\n"; print " * finished\n"; return 0; } sub edit__check_arguments { my ($showid, $title, $rhythm, $dow, $starttime, $len, $type) = @_; my ($result, $err, $hint) = RHRD::utils::dropbox_param_rhythm_ok($rhythm); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::cmdline_dow($dow); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_starttime_ok($starttime); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_len_ok($len); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } ($result, $err, $hint) = RHRD::utils::dropbox_param_showtype_ok($type); unless($result) { print STDERR $err . "\n " . $hint . "\n"; return 1; } return 0; } sub edit { my ($ctx, $showid, $title, $rhythm, $dow, $starttime, $len, $type) = @_; my $ret = edit__check_arguments($showid, $title, $rhythm, $dow, $starttime, $len, $type); if($ret) { return $ret; } ($dow, undef, undef) = RHRD::utils::cmdline_dow($dow); my ($result, $status, $errorstring) = RHRD::rddb::update_show_title($ctx, $showid, $title); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } if ($result != 1) { print "show '" . $showid ."' does not exist.\n"; return 1; } ($result, $status, $errorstring) = RHRD::rddb::update_show_dropbox($ctx, $showid, $rhythm, $dow, $starttime, $len, $type); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } if ($result != 1) { print "show '" . $showid ."' does not exist.\n"; return 1; } print "show '" . $showid . "' successfully changed!\n"; return 0; } sub remove { my ($ctx, $showid) = @_; my ($group, $status, $errorstring) = RHRD::rddb::get_show_group($ctx, $showid); unless(defined $group) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } my @results = RHRD::rddb::remove_show($ctx, $showid); if(!defined $results[0] && defined $results[2]) { print STDERR $results[1] . ": " . $results[2] . "\n"; return 1; } for my $href (@results) { print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; } my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $group); if(!defined $carts[0] && defined $carts[1]) { print STDERR $carts[1] . ": " . $carts[2] . "\n"; return 1; } if(scalar @carts == 0) { print ">> group '" . $group . "' is now empty .. you should probably remove it!\n\n"; } my @args = ($showid); my ($ret, $log) = RHRD::utils::pv_execute_action('remove_automation_id', undef, @args); if($ret) { print "Error: removing showid from PV failed:"; print $log; } else { print "removed show $showid from PV\n"; } return 0; } sub multi_list { my ($ctx) = @_; my @mshows = RHRD::rddb::list_multi_shows($ctx); if(!defined $mshows[0] && defined $mshows[1]) { print STDERR "$mshows[1]: $mshows[2]"; return 1; } my @sorted = sort { lc($a->{'TITLE'}) cmp lc($b->{'TITLE'}) } @mshows; for my $href (@sorted) { my %showids = %{$href->{'SHOWS'}}; my $showstr = join(", ", map { "W$_: $showids{$_}" } sort keys %showids); print $href->{'ID'} . ": " . $href->{'TITLE'} . " (" . $showstr . ")\n"; } return 0; } sub multi__parse_shows { my $ctx = shift; my %shows = ( 1 => 0, 2 => 0, 3 => 0, 4 => 0 ); foreach my $show (@_) { my ($week, $showid) = split('=', $show, 2); unless(defined $week && defined $showid) { print STDERR "'" . $show . "' is invalid, needs to have format <week>=<showid>\n"; return undef; } $week = int($week); if($week < 1 || $week > 4) { print STDERR "week '" . $week . "' is out of range (needs to be 1,2,3 or 4)\n"; return undef; } if($shows{$week} != 0) { print STDERR "week " . $week . " is already set to show-id $shows{$week}\n"; return undef; } $showid = int($showid); my ($showid_min, $showid_max, $errorstring) = RHRD::rddb::get_showid_range($ctx); unless(defined $showid_min) { print STDERR $showid_max . ": " . $errorstring . "\n"; return undef; } if ($showid < $showid_min || $showid > $showid_max) { print STDERR "show-id '" . $showid . "' is out of range (min: $showid_min, max: $showid_max)\n"; return undef; } (my $exists, my $status, $errorstring) = RHRD::rddb::check_show_exists($ctx, $showid); unless(defined $exists) { print STDERR $status . ": " . $errorstring . "\n"; return undef; } if($exists != 1) { print STDERR "show with id '" . $showid . "' does not exist!\n"; return undef; } (my $title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($ctx, $showid); unless(defined $title) { print STDERR $status . ": " . $errorstring . "\n"; return undef; } $shows{$week} = $showid; print " - W$week: ($showid) $title\n"; } return \%shows; } sub multi_add { my $ctx = shift; my $title = shift; print " * creating multi-show: " . $title . "\n"; my $showref = multi__parse_shows($ctx, @_); unless(defined($showref)) { return 1; } my %shows = %{$showref}; my ($result, $status, $errorstring) = RHRD::rddb::create_multi_show($ctx, $title, \%shows); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " * finished\n"; return 0; } sub multi_edit { my $ctx = shift; my $showid = shift; my $title = shift; print " * updating multi-show: " . $showid . "\n"; my $showref = multi__parse_shows($ctx, @_); unless(defined($showref)) { return 1; } my %shows = %{$showref}; my ($result, $status, $errorstring) = RHRD::rddb::update_multi_show($ctx, $showid, $title, \%shows); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " * mulit-show '" . $showid . "' successfully changed!\n"; return 0; } sub multi_show { my ($ctx, $showid) = @_; my ($show, $status, $errorstring) = RHRD::rddb::get_multi_show_info($ctx, $showid); unless (defined $show) { print STDERR "$errorstring\n"; return 1; } print $show->{'TITLE'} . ":\n"; my %showids = %{$show->{'SHOWS'}}; foreach my $week (sort keys %showids) { if($showids{$week} > 0) { (my $title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($ctx, $showids{$week}); unless(defined $title) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print " - W" . $week . ": (" . $showids{$week} . ") " . $title . "\n"; } } return 0; } sub multi_remove { my ($ctx, $showid) = @_; my ($showid_min, $showid_max, $errorstring) = RHRD::rddb::get_multi_showid_range($ctx); unless(defined $showid_min) { print STDERR $showid_max . ": " . $errorstring . "\n"; return 1; } if ($showid < $showid_min || $showid > $showid_max) { print STDERR "multi-show-id '" . $showid . "' is out of range (min: $showid_min, max: $showid_max)\n"; return 1; } (my $result, my $status, $errorstring) = RHRD::rddb::remove_multi_show($ctx, $showid); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } print int($result) . " rows affected\n"; if($result > 0) { my @args = ($showid); my ($ret, $log) = RHRD::utils::pv_execute_action('remove_automation_id', undef, @args); if($ret) { print "Error: removing showid from PV failed:"; print $log; } else { print "removed show $showid from PV\n"; } } return 0; } my $num_args = $#ARGV + 1; if($num_args < 1) { print_usage(); exit(1); } my $cmd = $ARGV[0]; my $ret = 0; my ($ctx, $status, $errorstring) = RHRD::rddb::init(); if(defined $ctx) { if($cmd eq "list") { if($num_args > 2) { print_usage(); $ret = 1; } else { $ret = list($ctx, $ARGV[1]) } } elsif($cmd eq "search") { if($num_args != 2) { print_usage(); $ret = 1; } else { $ret = search($ctx, $ARGV[1]) } } elsif($cmd eq "show") { if($num_args != 2) { print_usage(); $ret = 1; } else { $ret = show($ctx, $ARGV[1]) } } elsif($cmd eq "remove") { if($num_args != 2) { print_usage(); $ret = 1; } else { $ret = remove($ctx, $ARGV[1]); } } elsif($cmd eq "add") { if($num_args != 10) { print_usage(); $ret = 1; } else { $ret = add($ctx, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7], $ARGV[8], $ARGV[9]); } } elsif($cmd eq "edit") { if($num_args != 8) { print_usage(); $ret = 1; } else { $ret = edit($ctx, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7]); } } elsif($cmd eq "multi-list") { if($num_args != 1) { print_usage(); $ret = 1; } else { $ret = multi_list($ctx) } } elsif($cmd eq "multi-add") { if($num_args < 3) { print_usage(); $ret = 1; } else { shift @ARGV; my $title = shift @ARGV; $ret = multi_add($ctx, $title, @ARGV) } } elsif($cmd eq "multi-show") { if($num_args != 2) { print_usage(); $ret = 1; } else { $ret = multi_show($ctx, $ARGV[1]); } } elsif($cmd eq "multi-remove") { if($num_args != 2) { print_usage(); $ret = 1; } else { $ret = multi_remove($ctx, $ARGV[1]); } } elsif($cmd eq "multi-edit") { if($num_args < 4) { print_usage(); $ret = 1; } else { shift @ARGV; my $showid = shift @ARGV; my $title = shift @ARGV; $ret = multi_edit($ctx, $showid, $title, @ARGV) } } else { print_usage(); $ret = 1; } RHRD::rddb::destroy($ctx); } else { print STDERR "$errorstring\n"; $ret = 1; } exit $ret;