#  DelUser.pl
#  Example 2.4:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32;
use Getopt::Long;
use Win32::NetAdmin;
use Win32::AdminMisc;

Configure( \%Config );
if( $Config{help} )
{
  Syntax();
  exit();
}

if( "" ne $Config{domain} )
{

  # Must first assign some value to the machine key otherwise
  # GetDomainController() will fail.
  $Config{machine} = "";
  Win32::NetAdmin::GetDomainController( '', 
                                        $Config{domain}, 
                                        $Config{machine} );
}
elsif( "" eq $Config{machine} )
{
  # Must first assign some value to the machine key otherwise
  # GetDomainController() will fail.
  $Config{machine} = "";
  Win32::NetAdmin::GetDomainController( '', 
                                        Win32::DomainName(), 
                                        $Config{machine} );
}

foreach my $Account ( @{$Config{accounts}} )
{
  if( $Account =~ /\*$/ )
  {
    my( $Prefix ) = ( $Account =~ /^(.*)\*$/ );
    my @Users;
    Win32::AdminMisc::GetUsers( $Config{machine}, $Prefix, \@Users );
    map
    {
      $AccountList{lc $_} = 1;
    } ( @Users );
  }
  else
  {
    $AccountList{lc $Account} = 1;
  }
}

foreach my $Account ( keys( %AccountList ) )
{
  print "Account '$Config{machine}\\$Account'...";
  if( Win32::NetAdmin::UserDelete( $Config{machine}, $Account ) )
  {
    print "deleted.\n";
  }
  else
  {
    my $Error =  Win32::FormatMessage( Win32::NetAdmin::GetError() );
    $Error =~ s/\r|\n//g;
    print "failed to delete ($Error)\n";
  }
}

sub Configure
{
  my( $Config ) = @_;
  
  Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
  $Result = GetOptions( $Config, 
                         qw(
                             machine|m=s
                             domain|d=s
                             help|?
                         )
                     );
  
  $Config->{help} = 1 if( ! $Result );
  if( "" ne $Config->{machine} )
  {
    $Config->{machine} = "\\\\$Config->{machine}";
    $Config->{machine} =~ s/^(\\\\)+/\\\\/;
  }  
  push( @{$Config->{accounts}}, @ARGV );
  $Config->{help} = 1 if( ! scalar @{$Config->{accounts}} );
}

sub Syntax
{
  my( $Script ) = ( $0 =~ /([^\\\/]*?)$/ );
  my( $Line ) = "-" x length( $Script );

  print <<EOT;

$Script
$Line
Deletes an account.

Syntax:
    perl $Script [-m machine | -d domain] Account [ Account2 ... ]
        -m Machine..Specify a machine where the user account lives.
        -d Domain...Specify a domain where the user account lives.
        Account.....The name of the account (the userid).
                    This account can end with a * char to indicate
                    all accounts that begin with the specified string.
  	    If no domain or machine is specified then the current domain
        is used.
EOT
}
