#! /srvtools/http/bin/perl
#############################################################################
#  
#   File:       PrintHeader2.pl
#
#   Author:     Jungmin Kim
#
#   Created:    04/02/95
#   Modified: 
#   Version: 
#
#   Input:      path name, file name, user information, 
#               address of array
#   Output:     HTML text
#         
#
#   Description: Print out headers of messages in the directory 'path name'
#               with numerical order
#
#############################################################################

sub PrintHeader2{
   local($dir, $no, $user, $subdir, *array) = @_;
   # local($dir, $no, $user) = @_;
   local($number, $date, $key, $name, $url, $title);
   local($total_len, $title_len, $key_len, $indent, $tno, $temp);
   local(%files, $filename);

   $HEADER_LENGTH = 52;   # Total number of chars to display a header
   
    # Sort out the $dir $subdir stuff!!
   $filename = "$webboardhome/$subdir/$dir/$index";
   # $filename = "$webboardhome/$dir/$index";
   open(INFILE,"< $filename");

   while(<INFILE>) {
      # separator for each field is eot(\005) ****************** modified
	($number, $date, $name, $title) = split(/\005/, $_);

	# replace <BR> and <P> with a space
	$temp = $title;
	$title =~ s/\<P\>/ /ig;
	$title =~ s/\<BR\>/ /ig;
	$files{$number} = "$date\005$name\005$title";
       # *****************************************************************

	$number =~ /^([0-9]+).*/;
	$prefix = $1;
	#### if there is no file name passed, take last message.
	$no = $prefix if $no eq "";

	#### if the prefix of the file name is matched, add it to array
	if ($prefix > $no - $range && $prefix < $no + $range) {
	   # separator is eot(\005) ***************************** modified
	   $array{$number} = "$date\005$name\005$temp";
	   # *************************************************************
       }

   }
   close(INFILE);

   ## Print out the list of message headers (number, subject, author, date)
   print "<pre>\n";
   print "                   <B>Subject</B>                               ";  
   print "<B>Author</B>              <B>Date</B>\n";
   foreach $key (sort dotorder keys %files) {
      $key =~ /^([0-9]+).*/;
      $tno = $1;                 # prefix of file name (root message)
      ($date, $name, $title) = split(/\005/, $files{$key});
      $url = "<A HREF=\"$action/$subdir\:read\:${dir}\:${tno}\:${user}#$key\">"; 
      $key_len = length($key);
      ## Count the number of periods in the $key, to see if it is a
      ## topic, response, or comment
      @temp = split(/\./, $key);
      $numperiods = @temp - 1;
      ## (This is a crap way of doing it, but I don't know a better way)
      $indent = 2 * $numperiods;
      $total_len = $HEADER_LENGTH; 
      $title_len = $total_len - $key_len - $indent;
      chop($title);
      $title = substr($title, 0, $title_len);   # Crop the title length
      $addlen = 4;  # This is for </A>
      ## Identify the topics 
      # if (!($key =~ /\./)) {  
      if ($numperiods == 0) {  
         $key = "<B>$key</B>";
         # $title = "<B>$title</B>";
         # $addlen += 7;  # This is for <B> </B>
      }
      $title .= "</A>";  # This adds an extra 4 characters
      $title_len += $addlen;
      $key_len += $indent;
      printf("%${key_len}s %s%-${title_len}s  %-20s  %-10s \n", 
             $key, $url, $title, $name, $date);
   }
   print "</pre>";

}
1;


