#!/usr/bin/perl

#
# $Id: sysadmin,v 4.1 2004/11/22 19:24:42 matt Exp $
#

use strict;
use Sys::Hostname;

use lib "lib";
use Mail::Toaster::Utility 1.0;  my $utility = Mail::Toaster::Utility->new();
use Mail::Toaster::Provision;

use vars qw/ $VERSION $iam $whoami $opt_v /;

$VERSION = "3.00";

=head1 NAME

sysadmin

=head1 SYNOPSIS

sysadmin is a handy script used to manage a unix system.

=head1 DESCRIPTION

sysadmin began as a provisioning agent maildomain, later renamed
mailadmin, and finally evolving into sysadmin. It allows 
non-root users to perform admin tasks on behalf of end users 
(ie, for tech support personnel).

=cut

my $conf          = $utility->parse_config({file=>"sysadmin.conf"});

# Redirect STDERR to STDOUT so we can see what's printed out in a die() call.
#open(STDERR, ">&STDOUT");
#select(STDERR); $|=1; select(STDOUT); $|=1;

my $provision = Mail::Toaster::Provision->new($ARGV[0]);

$iam = $provision->what_am_i();
exit unless $provision->what_am_i_check($iam);

$ENV{'GATEWAY_INTERFACE'} ? ProcessCGI($iam) : ProcessShell($iam);

1;


sub ProcessShell
{
	use Getopt::Std;
	getopts('v');

	my $debug = 0; $debug = 1 if ($opt_v);

	print "ProcessShell...begin $iam. \n" if $debug;

	unless ($ARGV[0]) { $provision->usage_action(); exit 0 };

	my %vals = ( 
		'action'  => $ARGV[0],
		'debug'   => $opt_v,
	);

	if    ( $iam eq "dnsadmin"  ) { $provision->dns (\%vals, $conf) }
	elsif ( $iam eq "mailadmin" ) { $provision->mail(\%vals, $conf) }
	elsif ( $iam eq "useradmin" ) { $provision->user(\%vals, $conf) }
	elsif ( $iam eq "webadmin"  ) { $provision->web (\%vals, $conf) }
	else                          { $provision->usage(); exit 0;    };

	print "ProcessShell...end\n" if $debug;
};

sub ProcessCGI
{
	print "$iam cgi processing\n";
};


=head1 Dependencies

Quota  (for file system quotas)
Apache::ConfigFile

=cut

