# -- perl --

# configuration constants
$size = 30;#width of terminal
@format = ("%-5d"," %3s"," %-4s"," %4s",10);
@columns =(0,1,2,3,4);


&PrintOnScreen($ARGV[0]);

#*************************************************
#****************** PROCEDURES *******************
#*************************************************
sub PrintOnScreen {

   my($flag)=@_;
   
   if ($flag)
   {

      #get header names
      $line=<STDIN>;
      chomp($line);
      #$line=~ s/(\s+)/ /g;
      @headers=split(' ',$line);
           
      #get info
      $line=<STDIN>;
       chomp($line);
      while($line ne "")
      {
         #$line=~ s/(\s+)/ /g;
         @linesplit=split(' ',$line);
         
         &PrintInfo(\@headers,\@linesplit);

         #get next line
         $line=<STDIN>;
         chomp($line);         
      }
   }
   else #output list
   {   
      #remove headers
	   $line=<STDIN>;
      chomp($line);

      #parse 1 row at a time
      $line=<STDIN>;
      chomp($line);
	   while($line ne "")
	   {
             #$line=~ s/(\s+)/ /g;
             @linesplit=split(' ',$line);
             &PrintRow(@linesplit);
             #get next line
  	      $line=<STDIN>;
             chomp($line);         
	  }
   }

}
	
sub PrintRow {
       my(@fields) = @_;
       for($i=0;$i<@columns-1;$i++)
       {
          printf ($format[$i],$fields[$columns[$i]]);
       }
       &PrintName($fields[$columns[$i]],$format[$i]);
       print "\n";
}


sub PrintName {
	my($name,$NumberOfTimes)=@_;
	@name1=split(//,$name);
        print " ";
	for($i=0;$i<$NumberOfTimes;$i++)
	{
		printf("%s",$name1[$i]);
	} 
}


sub PrintInfo {
   my($headers,$line) = @_;
   
   $pos = 0; #current pos on line
   $indx = 0; #used to align words in @line with corresponding header
   
   foreach my $header (@$headers)
   {
      $pos = $pos + length($header) + length(@$line[$indx]) + 2;
      #print "\n$pos\n";
      if ($pos > $size)
      {
         print "\n";
         $pos = length($header) + 2 + length(@$line[$indx]) ;
      }
      #the const 2 is for ": " added in between the header and it's info
      print "$header: @$line[$indx]";
      
      #put a space before putting next field
      if ($pos == $size)
      {
         print "\n";
         $pos = 0;
      }
      else
      {
         print ' ';
         $pos++;
      }
      $indx++;

   }
   print "\n";
}

