#!/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 (show|remove) \n" . " rhrd-show add <num-carts> <rhythm> <dow> <starttime> <len>\n" . " rhrd-show edit <show-id> <title> <rhythm> <dow> <starttime> <len>\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 show { my ($ctx, $show_id) = @_; my @carts = RHRD::rddb::get_show_carts($ctx, $show_id); 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, $show_id); unless (defined $show) { print STDERR "$errorstring\n"; return 1; } $show->{'DOW'} = 7 if $show->{'DOW'} == 0; print $show->{'TITLE'} . ": " . join(", ", $show->{'RHYTHM'}, Date::Calc::Day_of_Week_to_Text($show->{'DOW'}), $show->{'STARTTIME'}, ($show->{'LEN'} . " min")) . "\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) = @_; 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; } return 0; } sub add { my ($ctx, $groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len) = @_; my $ret = add__check_arguments($groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len); 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 $show_id, $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 = " . $show_id . "\n"; ($result, $status, $errorstring) = RHRD::rddb::create_show_dropbox($ctx, $groupname, $show_id, $rhythm, $dow, $starttime, $len); 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 ($show_id, $title, $rhythm, $dow, $starttime, $len) = @_; 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; } return 0; } sub edit { my ($ctx, $show_id, $title, $rhythm, $dow, $starttime, $len) = @_; my $ret = edit__check_arguments($show_id, $title, $rhythm, $dow, $starttime, $len); if($ret) { return $ret; } ($dow, undef, undef) = RHRD::utils::cmdline_dow($dow); my ($result, $status, $errorstring) = RHRD::rddb::update_show_dropbox($ctx, $show_id, $rhythm, $dow, $starttime, $len); unless(defined $result) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } if ($result != 1) { print "show '" . $show_id ."' does not exist.\n"; return 1; } print "show '" . $show_id . "' successfully changed!\n"; return 0; } sub remove { my ($ctx, $show_id) = @_; my ($group, $status, $errorstring) = RHRD::rddb::get_show_group($ctx, $show_id); unless(defined $group) { print STDERR $status . ": " . $errorstring . "\n"; return 1; } my @results = RHRD::rddb::remove_show($ctx, $show_id); 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"; } 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 "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 != 9) { print_usage(); $ret = 1; } else { $ret = add($ctx, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7], $ARGV[8]); } } elsif($cmd eq "edit") { if($num_args != 7) { print_usage(); $ret = 1; } else { $ret = edit($ctx, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6]); } } else { print_usage(); $ret = 1; } RHRD::rddb::destroy($ctx); } else { print STDERR "$errorstring\n"; $ret = 1; } exit $ret;