#!/usr/bin/perl
##/cygdrive/c/strawberry/perl/bin/perl -d
# Program to check the validity of the all the subsidery information
# on each bird.  Things checked are the files in the feet, tongue,
# text for each bird and the video directories.
# If a file is in one of these directories, there should be a
# corresponding entry in the in_class database.
#
{
$dir_feet = "feet/";		# directory holding feet images
$dir_tongue = "Tongue/";	# directory holding tongue images
$dir_text = "text/";		# directory holding the description text
$dir_video = "videos/";		# directory holding the videos
$root_dir = "/cygdrive/c/htdocs/birds/";
$class_data = "$root_dir/progs/in_class";# database for classification info
$video_raw = "/cygdrive/c/Users/Nancy/Pictures/Bird_movies/";

   print "\nMake sure all the files in the Tongue, feet, text, and \n",
   	 "videos have an entry in the in_class database.\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/) {
		@line = split /:/;
		$line[1] =~ s/ +//g;    # get rid of spaces
		$line[1] =~ s/'//g;	# get rid of apostrophes
		$birds{$line[1]} = 1;
	   }
		
    }
    close CLASS;
    		# Now get the files from the Tongue directory

    $tfile = $root_dir . $dir_tongue;
    @tongue = `ls $tfile`;
    foreach $t (@tongue) {
	    next if ($t =~ /^Original/);
	    next if ($t =~ /^ZbThumbnail/);
	    chomp ($t);
	    $t =~ s/\..*//;
	    if (! exists $birds{$t}) {
		    print "bird doesn't exist for Tongue file: $t\n";
	    }
    }
    		# Now check the feet directory
    $ffile = $root_dir . $dir_feet;
    @feet = `ls $ffile`;
    foreach $f (@feet) {
	    chomp ($f);
	    next if ($f =~ /^Originals/);
	    next if ($f =~ /^ZbThumbnail/);
	    next if ($f =~ /^backups/);
	    $f =~ s/\..*//;
	    if (! exists $birds{$f}) {
		    print "bird doesn't exist for feet file: $f\n";
	    }
    }
    		# Now check the text directory
    $ffile = $root_dir . $dir_text;
    @feet = `ls $ffile`;
    foreach $f (@feet) {
	    chomp ($f);
	    	# the next three are valid text files but they are
		# not birds in the in_class database
	    next if ($f =~ /Favorites/);
	    next if ($f =~ /JustforFun/);
	    next if ($f =~ /Lates/);
	    if ($f =~ /txt/) {
	    	$f =~ s/\.txt//;
	    	$f =~ s/\..*//;
	    	if (! exists $birds{$f}) {
		    print "bird doesn't exist for text file: $f\n";
		 }
	    }
    }

    		# Now check the video directory that we process the raw
		# footage.
    $ffile = $video_raw;
    @videos = `ls "$ffile"`;
    foreach $f (@videos) {
	    chomp ($f);
	    $name = $video_raw . $f;
	    if (-d $name) {
	    	if (! exists $birds{$f}) {
		    print "bird doesn't exist for video raw file: $f\n";
	    	}
	    }
    }

    		# Now check the video directory that we get the videos from
    $ffile = $root_dir . $dir_video;
    @videos = `ls $ffile`;
    foreach $f (@videos) {
	    chomp ($f);
	    next if ($f =~ /Thumbnail/);
	    $f =~ s/_.\.m.*//;		# get rid of end _1.m*
	    $f =~ s/\.m.*//;		# get rid of end .m*
	    if (! exists $birds{$f}) {
		    print "bird doesn't exist for text file: $f\n";
	    }
    }
}
