#!/usr/bin/perl -w
#
# Try to retrieve all AD users
#
# By Igor Gubenko (igubenko@Princeton.EDU), 05/21/2012
#

use lib qw(/usr/local/perl/lib/perl5);
#use lib qw(/usr/local/perl/modules/lib/perl5/site_perl/5.8.8 /usr/local/perl/modules/lib/perl5/5.8.8 /usr/local/perl/modules/lib64/perl5/site_perl/5.8.8 /usr/local/perl/modules/lib64/perl5/5.8.8 /var/local/groundwork/perl/lib/site_perl/5.8.8/);

use strict;
use WWW::Mechanize;

sub usage;

my $progname = $0;

usage unless @ARGV == 0;

my $mech = WWW::Mechanize->new (agent => 'Mozilla/5.0', stack_depth => '1', noproxy => '1', quiet => '1', onerror => undef, onwarn => undef, timeout => '60') or die ("Unable to instantiate WWW::Mechanize!\n");

my $url_ini = "https://tools.princeton.edu/ListADUsers/DumpAllUsers.aspx";

# Retrieve the AD users
$mech->get ($url_ini);

unless ($mech -> success()) {
	print "Failed to fetch AD users from \"$url_ini\". Answer: " . $mech->status() . "\n";
	exit 2;
}

my @rows = split (/\n/, $mech->response()->decoded_content);
my $nrows = @rows;

print "Successfully retrieved $nrows users\n";
exit 0;

sub usage {
	print "Usage: $progname\n";
	exit 2;
}
