# -- perl -- 

&PrintOnScreen($ARGV[0],$ARGV[1]);

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

sub PrintOnScreen
{
    my($flag,$who)=@_;
    if ($flag ==1)
    {
    	#getting line:Kernel IP routing table  
		$line=<STDIN>;
		#print ($line);
		#getting headers
		$line=<STDIN>;
      
      #search for who
		$found=0;
      $line = <STDIN>;
      chomp($line);
      while ($line ne "")
      {
         $line=~ s/(\s+)/ /g;
         @line2=split(/ /,$line);
         if ( ($line2[7] eq $who) || ($who eq "gtwy" && ($line2[0] eq
         "default" || $line2[0] eq "0.0.0.0")) )
         {
            $found = 1;
            last;
            
         }
         $line = <STDIN>;
         chomp($line);
      }
		if ($found==1)
		{
			printf("Dest:%15s Flgs:%s\n",$line2[0],$line2[3]);
			printf("Gtwy:%15s  Use:%s\n",$line2[1],$line2[6]);
			printf("Mask:%15s  IFC:%s\n",$line2[2],$line2[7]);
			printf("MSS:%5d Win:%5d irt:%6d\n",$line2[8],$line2[9],$line2[10],$line2[9]);
			
		}
		else
		{
			printf("Please enter a proper ifc ID.\n");
			printf("ID can be found by looking at \n");
			printf("the first column of w command \n");
		}
    }
    else
    {

 		#getting line:Kernel IP routing table  
		$line=<STDIN>;
		#getting headers
		$line=<STDIN>;
		printf("Iface  Destination\n");
	    $line=<STDIN>;
		$count=1;
     	while($line ne "")
    	{
  			chomp($line);
   		$line=~s/( +)/ /g;
     		@line1=split(/ /,$line);
         if ($line1[0]=~s/default/gtwy/g || $line1[0]=~s/0.0.0.0/gtwy/g)
         {
            printf("%5s %s\n",$line1[0],$line1[1]);
         }
         else
         {
			   printf("%5s %s\n",$line1[7],$line1[0]);
         }
			$count=$count+1;
     		$line=<STDIN>;
       	}
	
    }

}

