#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
use warnings;
use strict;

# Copyright 2004 Randy Smith
# $Id: vuser,v 1.3 2005/07/02 20:19:09 perlstalker Exp $

use Pod::Usage;
use Getopt::Long;
use FindBin;
use Config::IniFiles;

our $REVISION = (split (' ', '$Revision: 1.3 $'))[1];
our $VERSION = '0.1.0';

our $DEBUG = 0;

BEGIN {

    our @etc_dirs = ('/usr/local/etc',
		     '/usr/local/etc/vuser',
		     '/etc',
		     '/etc/vuser',
		     "$FindBin::Bin/../etc",
		     "$FindBin::Bin",
		     "$FindBin::Bin/..",
                     "$FindBin::Bin/vuser",
                     "$FindBin::Bin/../etc/vuser"
                     );
}

use vars qw(@etc_dirs);

use lib (map { "$_/extensions" } @etc_dirs);
use lib (map { "$_/lib" } @etc_dirs);

use VUser::ExtHandler;

my $config_file;
for my $etc_dir (@etc_dirs)
{
    if (-e "$etc_dir/vuser.conf") {
	$config_file = "$etc_dir/vuser.conf";
	last;
    }
}

if (not defined $config_file) {
    die "Unable to find a vuser.conf file in ".join (", ", @etc_dirs).".\n";
}

my %cfg;
tie %cfg, 'Config::IniFiles', (-file => $config_file);

my $eh = new VUser::ExtHandler (\%cfg);

my $keyword = shift @ARGV || 'help';
my $action = shift @ARGV;

# Actions cannot start with -
if (defined $action
    and $action =~ /^-/) {
    unshift @ARGV, $action;
    $action = '';
}

$action = '' unless defined $action;

# Ok. Now it's time to do the action.

eval { $eh->run_tasks($keyword, $action, \%cfg); };
warn $@ if $@;

eval { $eh->cleanup(%cfg); };

exit;

sub revision
{
    print $REVISION;
}

__END__

=head1 NAME

vuser - Virtual user management utility

=head1 SYNOPSIS

vuser module action [options]

=head1 OPTIONS

=head1 DESCRIPTION

=head1 BUGS

=head1 SEE ALSO

=head1 AUTHOR

Randy Smith <perlstalker@gmail.com>

=head1 LICENSE
 
 This file is part of vuser.
 
 vuser is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 vuser 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 General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with vuser; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut
