#!/usr/bin/perl -w
use strict;
use vdef_globals;
use Getopt::Std;
use DBI();

my( $id, @row );

my( $dbh, $sth );
my( $rows );
my $dbpass;

our( $opt_m, $opt_h, $opt_u, $opt_g, $opt_F );
getopts("mhugF");

if( $#ARGV!=0 )
{
   usage();
   exit 1;
}
$id = $ARGV[0];


$dbpass = readpw();

$dbh = DBI->connect($vdef_globals::db_datasource,
                    $vdef_globals::db_user, $dbpass,{'RaiseError' => 1});
$sth = $dbh->prepare("SELECT id,crypt,clear,name,uid,gid,home,maildir FROM users WHERE id like \"".$id."\"");
$sth->execute();
my $f_id = 0;
my $f_crypt = 1;
my $f_clear = 2;
my $f_name = 3;
my $f_uid = 4;
my $f_gid = 5;
my $f_home = 6;
my $f_maildir = 7;
while ( @row = $sth->fetchrow_array ) {
   print $row[$f_id] . "\n";
   print "   home    = " . $row[$f_home] . "\n" if defined $opt_h;
   print "   maildir = " . $row[$f_maildir] . "\n" if defined $opt_m;
   print "   uid     = " . $row[$f_uid] . "\n" if defined $opt_u;
   print "   gid     = " . $row[$f_gid] . "\n" if defined $opt_g;
   if( defined $opt_F )
   {
      my( @alias, $stq );
      $stq = $dbh->prepare("SELECT id FROM users WHERE maildir like \"".$id."\"");
      $stq->execute();
      while ( @alias = $stq->fetchrow_array ) {
         print "   fwd from  " . $alias[0] . "\n";
      }
      $stq->finish;   
   }
}
$sth->finish;
$dbh->disconnect();

sub usage
{
   print "usage: vmailuser_show [-mhug] email-address\n";
   print "       -m shows the maildir\n";
   print "       -h shows the home\n";
   print "       -u shows the UID\n";
   print "       -g shows the GID\n";
   print "       -F shows the aliases which forward to email-address\n";
}

