#!/cygdrive/c/Perl/bin/perl 
# Program to create the classification pages for my bird journal.
# A flat file containing the bird classification, sub-class, bird
# name, url to detailed bird page, and picture to display are all
# in the file.  Also, optional text for each bird.
#
# Input for bird classifications (note the first word followed by
# a ":" is mandatory.  Otherwise, the line will generate an error.
# Blank lines are also ignored.
#   
# main:	tile of page, required
# 	main: Ducks, Geese, Swans
# sub:  Subtitle, optional
# 	sub:  Dabbling Ducks
# bird: name, url, picture (each item is separated by a colon) required
#	bird: Mallard: bird_Mallard.shtml 200612/Images_sm/DSC00288.JPG
# text: Optional, e.g.,
# 	text: A Mallard is a green bird
#
$new_file = 1;		# Controls whether a file has ever been opened
$new_title = 1;		# Controls if it is the first title in a file
$count = 1;		# current number of birds pictures in a row
$number = 1;		# number of pictures for a bird
$row_max = 3;		# number of pictures per row
$title = "";		# classification title, e.g., Warblers
$subtitle="";		# sub-class, e.g., Dabbling Ducks
$filename = "";		# holds filename of the current open bird file
$bird_text = "        ";# temp. holds row text until ready to print
{
   foreach (<STDIN>) {
        chomp;
	@name = split /:/;
			# skip blank lines
	next if /^(\s)*$/;
			# skip comment lines
	next if ($name[0] =~ /#/);
	$num = $#name;
	for ($i = 0; $i <= $num; $i++) {
	    $name[$i] =~ s/^\s+//;	# get rid of any beginning spaces
	    $name[$i] =~ s/\s+$//;	# get rid of any ending spaces
	}
			# Now make sure it is a valid line, i.e., begins
			# with one of the following:
			# bird, header, title, or text
			# get rid of any beginning spaces
	$_ = $name[0];
	if (/title/) {
			# if another file opened, write end stuff and 
			# close it
	    if (!$new_file) {
		    # print extra cell if only two has been printed
		    # Otherwise, the spacing is off on the table
		if ($count=3) {print OUT "<td></td>\n";}
		&print_bird_text;
		&print_end;
		close OUT;
		$new_title = 1;
            }
	    $filename = "class_" . $name[1] . ".shtml";
	    $filename =~ s/\s+//g;
	    $filename =~ s/,+//g;
	    $new_file = 0;
	    open OUT, ">$filename" or die "Can't open $filename for writing\n";
	    &print_header1;
	    print OUT "<title>$name[1]", '</title>';
	    &print_header2;
	    print OUT '<a name="atop"></a>';
	    print OUT '<h2 style="font-family: arial;',
	              'color: rg(50%,50%,50%);">',
	    	      $name[1], '</h2>', "\n";
	    print OUT "<p>Clicking on an image will bring up more images of that bird.</p>";
	    $count = 1;
		
	}elsif (/header/) {
	    if ($new_title) {
		    print OUT "    <h3>$name[1]</h3>\n";
		    &start_table;
		    $new_title = 0;
	    	    $count = 1;
	    }else{
		    	# print extra cell if only two has been printed
		    	# Otherwise, the spacing is off on the table
		    if ($count=3) {print OUT "<td></td>\n";}
		    &print_bird_text;
		    &end_table;
		    print OUT "    <h3> $name[1] </h3>\n";
		    &start_table;
		    $new_title=0;
	    	    $count = 1;
	    }
	}elsif (/bird/) {
	    if ($count > $row_max) {
		    print OUT "       </tr>\n";
		    &print_bird_text;
		    print OUT "       <tr>\n";
		    $count=1;
	    }
	    print OUT '        <td valign="bottom" width="205">' . "\n";
	    print OUT '        <a href="' . $name[2] .' "> <img src="' .
	    	 $name[3] . '"' . "></a>\n";
	    		;
	    print OUT "        </td>\n";
	    $bird_text =  $bird_text . '        <td valign="top" width="205">'
	   		 . "<b>" .  $name[1]  . "</b></td>\n";
	    $count = $count+1;
	}elsif (/text/) {
			# replace ending </td> with a newline
			# since I want to add some text to the bird
			# name cell because a "text" line was added.
			# It should work for multiple text lines.
		$bird_text =~ s,</td>$,<br />,;
		$bird_text = $bird_text . "        " . $name[1] . "\n" .
			"        </td>\n";
	}else {
		print "error:  unrecognized line: @line\n";
		next;
	}

   }
   if ($count=3) {print OUT "<td></td>\n";}
   &print_bird_text;
   &print_end;
   close OUT;
}
sub print_bird_text{
   print OUT "        </tr>\n"; 
   print OUT "        <tr>\n"; 
   print OUT $bird_text; 
   print OUT "        </tr>\n"; 
   $bird_text = "        ";
}
sub start_table {
	print OUT '        <table width="650">';
	print OUT "\n";
	print OUT "        <tr>\n";
}
sub  end_table {
	print OUT "        </tr>\n";
	print OUT "        </table>\n";
}


sub print_header1 {
	print OUT <<HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html>
<head>
HTML
}

sub print_header2 {
	print OUT <<HTML;
</head>
<!-- 
The following lines are needed for each web page using the
	head and menu. -->
<body>
<!-- Include Header -->
<!--#include virtual="birds_header.html"-->
<table>
<tr>
<td valign="top">
<!-- Include menu -->
<!--#include virtual="birds_menu.html"-->
</td>
<td width="20"></td>
<td valign="top">
<!-- End of needed lines -->

HTML
}

sub print_end {
	print OUT <<HTML;
	  </tr>
	</table>
	<br />
	<a href="#atop"><b>Top of Page</b></a>
<!-- The following lines are needed for each web page using the
	head and menu. -->
	</td></tr>
	</table>
</td>
</tr>
</table>
</body>
</html>

HTML
}
