#!/afs/athena/contrib/perl/perl
#
# PROGRAM:      watchznol.pl
# AUTHOR:       Erik Nygren <nygren@mit.edu>
# DATE:         1/21/93
# VERSION:	2.0b	- Port to Perl, classifies activity and adds options
#		1.0	- Original
# USAGE:        watchznol [-h] [-f <file>] [-u <user>] [-i <instance>]
#			[-O <opcode>] [-c <class>] [[-t] <time>]
# PURPOSE:      Watch list of users logged in and notify when it changes.
# DEFAULTS:	Default time is 10 minutes
#		Default file is ~/.anyone
#		Default class is message
#		Default instance is personal
#		Default opcode is nol
#

# Set up default options
%opts = ('-i', 'personal',
	 '-c', 'message',
	 '-O', 'nol',
	 '-t', 10*60);		# Ten minutes times 30 secs per minute
@userstolist = ();		# Clear users to list

# Usage text
$usagetext = <<EOS;
Usage:  $0 [-h] [-f <file>] [-u <user>] [-i <instance>]
                  [-O <opcode>] [-c <class>] [[-t] <time>]
	-h	Prints this message
	-f	File of names to use (~/.anyone by default), - is stdin
	-u	User name to use
	-i	Zephyr instance to send to
	-c	Zephyr class to send to
	-O	Zephyr option to send with
	-t	Time in seconds between checks
Send comments and questions to nygren@mit.edu
EOS

# Parse command line options
$lastflag = '';
for $curarg ($[ .. $ARGV)
{
    if ($lastflag)	
    {
	if ($lastflag eq '-u')
	{
	    @userstolist = (@userstolist,$curarg);
	}
	if ($lastflag eq '-f')
	{
	    if ($curarg eq '-')
	    { @usersfromfile = <STDIN>; }
	    else
	    {
		open (NAMEFILE,$curarg) || die $0.": can't open file $curarg \n"; 
		@usersfromfile = <NAMEFILE>;
		close (NAMEFILE);
	    }
	    chop @usersfromfile;
	    @userstolist = (@userstolist,@usersfromfile);
	}
	$opts{$lastflag} = $curarg;
	$lastflag = "";
    }
    elsif (substr($curarg,0,1) ne "-")  # Not an option or param so time
    {
	unless ($opts{'-t'} = int($curarg))
	{
	    die $0.': invalid time\n\n'.$usagetext;
	}
    }
    else
    {
	if ($curerg eq '-h')
	{
	    die $usagetext;
	}
	$lastflag = $curarg;
    }
}
# Parsing loop done.  Now finish parsing last of command line options.
if ($lastflag)
{
    die $0.": invalid arguement\n".$usagetext;
}
unless (@userstolist)
{
    open (NAMEFILE,'~/.anyone') || die $0.": can't open file ~/.anyone\n"; 
    @usersfromfile = <NAMEFILE>;
    close (NAMEFILE);
}

print "Done parsing!\n";
print "Users:\n";
print @usersfromfile;
print "\nFlags:\n";
print %opts;
print "\n";









