summaryrefslogtreecommitdiff
path: root/utils/rd-group
blob: a221d9f26c4aa6508a617d32e49a09d36908edc4 (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
#!/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 RHRD::rddb;

my $num_args = $#ARGV + 1;
if ($num_args < 2) {
  print STDERR "Usage: rd-group ( (check|remove|get-carts|get-reports) <groupname>                        |\n" .
               "                   add <groupname> [ <description> ]                                      |\n" .
               "                   set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]]  |\n" .
               "                   set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]]  )\n";
  exit 1;
}

my $cmd = $ARGV[0];
my $groupname = $ARGV[1];

my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
if(defined $dbh) {
  if($cmd eq "check") {
    (my $result, my $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
    print "$result, $status: $errorstring\n";
    RHRD::rddb::closedb($dbh);
    exit $result;
  } elsif($cmd eq "add") {
    (my $cnt, undef, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $ARGV[2]);
    unless(defined $cnt) {
      print "$errorstring\n";
      RHRD::rddb::closedb($dbh);
      exit 1;
    }
    print int($cnt) . " rows affected\n";
  } elsif($cmd eq "remove") {
    my @results = RHRD::rddb::remove_group($dbh, $groupname);
    if(!defined $results[0] && defined $results[2]) {
      print "$results[2]\n";
    } else {
      for my $href (@results) {
        print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
      }
    }
  } elsif($cmd eq "get-carts") {
    my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname);
    unless(defined $low) {
      print "$type\n";
      RHRD::rddb::closedb($dbh);
      exit 1;
    }
    print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n";
  } elsif($cmd eq "set-carts") {
    (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
    unless(defined $cnt) {
      print "$errorstring\n";
      RHRD::rddb::closedb($dbh);
      exit 1;
    }
    print int($cnt) . " rows affected\n";
  } elsif($cmd eq "get-reports") {
    my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname);
    unless(defined $nownext) {
      print "$music\n";
      RHRD::rddb::closedb($dbh);
      exit 1;
    }
    print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n";
  } elsif($cmd eq "set-reports") {
    (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
    unless(defined $cnt) {
      print "$errorstring\n";
      RHRD::rddb::closedb($dbh);
      exit 1;
    }
    print int($cnt) . " rows affected\n";
  } else {
    print STDERR "unknown command\n";
    RHRD::rddb::closedb($dbh);
    exit 1;
  }

  RHRD::rddb::closedb($dbh);
} else {
  print STDERR "$errorstring\n";
  exit 1;
}

exit 0