#!/usr/bin/perl -w
my $base_dir = "/home";
my $web_dir = "/WWW";
my @home_dirs = ("ug01", "ug02", "ug03", "ug04", "honours");
my $default_regex =
"
This home page is currently unavailable\.<\/H3>";
my $header_file = "hdr.html";
my $footer_file = "ftr.html";
my @users = sort(&getAllUsers());
my $count = 0;
&printFile($header_file);
for my $user (@users) {
if (&hasHomePage($user)) {
print "
" . &getLink(&getUserName($user)) . "";
$count++;
}
}
print "" . $count . " out of " . $#users . " people have homepages";
&printFile($footer_file);
sub printFile {
if (!@_) {
die "printFile missing an argument";
}
my $file = shift;
open(TMPL, $file) || die "Could not open $file: $!";
print join("", );
close(TMPL);
}
sub getLink {
if (!@_) {
die "getLink missing an argument!";
}
my $user = shift;
return '' . $user . '';
}
sub getUserName {
if (!@_) {
die "getUserName missing an argument!";
}
$user = shift;
$user =~ s/$base_dir//;
$user =~ s/$web_dir//;
$user =~ s/\///g;
map {$user =~ s/$_//} @home_dirs;
return $user;
}
sub hasHomePage {
if (!@_) {
die "No user directory supplied to hasHomePage!";
}
my $rv = 0;
my $user = shift;
my $dir = $user . $web_dir;
if (!-f $dir . "/index.html") {
if (-f $dir . "/index.php") {
$rv = 1;
}
} else {
#note: we may not be able to open the file due to access restrictions
# though if we can't httpd can't either , so that is a false (even though
# the file exists)
open(USER, $dir . "/index.html") || return 0;
my $file = join("", );
if (!grep {/$default_regex/} $file) {
#strip whitespace and make sure this isn't 0-length
$file =~ s/\ //g;
if (length $file != 0) {
$rv = 1;
}
}
close(USER);
}
return $rv;
}
sub getAllUsers {
my @rv = ();
foreach my $home (@home_dirs) {
@rv = (@rv, &getUsersFromDir($home));
}
return @rv;
}
sub getUsersFromDir {
if (!@_) {
die "Fatal! No home dir provided to getUsersFromDir!";
}
my $home = $base_dir . "/" . shift;
opendir(DIR, $home) ||
die "Can't open directory ($home): $!";
my @files = map {$home . "/" . $_ }
grep {!/[\+\.]+/ && -d "$home/$_" } readdir(DIR);
closedir(DIR);
return @files;
}