#! /usr/bin/perl -w
use strict;
use Getopt::Std;

my %options = ();

getopts ("dc",\%options);

my $SYMBOL_BASE = '/mnt/socorro/symbols';

my @SYMBOL_INDEX_FILES = `find $SYMBOL_BASE -maxdepth 2 -type f -name \\*.txt | grep -v symbols_os`;

my ($index_file, $symbol_dir, @symbols, @symbol_paths, @missing_symbols);

my $EXIT_STATUS = 0;

foreach $index_file (@SYMBOL_INDEX_FILES) {
    open (INDEX_FILE, $index_file) or
      die "Could not open $index_file: $!\n";
    {
        local $\;
        @symbols = <INDEX_FILE>;
        close (INDEX_FILE);
    }
    $symbol_dir = $index_file;
    $symbol_dir =~ s:(.*/)[^\/]*$:$1:;
    @symbol_paths = map {$symbol_dir . $_} @symbols;
    chomp $index_file;
    $index_file =~ s:.*/::;
    foreach (@symbol_paths) {
        chomp;
        $_ =~ s///g;
        $_ =~ s:\\:/:g;
        if ( ! -r ) {
            $_ =~ s:.*/::;
            if (defined $options{c}) {
                push @missing_symbols, "Symbol $_ from $index_file is not readable.\n";
                print "Symbol $_ from $index_file is not readable.\n" if defined $options{d};
                $EXIT_STATUS = 2;
            } else {
                print "Symbol $_ from $index_file is not readable.\n";
                exit 2;
            }
        }
    }
}

if ($EXIT_STATUS) {
    print "Missing symbols found.\n";
    print @missing_symbols;
} else {
    print "All referenced symbols present.\n";
}

exit $EXIT_STATUS;
