--- orig.aw.cgi	Sat Jul  8 21:06:38 2006
+++ aw.cgi	Mon Jul 10 13:36:45 2006
@@ -53,6 +53,7 @@
 $options{leftfill} = "undefined.rfk";
 $options{rightmenu} = "undefined.rfk";
 $options{rightfill} = "undefined.rfk";
+$options{textwrap} = 1; # for now leave on, but later maybe a command line switch
 
 
 # load our defaults into %options
@@ -187,6 +188,7 @@
 my $docdir = $options{docdir}; # highest subdirectory that is permissible to read from (i.e. /var/www/)
 my $dynamic_debugmode = $options{dynamic_debugmode};
 $debugmode = $options{debugmode};
+my $textwrap = $options{textwrap};
 
 
 # And will you be running in static or dynamic mode today?
@@ -292,15 +294,8 @@
 # We need to figure out which column (L, R, or Center) is longest since the
 # whole document will need to be formed around the longest column.
 
-my $longest; # This variable holds the length of the longest column.
+my $longest = get_longest(); # This variable holds the length of the longest column.
 
-if ($#L_MENU >= $#CENTER && $#L_MENU >= $#R_MENU){
-  $longest = $#L_MENU;
-}elsif ($#CENTER >= $#L_MENU && $#CENTER >= $#R_MENU) {
-  $longest = $#CENTER;
-}else{
-  $longest = $#R_MENU;
-}
 
 # PADDING OUT COLUMNS:
 # In order to have straight columns, we need to pad out each column to the 
@@ -327,18 +322,30 @@
 my $plays_well_with_others = 1;
 
 for (my $j = 0; $j <= $#CENTER; $j++) {
-	my $bokbar = $CENTER[$j]; # don't count HTML as space taking columns...
- 	$bokbar =~ s/<[^>]+>//ig; # remove all html for column counting, since html tags are "invisible".
- 	$bokbar =~ s/&\w+;/X/ig; # replace html escape sequences with one character for counting since they appear as one character.
+	my $bokbar = get_bokbar ($CENTER[$j]); # don't count HTML as space taking columns...
  	if (length($bokbar) > $ccolwidth) {
- 		$plays_well_with_others = 0; # @CENTER is too big for it's britches. R_MENU will not be displayed.
- 	} else {
- 		for (my $blanks = length($bokbar); $blanks < $ccolwidth; $blanks++) {
- 		$CENTER[$j] = $CENTER[$j] . " "; # fill out the end of the line with spaces.
+		if ($textwrap) {
+			wrap_line ($ccolwidth, $j);
+			$bokbar = get_bokbar ($CENTER[$j]);
+		} else {
+			# @CENTER is too big for it's britches. R_MENU will not be displayed.
+ 			$plays_well_with_others = 0; 
+		}
+ 	} 
+ 		
+	#moved from else, if we did line_wrap then we still want padding
+	if ($plays_well_with_others == 1) {	
+		for (my $blanks = length($bokbar); $blanks < $ccolwidth; $blanks++) {
+ 			$CENTER[$j] = $CENTER[$j] . " "; # fill out the end of the line with spaces.
  		}
 	}
 } # end row padding / width check section. 
 
+#re-calculate longest if we did any textwrapping
+if ($textwrap) {
+  $longest=get_longest();
+}
+
 # FILLER FILES:
 # -------------
 # There are also three "filler" files -- left, center, and right. These files contain 
@@ -446,6 +453,99 @@
 # SUBROUTINES!
 # ------------
 # This is where all the fancy stuff happens.
+
+sub wrap_line { # arguments: $width, $j
+# Wraps text in lines in center collumn that are too long. Also we can't use
+# rindex because we need to not count html tags as part of the width.
+# The basic logic for this is:
+# 1) find the last whitespace before width
+# 2) if found split the string at this point and append the stuff on the right to 
+#    the front of next_line
+# 3) else do a hard break at width 
+
+  (my $width, my $j) = @_;
+  my $last_whitespc = -1; 
+  my $tag_start = 0; # handleing wrapping of links the idea is to preserve the
+  my $tag_stop = 0;  # original <a href> tag, add a </a> to the end of $cur_line
+  my $in_link = 0;   # and then duplicate the href at the front of $next_line
+  my $tag;
+  my $length = 0; 
+  my $cur_line = $CENTER[$j];
+  my $next_line = "";
+  my @line = split(//,$cur_line);
+  my $break_point;
+  
+  if ($width > length($cur_line)) { # sanity check
+	#uhoh calling code made an error
+	die "wrap_line called with bad arguments";
+  }
+
+  for (my $k=0; $length < $width; $k++) {
+	if ($line[$k] eq "<") { # inside tag
+		$tag_start = $k;
+		while ($k <= $#line && $line[$k] ne ">") {$k++;}
+		$tag_stop = $k;
+	
+		#save tag we may need it for later
+		$tag = substr ($cur_line, $tag_start, $tag_stop-$tag_start+1);
+		if ($in_link && $tag =~ /\/a/) {
+			$in_link = 0;
+		} elsif ($tag =~ /a href/) {
+			$in_link = 1;
+		}
+	} else {
+		$length++;
+		if ($line[$k] =~ /\s/) {
+			$last_whitespc=$k;
+		}elsif ($line[$k] eq "&") { #kinda a hack skip till we find ";" 
+                       	while ($k <= $#line && $line[$k] ne ";") {$k++;}
+		}
+	}
+  }
+  
+
+  if ($last_whitespc < 0) {
+  	$next_line = substr($cur_line, $width);
+  	$cur_line =  substr($cur_line, 0, $width);
+  } else {
+  	$next_line = substr($cur_line, $last_whitespc +1);
+  	$cur_line =  substr($cur_line, 0, $last_whitespc);
+  }
+
+  if ($in_link) {
+	$cur_line = $cur_line . "</a>";
+	$next_line = $tag . $next_line;
+  }
+
+  $CENTER[$j] = $cur_line;
+
+  if ($j == $#CENTER) { # if we are at the end
+	push(@CENTER, $next_line);
+  } elsif ($CENTER[$j+1] !~ /\S/) { # if next line is blank, then insert a line
+	splice (@CENTER, $j+1, 1, $next_line, " ");	
+  } else {
+	$CENTER[$j+1] = $next_line . " " . $CENTER[$j+1];
+  }
+} # end wrap_line
+
+sub get_bokbar {
+  my $bokbar = $_[0]; # don't count HTML as space taking columns...
+  $bokbar =~ s/<[^>]+>//ig; # remove all html for column counting, since html tags are "invisible".
+  $bokbar =~ s/&\w+;/X/ig; # replace html escape sequences with one character for counting since they appear as one character.
+  return $bokbar;
+} # end get_bokbar
+
+sub get_longest { #with text wrapping we are increasing the lenght and will need to re-compute
+  my $longest;
+  if ($#L_MENU >= $#CENTER && $#L_MENU >= $#R_MENU){
+    $longest = $#L_MENU;
+  }elsif ($#CENTER >= $#L_MENU && $#CENTER >= $#R_MENU) {
+    $longest = $#CENTER;
+  }else{
+    $longest = $#R_MENU;
+  }
+  return $longest;
+} # end get_longest
 
 sub get_page {
 # get_page does most of the complicated work. It gets the page, 
