summaryrefslogtreecommitdiff
path: root/utils/rhrd-show
blob: dad08009708b9b9eb23992f0b1f9443b1a9d8801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/perl -w
#
#  rhrdlibs
#
#  Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
#
#  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 <http://www.gnu.org/licenses/>.
#

use strict;
use lib "../lib/";
use RHRD::rddb;
use RHRD::utils;
use Date::Calc;

use Data::Dumper::Simple;

sub print_usage
{
  print STDERR "Usage: rhrd-show list [ <group> ]\n" .
               "       rhrd-show search <expression>\n" .
               "       rhrd-show (show|remove) <show-id>\n" .
               "       rhrd-show add <groupname> <name> <title> <num-carts> <rhythm> <dow> <starttime> <len>\n" .
               "       rhrd-show edit <show-id> <title> <rhythm> <dow> <starttime> <len>\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-add-id|multi-remove-id) <multi-show-id> <show-id> [ <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;
    print $show->{'ID'} . ": " . $show->{'TITLE'} . ", ";
    print join(", ", $show->{'RHYTHM'}, Date::Calc::Day_of_Week_to_Text($show->{'DOW'}), $show->{'STARTTIME'}, ($show->{'LEN'} . " min"));
    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;
  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 $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);
  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) = @_;

  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, $showid, $title, $rhythm, $dow, $starttime, $len) = @_;

  my $ret = edit__check_arguments($showid, $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_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);
  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_add__check_shows
{
  my $ctx = shift;
  my @shows = @_;

  my %weeks = ( 1 => 0, 2 => 0, 3 => 0, 4 => 0 );

  foreach my $show (@shows) {
    my ($week, $show_id) = split(':', $show, 2);
    unless(defined $week && defined $show_id) {
      print STDERR "'" . $show . "' is invalid, needs to have format <week>:<showid>\n";
      return 1;
    }

    $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 1;
    }
    if($weeks{$week} != 0) {
      print STDERR "week " . $week . " is already set to show-id $weeks{$week}\n";
      return 1;
    }

    $show_id = int($show_id);
    my ($show_id_min, $show_id_max, $errorstring) = RHRD::rddb::get_showid_range($ctx);
    unless(defined $show_id_min) {
      print STDERR $show_id_max . ": " . $errorstring . "\n";
      return 1;
    }
    if ($show_id < $show_id_min || $show_id > $show_id_max) {
      print STDERR "show-id '" . $show_id . "' is out of range (min: $show_id_min, max: $show_id_max)\n";
      return 1;
    }
    (my $exists, my $status, $errorstring) = RHRD::rddb::check_show_exists($ctx, $show_id);
    unless(defined $exists) {
      print STDERR $status . ": " . $errorstring . "\n";
      return 1;
    }
    if($exists != 1) {
      print STDERR "show with id '" . $show_id . "' does not exist!\n";
      return 1;
    }

    (my $title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($ctx, $show_id);
    unless(defined $title) {
      print STDERR $status . ": " . $errorstring . "\n";
      return 1;
    }
    $weeks{$week} = $show_id;

    print "W$week: ($show_id) $title\n";
  }

  return 0;
}

sub multi_add
{
  my $ctx = shift;
  my $title = shift;
  my @shows = @_;

  my $ret = multi_add__check_shows($ctx, @shows);
  if($ret) {
    return $ret;
  }

  my ($result, $status, $errorstring) = RHRD::rddb::create_multi_show($ctx, $title, @shows);
  unless(defined $result) {
    print STDERR $status . ": " . $errorstring . "\n";
    return 1;
  }

  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 != 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]);
    }
  }
  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)
    }
  }
  else {
    print_usage();
    $ret = 1;
  }
  RHRD::rddb::destroy($ctx);
} else {
  print STDERR "$errorstring\n";
  $ret = 1;
}

exit $ret;