summaryrefslogtreecommitdiff
path: root/utils/rd-show
blob: 7a2736c0674caef9b50f4bc9565b5b0bbda8d605 (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
#!/usr/bin/perl -w
#
#  rhrdlibs
#
#  Copyright (C) 2015 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;

# 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;
# }

sub print_usage
{
  print STDERR "Usage: rd-show list\n" .
               "       rd-show (show|remove) <show-id>\n" .
               "       rd-show add <groupname> <name> <title> <num-carts> <rhythm> <dow> <starttime> <len>\n";
}

sub list
{
  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
{
  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__get_group_carts
{
  my ($dbh, $groupname, $num_carts) = @_;

  my ($result, $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
  unless(defined $result) {
    print STDERR $status . ": " . $errorstring . "\n";
    return undef;
  }

  my $low_cart = 0;
  if($result) {
    print "   > using existing group '" . $groupname . "'\n";
    # TODO:
    #   find free range in group which is big enough for $num_carts carts

  } else {
    print "   > '" . $groupname . "' does not exist - creating it .. ";
    (my $cnt, $status, $errorstring) = RHRD::rddb::add_group($dbh, $groupname);
    unless(defined $cnt) {
      print STDERR $status . ": " . $errorstring . "\n";
      return undef;
    }
    print int($cnt) . " rows affected\n";

    ($low_cart, my $high_cart, $errorstring) = RHRD::rddb::get_shows_next_free_slot($dbh);
    if(!$low_cart) {
      print $high_cart . ": " . $errorstring . "\n";
      return undef;
    }
    print "     using carts " . $low_cart . " - " . $high_cart . " for new group .. ";

    ($cnt, $status, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $low_cart, $high_cart, 1, 'Y');
    unless(defined $cnt) {
      print STDERR $status . ": " . $errorstring . "\n";
      return undef;
    }
    print int($cnt) . " rows affected\n";

    print "     enabling reports  .. ";
    ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, 'Y', 'Y', 'Y');
    unless(defined $cnt) {
      print STDERR $status . ": " . $errorstring . "\n";
      return undef;
    }
    print int($cnt) . " rows affected\n";
  }

  return $low_cart;
}

sub add
{
  my ($dbh, $groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len) = @_;

  print " * creating show: " . $title . " (" . $name . ") for group '" . $groupname . "'\n";

  my $low_cart = add__get_group_carts($dbh, $groupname, $num_carts);
  return 1 unless defined($low_cart);
  my $high_cart = $low_cart + $num_carts - 1;
  print " * will be using carts: " . $low_cart . " - " . $high_cart . "\n";

  # TODO:
  #   create log named <name> and fill with $low_cart - $high_cart
  #   create macro cart referencing log -> show-id
  #   create dropbox for: groupname, show-id, $rhythm, $dow, $starttime, $len

  print " * finished\n";

  return 0;
}

sub remove
{
  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, $status, $errorstring) = RHRD::rddb::opendb();
if(defined $dbh) {
  if($cmd eq "list") {
    if($num_args != 1) {
      print_usage();
      $ret = 1;
    } else {
      $ret = list($dbh)
    }
  }
  elsif($cmd eq "show") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = show($dbh, $ARGV[1])
    }
  }
  elsif($cmd eq "remove") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = remove($dbh, $ARGV[1]);
    }
  }
  elsif($cmd eq "add") {
    if($num_args != 9) {
      print_usage();
      $ret = 1;
    } else {
      $ret = add($dbh, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7], $ARGV[8]);
    }
  }
  else {
    print_usage();
    $ret = 1;
  }
  RHRD::rddb::closedb($dbh);
} else {
  print STDERR "$errorstring\n";
  $ret = 1;
}

exit $ret;