#!/tools/perl_5.004_01/bin/perl

#Written by:  Paul Dutra (paulo@xilinx.com)
#Date:    8/17/95
#Update:  12/12/96
#Determines who has a web directory on your system

($current)=getlogin || (getpwuid($<))[0] || die "Not a user here!!\n";

require "newgetopt.pl" || die "Failed: $!\n";
&NGetOpt("h","mail","path=s","title=s","site=s","page=s");

# you need to edit this for your own domain
$opt_site="web" unless ($opt_site);
# default personal web directory name
$UserDir = "xroads" unless ($ARGV[0]);
# default HTML output file
$opt_path="$ENV{'HOME'}/$UserDir" unless ($opt_path);
# default title
$opt_title = "Personal Pages" unless ($opt_title);
# default page name
$opt_page = "personal" unless ($opt_page);

&usage if ($@ || $opt_h);

sub usage
{
die <<"EndUsage";

usage: $0 [-h] [-mail] [-page] [-path] [-title] [-site] [web_directory]

Determines who has a web directory ("$UserDir") on your site

Display Options:
-h		Help -- just display this message and quit.

Input Options:
-mail		send mail to everyone w/a web directory
		(This is advertising--tell everyone)
-page	"name"	Page name 
		Default: "$opt_page"
		(This is advertising--tell everyone)
-path	"name"	Path of all output files
		Default: "$opt_path"
-title	"name"	Title specification of HTML pages.
		Default: "$opt_title"
-site	"name"	Site name. 
		Default: "$opt_site"

Author: Paulo Dutra (paulo\@xilinx.com)

EndUsage
}

sub by_lastname
{
   #compare last names, but if two users share the
   #same last name, then compare login name
   ($last{$a} cmp $last{$b}) || ($a cmp $b);
}

sub good_bye
{
local($found,$count) = @_;
local($percent) = ($found/$count)*100;
 
($found) || die "\nProcess stopped: No users found\n";
print "\nFound $found web listings\n";
printf ("%.2f%% of the users have a web directory\n", $percent);
}

sub header
{
local($title) = shift;
chop($today = `date`);          #date
 
print <<"HEADER";
<HTML><HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK=#0000EE VLINK=#551A8B ALINK=#FF0000>
 
<H2>$title</H2>
<B>Last updated:</B> <EM>$today</EM><P>
List generated by
<A HREF="/~paulo/script/whack.html">whack</A>
<HR SIZE=4 NOSHADE>
 
HEADER
}
 
sub trailer
{
local($return) = shift;
 
print <<"TRAILER";
<HR SIZE=4 NOSHADE>
return to <A HREF="$return">previous</A>
</BODY></HTML>
TRAILER
}

sub html_page
{
my($title,$path,$page) = @_;
my($default) = "$path/${page}.html";
 
open(WEB,"> $default") || die "Can't create $default: $!\n";
#change filehandle to the output file
local($oldhandle) = select(WEB);
 
&header($title);
print "<UL>\n";
foreach $login (sort by_lastname keys %last)
   {
   print "<LI><A HREF=\"/~$login\">$real{$login}</A>\n";
   }
print "</UL>\n";
&trailer("index.html");
 
close(WEB);
select($oldhandle);
print "$default has been created !!!\n";
chmod (0644,$default);
}

sub mail
{
local($login) = shift;
local($oldhandle) = select(MAIL);

open(MAIL,"|mail $login");

#print the actual message
print <<EOF;
Your web site has been added to a list of other web sites.
The list has been generated by a PERL script that reads through
the passwd file for the home directory path and then checks
for the existence of a personal web directory.
Check out the list at http://$site/~$current

EOF

close (MAIL);
select($oldhandle);
}

# The following sets STDERR to flush after each period
$|=1;
print "\n$0 -help for more options\n";
print "Processing ";

setpwent;		#initialize scan
while (@passwd=getpwent)
  {
  ++$count;
  print "." unless ($count % 50);
  #grab login name, GCOS, and home 
  ($login,$name,$homedir) = @passwd[0,6,7];
  $name = "???" unless $name;
  $homedir .= "/$UserDir";
  if (-d $homedir && -x _)
    {
    &mail($login) if ($opt_m && $current eq $login);
    ++$found;		#count how many listings
    $name =~ s/,.*//;   #throw away everything after first coma
    ($last = $name) =~ s/^(.*[^a-z])?([a-z]+.*).*/$2/i;
    $real{$login} = $name;
    $last =~ tr/A-Z/a-z/;
    $last{$login} = $last;
    }
  }
endpwent;	#all done
($found) || die "No users found -- Quiting!\n";

&good_bye($found,$count);              #good-bye message with some stats
&html_page($opt_title,$opt_path,$opt_page);

print "Thanks for using $0, $current\n\n";

exit
