# -- perl --

# Jun 14 2202
# modified by Suryadi Suryadi for distribution

if (@ARGV[0] eq "--help")
{
    &ExtendedHelp;
    exit;
}

$i=0;
while(@ARGV[$i])
{
    &CheckOptions(@ARGV[$i]);
    $i = $i + 1;
}


if ((@ARGV[0] eq "-a") || (@ARGV[1] eq "-a"))
{
	$try=`rwho -a > ~/.garbage`;
	if (@ARGV[1] eq "-f")
	{
		@ARGV[0]="-f";
	}
}
else
{
	$try=`rwho > ~/.garbage`;
}

&PrintOnScreen;
$try=`rm ~/.garbage`;





#*************************************************
#****************** PROCEDURES *******************
#*************************************************


sub PrintOnScreen
{
    $DIR = $ENV{"HOME"};
    open(MYFILE,"$DIR/.garbage") or die "Can't Open File  \n";
    if (@ARGV[0] eq "-f")
    {
	print("USER       FROM\n");	
    }
    else
    {
    	print("USER       Date  Time\n");
    }
    $line=<MYFILE>;
    while ($line ne "")
    {	     
	$line=~s/( +)/ /g;
	@line1=split(/ /,$line);
	chomp($line1[4]);
	if (@ARGV[0] eq "-f")
	{
		printf("%-10s %s\n",$line1[0],$line1[1]);
	}
	else
	{
		printf("%-10s %-3s%+2s %-5s\n",$line1[0],$line1[2],$line1[3],$line1[4]);
	}
	$line=<MYFILE>;
    }
    close(MYFILE);
}


sub CheckOptions
{
    my($Option)=@_;
    if (($Option ne "-a") && ($Option ne "-f"))
    {
	&Help;
	exit;
    }
}

sub Help
{
    print("\nUSAGE: rwho [-a] [-f] [--help] \n\n");
}

sub ExtendedHelp
{
    print("rwho - Show who is logged on\nand the time they logged on\n\n"); 

    print("Options\n");
    print("-f: From Where did they log on\n");
    print("-a: If a user has not typed \nanything for more than an hour\nit will be ignored unless -a\noption is specified\n"); 
}

