#!/usr/bin/perl
##/cygdrive/c/strawberry/perl/bin/perl -d
# Program to check the validity of the bird database.  Three things are
# checked:
# First:  make sure that each entry in the database has three pictures:
# 	  <date>/Image, <date>/Image_sm, and <date>Image/Full_res
# 	  If not, the anomolies are printed.
# Second: Make sure that the bird listed in the inventory database has
# 	  an entry in the classification database (in_class)
# Third:  Make sure that the birds breeding/non-breeding status is reflected
# 	  in the inventory database.
#
# The corresponding program for checking the validity of my data is
# check_file. check_file makes sure all files found in <date> have
# an entry in the inventory database.
#
# Original line from excel
#   date priority breeding filename bird_name bird_category picture_location notes
#
#$root_dir = "../";		# root dir for finding birds images
#$root_dir = "c:/htdocs/birds/";		# root dir for finding birds images
{
$dir_thumb = "Images_sm/";		# directory holding thumbnails
$dir_image = "Images/";			# directory for full displayed images
$dir_orig  = "Images/Full_res/";	# directory for original full images
$root_dir = "/cygdrive/c/htdocs/birds/";
$class_data = "$root_dir/progs/in_class";# database for classification info

   print "Checking the validity of the inventory database. Make sure that\n",
   	 "  each entry in the database has three files (Thumbnail, regular,\n",
	 "  and full-resolution. Also, make sure that each bird in the, \n",
	 "  inventory has been added to the classification database.\n",
	 "  And finally make sure if the bird has breeding status, i.e.,\n",
	 "  the adult bird is a 2 or 3 and not 4.\n\n ";
	 		# read in the classification database creating a hash
			# table to easily check the bird inventory entries
			# against
   open CLASS, $class_data or die "Cannot open the file: $class_data\n";
   while ( <CLASS> ) {
	   next if /^#/;
	   if (/bird/ || /place/) {
		@line = split /:/;
		$line[1] =~ s/^ +//;
		$line[1] =~ s/ +$//;
		$birds{$line[1]} = 1;
			# now create the hash table for breeding status
			# That means something in $lin[4]
		if ($line[4]) {
			$breeding{$line[1]} = 1;
		}
			# check to make sure that the entry in in_class
			# points to a valid picture
			# first get rid of any quotes excel might add
			# to the date field and any spaces
		$file_c = $line[3];
		$file_c =~ s/\"//g;
		$file_c =~ s/\s+//g;
		chomp($file_c);
		$file_c = "../" . $file_c;
		if (!(-e $file_c)) {
			$file_c = "in_class Thumbnail not found " .
				$file_c . " for bird $line[1]\n";
			push (@in_class, $file_c);
		}	
		
	   }
    }
    close CLASS;

   while ( <STDIN> ) {
	   		# get rid of any beginning spaces or # (comments)
	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] =~ /#/);
			#  ***  first check to see if the images are there ***
			# get the year and month for the bird directory
	$dir = $root_dir . substr($name[0],0,4) . "/" . substr($name[0],4,2) . "/";
			# get rid of any extra spaces at the beginning or end of
			# the bird image filename
	$name[3] =~ s/^ +//;		# get rid of any beginning spaces
	$name[3] =~ s/ +$//;		# get rid of any ending spaces
			# main part  - check for files
	$file_thumb =   $dir . $dir_thumb . $name[3] . ".jpg";
	$file_image =   $dir . $dir_image . $name[3] . ".jpg";
	$file_orig =    $dir . $dir_orig . $name[3] . ".JPG";
	if (! (-e  $file_thumb)) {
		print "thumbnail images doesn't exist $file_thumb\n";
	}
	if (! (-e  $file_image)) {
		print "displayed image doesn't exist $file_image\n";
	}
	if (! (-e  $file_orig)) {
		print "orig images doesn't exist $file_orig\n";
	}
			# make sure the age/breeding/adult field is
			# correct, 1-4.
			# check to make sure bird has the correct breeding
			# status. If a 2 or 3 than it must have the breeding
			# dates in in-class.  If a 4 than no breeding dates.
			# 1 is ignored because it is a juvenile
	if ($name[2] < 0 || $name[2] > 4) {
		print "Following line doesn't have a 1, 2, 3, or 4 in the", 
			" breeding fields\n    ", @name;
	}elsif ($name[2] == 2 || $name[2] == 3) {
		if (! (exists ($breeding{$name[4]}))) {
			print "Bird doesn't have the correct breed/non-breed",
		       		" status\n  ", @name;
		}
	}
			# *** check to see if there is a entry for this bird
			#     in the classification database ***
	$name[4] =~ s/^ +//;		# get rid of any beginning spaces in birdname
	$name[4] =~ s/ +$//;		# get rid of any ending spaces in birdname
	$name[4] =~ s/\"//g;		# for some reason excel puts " on some fields
	next if ($name[4] =~ /Just for Fun/);	# Don't have class for these misc. animals
	next if ($name[4] =~ /Unknown/);	# Don't have class for these unknown birds
	if (! (exists ($birds{$name[4]}))) {
		print "Bird: $name[4] - is not in $class_data database.\n";
		print "    file name is: $name[5]\n";
		print "    @name\n";
	}
   }
   			# print any file not found in in_class

   if (@in_class) {
   	print @in_class;
   }

}
