#!/cygdrive/c/strawberry/perl/bin/perl 
# Program to create the classification pages for my bird journal and
# it generates the bird index page. 
# The program reads a flat file containing the bird or place
# classification, sub-class, bird name, url to detailed bird page,
# and thumbnail to display.
# The program also searches for files in ../text_class for optional
# text for each classification "header".
#
# Input for bird classifications (note the first word followed by
# a ":" is mandatory.  Otherwise, the line will generate an error.
# Blank lines are ignored.
#   
# title:title of bird page, required, e.g.,
# 	title: Ducks, Geese, Swans
# ptitle:title of page for places, required, e.g.,
# 	ptitle: Lousiana
# header: Subclass of "title", e.g., "Dabling Ducks" for Duck, etc.
# 	header:  Dabbling Ducks
#
#		 Example bird line:
# 	bird: name: url pointer: pointer to thumbnail picture : option breeding date : option nonbreeding date
# 		(each item is separated by a colon) required, e.g.,
#	bird: Mallard: bird_Mallard.shtml 200612/Images_sm/DSC00288.JPG:Jun-Aug:Sep-May
#			or
# 	place:name of place:url pointer: pointer to thumbnail picture
#
$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
$row_max = 3;		# number of pictures per row
$filename = "";		# holds filename of the current open bird file
@birdlist;		# hold bird lines to create bird index html page
$bird_index = "class_index.shtml";	# file handle for outputting bird index
$menu_name = "birds_menu.html";  # left side menu to print
@inline=[100];		# temporarily hold the bird indexes for a specific bird
$text_title = "../text_title/";	# text for under the titles for class pages.
				# used to print internal links for index page
{
   foreach (<STDIN>) {
			# skip blank lines
	next if /^(\s)*$/;
			# skip comment lines
	next if /^#/;
	   		# get rid of beginning blanks
	s/^(\s*)//;
			# If a bird line, add it to a list for later sorting
			# to create the bird index.
	if (/^bird/) {
		$line = $_;
   		$line =~ s/^bird:\s*//;
			# set up for the bird name being first, e.g.,
			# American Wigeon becomes:
			#   Wigeon, American
			# get the first field which has the bird name
		@l_fields = split /:/, $line;
			# now split that first field on spaces
		@b_fields = split / /, $l_fields[0];
			# get the last field and put it first
		$name = pop (@b_fields);
		$n = @b_fields;
		if ($n > 0) {
			$name = $name . ",";
		}
		foreach $i (@b_fields) {
			$name = $name . " " . $i;
		}
		$l_fields[0] = $name;
		$birdname = join ":" , @l_fields;
		push (@birdlist,$birdname);
	}
        chomp;
			# split original line into components
	@name = split /:/;
	$num = $#name;
			# cycle thru all fields
	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, or title
	$_ = $name[0];
	if (/title/ || /ptitle/) {
			# first get rid of any commas (filenames don't
			# have commas)
	    s/,//g;
			# if another file opened, write end stuff and 
			# close it
	    if (!$new_file) {
		    # print extra cell if only one has been printed
		    # Otherwise, the spacing is off on the table
#		if ($count=2) {
#			print OUT "<td>&nbsp;</td>\n";
#			print OUT "<td>&nbsp;</td>\n";
#		}
		&print_end;
		close OUT;
		$new_title = 1;
            }
	    $filename = $name[1];
	    $filename =~ s/\s+//g;
	    $filename =~ s/,+//g;
	    $title_text = $filename;
	    $filename = "class_" . $filename . ".shtml";
	    $new_file = 0;
	    open OUT, ">$filename" or die "Can't open $filename for writing\n";
	    &print_header1;
	    print OUT "<title>$name[1]", '</title>';
			 # need to define the left menu that will be put on the pages
			 # for either the birds or the places.
	    $menu_name="birds_menu.html";
	    if (/ptitle/) {
	    	$menu_name="places_menu.html";
	    }
	    &print_header2;
	    print OUT '        <a name="atop"></a>';
	    print OUT '        <h2 style="font-family: arial; color: rg(50%,50%,50%);">',
	    	      $name[1], '</h2>', "\n";
	    $count = 1;
	    		# see if there is any text for the title
	    $title_text = $text_title . $title_text . ".txt";
	    if (-f $title_text) {
		     open TEXT, "<$title_text" or die "Can't open $title_text for reading\n";
		     while (<TEXT>) {
			     print OUT "        ", $_;
		     }
		     close TEXT;
             }
		
	}elsif (/header/) {
			# prepare header name for looking for text for headers
	    $head = $name[1];
	    chomp($head);
	    $head =~ s/\s+//g;
	    $head =~ s/'//g;
	    $head =~ s/,//g;
	    if ($new_title) {
		    print OUT "        <h3>$name[1]</h3>\n";
		    &print_bird_text;
		    &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=2) {print OUT "         <td></td>\n";}
		    &end_table;
		    print OUT "        <h3> $name[1] </h3>\n";
		    &print_bird_text;
		    &start_table;
		    $new_title=0;
	    	    $count = 1;
	    }
	}elsif (/bird/ || /place/) {
	    if ($count > $row_max) {
		    print OUT "       </tr>\n";
		    print OUT "       <tr>\n";
		    $count=1;
	    }
	    print OUT '        <td style="vertical-align:top;" width="160">' . "\n";
	    print OUT '        <a href="' . $name[2] .' "> <img src="' .
	    	 $name[3] . '"' . ' alt="' . $name[1] . '"'. "></a>\n";
#	    print OUT "        <br />\n";
	    print OUT "        <b>$name[1]</b>\n";
	    print OUT "        </td>\n";
	    $count = $count+1;
	}else {
		print "error:  unrecognized line: $_\n";
		next;
	}

   }
   if ($count=3) {print OUT "<td>&nbsp;</td>\n";}
   &print_end;
   close OUT;
   			# now print the bird index, three columns
   $menu_name = "birds_menu.html";
   @sorted = sort @birdlist;
   open OUT, ">$bird_index" or die "Can't open $bird_index for writing\n";
   &print_header1;
   print OUT "<title>Nancy's bird index</title>\n";
   &print_header3;
   print OUT '<h3 style="text-align:center;">', "Nancy's Bird Index</h3>", "\n";
   			# get the first letter of all the birds
			# to print the top internal links
   &print_alphabet;
   print OUT '<table>', "\n";
   			# first process the sorted birds & create html lines
			# we do this by taking the sorted birds and processing
			# all birds that start with the same letter at a time
			# At this time we also output the anchor for the
			# link that is defined in sub print_alphabet.
   $last = "";
   $num_birds = 0;
   			# fill index with blanks, handles problem when
			# I don't have 4 birds to print on one row
   &fill_array;
   foreach (@sorted) {
	@name=split/:/;
			# get first character of the birdname
	$char = substr ($_, 0, 1);
			# change of alphabet
	if ($char ne $last) {
			# see if we need to print a previous letter of birds
		if ($last) {
			&print_index;
			&fill_array;
			$num_birds = 0;
		}
		$upper = uc($char);
			# print letter of alphabet for internal link
		print OUT '<tr><td style="vertical-align:top;"><a name="', $upper,
			'"><b style="color:magenta;">', $upper, "</b></a> </td></tr>\n";
			# load array with the bird info
		$temp = '<td style="vertical-align:top;"> <a class="index" href="'. $name[1] . '">' . $name[0] . "</a> </td>\n";
		$inline[$num_birds] = $temp;
		$num_birds++;
		$last = $char;
	}else{
		$temp = '<td style="vertical-align:top;"> <a class="index" href="'. $name[1] . '">' . $name[0] . "</a> </td>\n";
		$inline[$num_birds] = $temp;
		$num_birds++;
	}

   }
   &print_index;
   print OUT "</table>\n";
   &print_index_end;
   close OUT;
}
sub print_index {
# prints out all the birds that begin with the same letter
			# first need to find how many birds I have
   			# Since I want the listing of birds to go down the column I need to 
			# print every fourth bird
   my $num = int($num_birds/4);
   if ($num_birds % 4) {$num ++;}
   for ($i = 0; $i < $num; ++$i) {
	print OUT "<tr>\n";
   	print OUT $inline[$i], $inline[$i+$num], $inline[$i+2*$num],
		$inline[$i+3*$num];

#   	print OUT $inline[$i], $inline[$i+1], $inline[$i+2], $inline[$i+3];

	print OUT "</tr>\n";
   }

}
sub print_bird_text{
	   	# If there is a "txt" file for the current header, open it and add the
		# contents to the web page.  Otherwise, print nothing.
   $file_txt = "../text_class/" . $head . ".txt";
   if (-f  $file_txt) {
	open TEXT, "<$file_txt" or die "Can't open $file_txt for reading\n";
	   	   while (<TEXT>) {
			s/<p>/<p style=\"font-size:small;\">/;
			print OUT $_;
	           }
	close TEXT;
    }
}
sub start_table {
	print OUT '        <table style="width:600;">';
	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 HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
HTML
}

sub print_header2 {
	print OUT <<HTML;

<link rel=stylesheet href="../HighSlide/high.css" type="text/css">
<link rel=stylesheet href="birds.css" type="text/css">

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-29073508-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>
<!-- 
The following lines are needed for each web page using the
	head and menu. -->
<body>
<!-- Include Header -->
<!--#include virtual="birds_header.html"-->
<table class="maintable">
<tr>
<td style="vertical-align:top;">
<!-- Include menu -->
<!--#include virtual="$menu_name"-->
</td>
<td width="20"></td>
<td style="vertical-align:top;">
<!-- End of needed lines -->

HTML
}

sub print_header3 {
# prints the header info for the index page which doesn't have the
# left menu
	print OUT <<HTML;

<link rel=stylesheet href="../HighSlide/high.css" type="text/css">
<link rel=stylesheet href="birds.css" type="text/css">
<style type="text/css">
   a {text-decoration:none;}
</style>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-29073508-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>
<!-- 
The following lines are needed for each web page using the
	head and menu. -->
<body>
<!-- Include Header -->
<!--#include virtual="birds_header.html"-->
<table class="maintable">
<tr>
<td width="40"></td>
<td style="vertical-align:top;">
<!-- End of needed lines -->

HTML
}

sub print_end {
	print OUT <<HTML;
	  </tr>
	</table>
	<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>
</body>
</html>

HTML
}
sub print_index_end {
	print OUT <<HTML;
<!-- index end -->
</td>
</tr>
</table>
</body>
</html>

HTML
}
sub print_alphabet {
# program to set up the internal links for the index and print them out
#@alpha = qw (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
   my $last = "";
   print OUT "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
   foreach (@sorted) {
	my $char = substr ($_, 0, 1);
	if ($char ne $last) {
		my $upper = uc($char);
	  	print OUT '<a href="#', $upper, '"><b style="color:magenta;">', $upper, "</b></a>&nbsp;&nbsp;\n";
	$last = $char;
	}
    }
#   foreach (@alpha) {
#  	print OUT '<a href="#', $_, '">', $_, "</a>&nbsp;&nbsp;&nbsp;\n";
#  }
#   print OUT "<br />\n";
}

sub fill_array {
	 my $len = $#inline;
	 for ($i = 0; $i <= $len; ++$i) {
		 $inline[$i] = " ";
	 }
}
