# -- perl --
#************************************************************
#		  DHAVAL RAJ
#	LINACCESS PROJECT For PROFESSOR MANN
#
#
# The following script is for the command "w"
# It has following options
# -u   : Print just the header information
# id   : Print Information about the user with id=<id>
# 
#
#************************************************************

# Jun 14 2002
# modified by Suryadi Suryadi for distribution

$i=0;
$flag=0;
$user="";

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

while(@ARGV[$i])
{
    @Option=split(//,@ARGV[$i]);
    if ($Option[0] eq "-")
    {
    	&CheckOptions(@ARGV[$i]);
    }
    else
    {
	$flag=1;
	$user=@ARGV[$i];
    }
    $i=$i+1;
}

if ($flag==1)
{
	@ARGV[0]=$user;
}
$try=`w > ~/.garbage`;
&PrintOnScreen($flag);
$try=`rm ~/.garbage`;





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


sub PrintOnScreen
{
    $DIR = $ENV{"HOME"};
    my($IncludeType)=@_;
    open(MYFILE,"$DIR/.garbage") or die "Can't Open File  \n";
    if ($IncludeType ==1)
    {
	$line=<MYFILE>;
	$line=<MYFILE>;
	$found=0;
	for ($start=1;$start<=@ARGV[0];$start++)
	{
		$line=<MYFILE>;
		$found=1;
	}
	$line=~s/( +)/ /g;
	@line2=split(/ /,$line);
	if (($line ne "") && ($found==1))
	{
		chomp($line2[7]);
		printf("User   :%s\n",$line2[0]);
		printf("Login@ :%s\n",$line2[3]);
		printf("From   :%s\n",$line2[2]);
		printf("TTY    :%s\n",$line2[1]);
		printf("Idle   :%s\n",$line2[4]);
		printf("JCPU   :%s\n",$line2[5]);
		printf("PCPU   :%s\n",$line2[6]);
		printf("What   :%s\n",$line2[7]);
	}
	else
	{
		printf("Please enter a proper user ID.\n");
		printf("ID can be found by looking at \n");
		printf("the first column of w command \n");
	}
    }
    else
    {
	if ($i==0)
	{
		$line=<MYFILE>;
       		$line=<MYFILE>;
		print("  User     Login@   What\n");
       		$line=<MYFILE>;
		$count=1;
       		while($line ne "")
    		{
	     		$line=~s/( +)/ /g;
	     		@line1=split(/ /,$line);
			chomp($line1[7]);
			printf("%1d %-8s %-8s %-8s\n",$count,$line1[0],$line1[3],$line1[7]);
			$count=$count+1;
	     		$line=<MYFILE>;
        	}

	}
	else
	{
    		$DIR = $ENV{"HOME"};
		open(MYFILE,"$DIR/.garbage") or die "Can't Open File  \n";
		$line=<MYFILE>;
	     	$line=~s/( +)/ /g;
		@line1=split(/,/,$line);
		@TimeAndUpSince=split(/up/,$line1[0]);
		printf("Time      :%s\n",$TimeAndUpSince[0]);
		printf("Up Since  :%s %s\n",$TimeAndUpSince[1]);
		printf("# of Users:%s\n",$line1[1]);
		@Avg=split(/ /,$line1[2]);
		chomp($line1[4]);
		printf("Load Avg  : %s%s%s\n",$Avg[3],$line1[4],$line1[4]);

	}
    }
    close(MYFILE);
}


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

sub Help
{
    print("\nUSAGE: w [-u] [UserID] [--help] \n\n");
}

sub ExtendedHelp
{
    print("w - Show who is logged on and\n"); 
    print("    what they are doing\n\n");
    print("Options\n");
    print("id   : Give me information\n"); 
    print("       just about the user\n");
    print("       with that id\n");
    print("-u   : Just print the header\n");
}

