#!/cygdrive/c/strawberry/perl/bin/perl
# A Program to check and count a variety of things.  The program
# mostly deals with in_class and the text/tongue/feet text files.
# In other words, this program will count the number of birds and
# places.  See which ones have text for description, feet, and tongues.
# It will write out to a file the ones who don't have the descriptions.
# For places, it will also tell which ones have google maps.
#
$text_dir = "../text/";		# dir for bird/place descriptions
$desc_dir = "../Descriptions/";	# dir for complete bird descriptions
$place_dir = "../text_place/";	# dir for bird/place descriptions
$feet_dir = "../feet/";		# dir for bird feet
$tongue_dir = "../Tongue/";	# dir for bird tongues
$pic_dir = "/cygdrive/c/users/nancy/Pictures/Bird_movies/";  # dir for movies
$vid_dir = "/cygdrive/c/htdocs/birds/videos/";  	     # dir for finished videos
$fileout = "descript_out";	# output written to this file

$class_data = "in_class";	# database for classification info
$inv_data = "inventory.txt";	# database for classification info
$text_sum = 0;			# I sum the number of birds with text
$desc_sum = 0;			# I sum the number of birds with complete descriptions
@birds;				# Stores the bird name and the classification
				#   from in_class
%video_dir;			# hash table contains birds with video subdirectories
%video_vsp;			# hash table contains birds with .VSP files (corel)
%video_mp;			# hash table contains birds with mpeg movies in web dir
%video_redo;			# hash table contains birds that needs to be updated
				#    (.mov or .mpg more recent that .vsp file).
%dates_b;			# Keeps track if there are
				#   breeding/non-breeding dates for each bird
%breeds;			# Keeps track if there are
				#   breeding/non-breeding dates for each bird
$blnk = " ";
$head1 = '"\n%35s%6s%5s%5s%5s%6s%2s%5s%5s",' .  
 	 '"# of", "Short", " Full", $blnk, $blnk, $blnk, "|", $blnk, "Videos\n"';
$head2 = '"%35s%6s%5s%5s%5s%6s%2s%5s%5s%5s%5s\n",' .
   	 '"Pics", "Desc.", "Desc.", "Feet", "Tong", "Breed", "|", "Have", "VSP", "Web", "Redo\n"';
{

   print "Counts the number of birds and places visited and does a variety of checks.\n",
   	 "For birds, check if a description exists, if there are videos, pictures of\n",
	 "the feet and tongue.  For places, check if description and a google map.\n\n";
	 		# read in the classification database and creates an
			# array of the birds and the places (each in a separate hash)
   open CLASS, $class_data or die "Cannot open the file: $class_data\n";
   while ( <CLASS> ) {
	   s/^ *//;
	   @array = split/:/;
	   if (/^title/) {
		   chomp($array[1]);
		   $temp = $array[1];
	   }
	   if (/^bird/) {
		   push (@birds, $temp . ":" . $array[1]); 
		   if ($array[4] || $array[5]) {
			$t = $array[1];
			$t =~ s/ +//g;		# get rid of spaces
			$t =~ s/'//g;		# get rid of apostephes
			$dates_b{$t} = 1;   
		   }
	   }

	   if (/place/) {
		   push (@places, $temp . ":" . $array[1]); 
	   }
    }
    close CLASS;
    			# open the inventory database
   open INV, $inv_data or die "Cannot open the file: $inv_data\n";
   while ( <INV> ) {
			# read in the inventory database and create a hash table counting
			# the number of pictures for each bird and each place separately.
	s/^\s+//;
	@name = split /\t/;
	$num = $#name;
			# skip if a zero'd line (excel did this) 
	next if ($num <= 0);
			# skip comment lines
	next if ($name[0] =~ /#/);
			# get rid of any extra spaces at the beginning or end of
			# the bird file name
	$name[4] =~ s/ +//g;		# get rid of spaces
	$name[4] =~ s/'+//g;		# get rid of quotes
	$birds{$name[4]} = $birds{$name[4]}+1;
			# store info if the bird  has breeding status
	if ($name[2] == 2 || $name[2] == 3) {
		$breeds{$name[4]} = 1;
	}
     }
     close INV;
			# create the hash tables for the video information.
			# Each bird has its own subdirectory in $pic_dir.  In the
			# subdirectory there are the original movie/picture files
			# and a <bird>.VSP file if I have processed the original
			# files with Corel.  If there are original movie files
			# (.MOV or .MPG) that have a earlier date than the .VSP
			# file, it means I need to redo the movie(s) for this bird.

			# find all the bird video subdirectories
    @temp = `ls -F $pic_dir | fgrep "/"`;
    while ($line = pop(@temp)) {
	chomp ($line);		# get rid of carriage return
	$line =~ s,/,,;		# get rid of ending /
	$video_dir{$line} = $line; 
	$video_files = $pic_dir . $line;
    			# check out the files in the bird subdirectories looking for
			# .VSP, .mov and .mpg files
	@temp1 = `ls -t $video_files`;
	@temp2 = @temp1;
	my $vflag = 0;
	while ($vfile = shift(@temp1)) {
		if ($vfile =~ /\.VSP/) {
			$video_vsp{$line} = 1;
				# if this is true, than an original movie file has an
				# earlier date than Corel's file and I will need
				# to update the edit list.
			if ($vflag) {
				$video_redo{$line} = 1;
			}
		}elsif (($vfile =~ /\.MOV/ || $vfile=~ /\.MPG/)) {
			$vflag = 1;
		}
	}
    }
    			# check out the files in the video subdirectory which is where
			# my web pages look for movies
    @temp = `ls $vid_dir`;
    while ($line = pop(@temp)) {
	chomp ($line);		# get rid of carriage return
	next if ($line =~ /Thumb/);
				# get bird name
	$birdname = $line;
	$birdname =~ s/_.\..*//;
	$video_mov{$birdname} = $video_mov{$birdname}+1;
    }

    			# sort the birds and places
    @birds_s = sort {$b cmp $a} @birds;
    @places_s = sort {$b cmp $a} @places;
			# get the sum of each and print it
    $sum_b = @birds_s;
    $sum_p = @places_s;
			# open file to write all the information to (except for
			# standard out)
    open OUT, ">$fileout" or die "Can't open $fileout for writing\n";

    print "Number of birds:  $sum_b\n";
    print OUT "Number of birds:  $sum_b\n";
    printf OUT "\n%35s%6s%5s%5s%5s%6s%2s%5s%5s", 
 	 "# of", "Short", "Full", $blnk, $blnk, $blnk, "|", $blnk, "Videos\n";
    printf OUT "%35s%6s%5s%5s%5s%6s%2s%5s%5s%5s%6s\n",
   	 "Pics", "Desc.", "Desc.", "Feet", "Tong", "Breed", "|", "Have", "VSP", "Web", "Redo\n";
    $temp = 0;
    $totalp = 0;
    while ($line = pop (@birds_s)) {
	$sum_text = "No";
	$sum_desc = "No";
	$sum_feet = "No";
	$sum_tong = "No";
	$sum_vido = "No";
	$breeder = "-";
	@array = split/:/, $line;
	$bird = $array[1];
	$bird =~ s/^ *//;
	if ($temp ne $array[0]) {
		$array =~ s/^ *//;
		print OUT "\n----", $array[0], "\n";
		$temp = $array[0];
	}
	    		# get rid of all spaces, and quotes to get the filename
	$array[1] =~ s/ *//g;
	$array[1] =~ s/'//g;
			# check for text or partial description
	$filename = $text_dir . $array[1] . ".txt";
	if  (-e $filename) { 
		$sum_text = "yes"; 
		$text_sum++;
	}
			# check for complete bird description
	$filename = $desc_dir . $array[1] . ".txt";
	if  (-e $filename) { 
		$sum_desc = "yes"; 
		$desc_sum++;
	}
			# check for feet pictures
	$filename = $feet_dir . $array[1] . ".jpg";
	if  (-e $filename) { $sum_feet = "yes"; }
			# check for tongue pictures
	$filename = $tongue_dir . $array[1] . ".jpg";
	if  (-e $filename) { $sum_tong = "yes"; }
	$totalp = $totalp + $birds{$array[1]};
			# check for breeding status & if I have added the dates to in_class
	$bb = $array[1];
	if ((exists $breeds{$array[1]}) && (exists $dates_b{$array[1]})) {
		$breeder = "OK";
	}elsif ((exists $breeds{$array[1]}) && !(exists $dates_b{$array[1]})) {
		$breeder = "Needs";
	}
			# check for a video directory.
	$vdflag = "-";		# flag for video directory for a bird
	$vspflag = "-";		# flag for Corel movie file
	$redoflag = "-";	# flag that means I have newer raw videos
	$vid_mov = "-";		# quicktime movies in web directory
	if (exists $video_dir{$array[1]}) {$vdflag = "yes";}
	if (exists $video_vsp{$array[1]}) {$vspflag = "yes";}
	if (exists $video_mov{$array[1]}) {$vid_mov = "yes";}
	if (exists $video_redo{$array[1]}) {$redoflag = "yes";}

	printf OUT ("%-29s%6d%5s%5s%5s%5s%7s%6s%5s%5s%6s\n", $bird,
		$birds{$array[1]}, $sum_text, $sum_desc, $sum_feet, $sum_tong,
		$breeder, $vdflag, $vspflag, $vid_mov, $redoflag);
    }
    print "Number of inventory pictures of birds: $totalp\n",
    		"Number of birds with text: $text_sum\n",
    		"Number of birds with complete descriptions: $desc_sum\n";
    print OUT "Number of inventory pictures of birds: $totalp\n",
    		"Number of birds with text: $text_sum\n",
    		"Number of birds with complete descriptions: $desc_sum\n";
    			# process the places visited, checking for txt and google maps
    print "Number of places:  $sum_p\n";
    print OUT "Number of places:  $sum_p\n";
    printf OUT "%40s%5s\n", "Desc", "GMap";
    $temp = 0;
    while ($line = pop (@places_s)) {
	$sum_text = "No";
	$sum_goog = "No";
	$sum_vido = "No";
	@array = split/:/, $line;
	$place = $array[1];
	$place =~ s/^ *//;
	if ($temp ne $array[0]) {
		$array =~ s/^ *//;
		print OUT "\n----", $array[0], "\n";
		$temp = $array[0];
	}
	    		# get rid of all spaces, and quotes to get the filename
	$array[1] =~ s/ *//g;
	$array[1] =~ s/'//g;
	$filename = $place_dir . $array[1] . ".txt";
			# check for description
	if  (-e $filename) { $sum_desc = "yes"; 
			# check for google maps in description
		$out = `fgrep iframe $filename`;
		if ($out) {$sum_goog = "yes";}
	}
	printf OUT ("%-35s%5s%5s\n", $place, $sum_desc, $sum_goog);
    }
}
