%%START_PERL%%
my $VERSION = '$Id: priveleges.pl.in,v 1.2.2.1 2001/01/15 18:52:05 jes Exp $ ';

use strict;
use Carp;
use Getopt::Long;
%%GENEX_EXTRALIBS%%
use Genex qw($SU_USERNAME $SU_PASSWORD);
use Bio::Genex::DBUtils qw(create_insert_sql);

my $user;
my @privs;
my $rc = GetOptions('user=s' => \$user,
		    'privelege=s@' => \@privs);

my $USAGE = <<"EOU";
usage: $0 --user=user_name --privelege={SELECT,DELETE,INSERT}
EOU

die "Bad option: $rc\n$USAGE" unless $rc;
die "Must specify --user\n$USAGE" unless defined $user;

my $priveleges;
unless (scalar @privs) {
  $priveleges = 'SELECT';
} else {
  $priveleges = join(',',@privs);
}

print STDERR "Giving $priveleges to $user\n";

# open up a writeable connection
$Bio::Genex::USER = $SU_USERNAME;
$Bio::Genex::PASSWORD = $SU_PASSWORD;
$Bio::Genex::TRANSACTION = 1;	# do it in a transaction

my $dbh = Bio::Genex::current_connection();

my $sth = $dbh->table_info() || die $DBI::errstr;

while (my $info = $sth->fetchrow_arrayref) {
  die $DBI::errstr if $sth->err;

  next unless $info->[3] eq 'TABLE';
  print STDERR "granting  $priveleges on $info->[2]\n";
  $dbh->do("GRANT $priveleges ON $info->[2] TO $user") || die $DBI::errstr;
}

$sth->finish();
$dbh->commit();
$dbh->disconnect();
exit(0);
