use python2 explicitly for f31
[plcrt.git] / addwatchers.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use vars qw($PROMPT $VERSION $Handle $Nobody $SystemUser $item);
4 use vars
5   qw(@Groups @Users @ACL @Queues @Watchers @ScripActions @ScripConditions @Templates @CustomFields @Scrips @Attributes);
6
7 use lib "/usr/local/lib/rt3/lib";
8 use lib "/usr/lib/perl5/vendor_perl/5.8.8";
9
10 #This drags in  RT's config.pm
11 # We do it in a begin block because RT::Handle needs to know the type to do its
12 # inheritance
13 use RT;
14 use Carp;
15 use RT::User;
16 use RT::CurrentUser;
17 use RT::Template;
18 use RT::ScripAction;
19 use RT::ACE;
20 use RT::Group;
21 use RT::User;
22 use RT::Queue;
23 use RT::ScripCondition;
24 use RT::CustomField;
25 use RT::Scrip;
26
27 RT::LoadConfig();
28 use Term::ReadKey;
29 use Getopt::Long;
30
31 my %args;
32
33 GetOptions(
34     \%args,
35     'prompt-for-dba-password', 'force', 'debug',
36     'action=s',                'dba=s', 'dba-password=s', 'datafile=s',
37     'datadir=s'
38 );
39
40 unless ( $args{'action'} ) {
41     help();
42     exit(-1);
43 }
44
45 $| = 1;    #unbuffer that output.
46
47 require RT::Handle;
48 my $Handle = RT::Handle->new($RT::DatabaseType);
49 $Handle->BuildDSN;
50 my $dbh;
51
52 if ( $args{'prompt-for-dba-password'} ) {
53     $args{'dba-password'} = get_dba_password();
54     chomp( $args{'dba-password'} );
55 }
56
57 if ( $args{'action'} eq 'insert' ) {
58     insert_data( $args{'datafile'} || ($args{'datadir'}."/content") );
59 }
60 else {
61     print STDERR "$0 called with an invalid --action parameter\n";
62     exit(-1);
63 }
64
65 sub insert_data {
66     my $datafile = shift;
67
68     #Connect to the database and get RT::SystemUser and RT::Nobody loaded
69     RT::Init;
70
71     my $CurrentUser = RT::CurrentUser->new();
72     $CurrentUser->LoadByName('RT_System');
73
74     # Slurp in stuff to insert from the datafile. Possible things to go in here:-
75     # @groups, @users, @acl, @queues, @ScripActions, @ScripConditions, @templates
76
77     print "LBLALDLJDLDKJ " . "\n";
78     print $datafile . "\n";
79
80     require $datafile
81       || die "Couldn't find initial data for import\n" . $@;
82
83     if ( @Watchers ) {
84         print "Creating Watchers...";
85         for $item (@Watchers) {
86             my $queue_entry = new RT::Queue($CurrentUser);
87             my $watchers = $item->{'Watchers'};
88             delete $item->{'Watchers'};
89             my ( $return, $msg ) = $queue_entry->Load($item->{'Queue'});
90
91             print "TEST---\n";
92             print $return. " ".  $msg . "\n";
93             foreach my $watcher  ( @{$watchers} ) {
94                 my ( $eval, $emsg ) = $queue_entry->AddWatcher(%$watcher);
95                 print "(Error: $emsg)\n" unless $eval;
96             }
97
98             print "(Error: $msg)" unless $return;
99             print $return. ".";
100         }
101         print "done.\n";
102     }
103     $RT::Handle->Disconnect() unless $RT::DatabaseType eq 'SQLite';
104     print "Done setting up database content.\n";
105 }
106
107 sub help {
108
109     print <<EOF;
110
111 $0: Set up RT's database
112
113 --action        init    Initialize the database
114                 drop    Drop the database.
115                         This will ERASE ALL YOUR DATA
116                 insert  Insert data into RT's database.
117                         By default, will use RT's installation data.
118                         To use a local or supplementary datafile, specify it
119                         using the '--datafile' option below.
120
121                 acl     Initialize only the database ACLs
122                         To use a local or supplementary datafile, specify it
123                         using the '--datadir' option below.
124
125                 schema  Initialize only the database schema
126                         To use a local or supplementary datafile, specify it
127                         using the '--datadir' option below.
128
129 --datafile /path/to/datafile
130 --datadir /path/to/              Used to specify a path to find the local
131                                 database schema and acls to be installed.
132
133
134 --dba                           dba's username
135 --dba-password                  dba's password
136 --prompt-for-dba-password       Ask for the database administrator's password interactively
137
138
139 EOF
140
141 }
142
143 1;