#!/usr/bin/env perl
# testload [vendor] - use snmptranslate to boot MIBs individually
# 5:23 for all mibs on 2013 Macbook Air

use strict;
use warnings;

use charnames ':full';
binmode STDOUT, ':utf8';

use File::Spec::Functions qw(splitdir catfile catdir);
use Term::ANSIColor qw(:constants);

use FindBin;
use lib catfile($FindBin::Bin, 'lib');
use Helpers;

# TODO: arguably rfc:net-snmp should be enough!
# but it seems cisco, nortel, etc are required. maybe in the future, fix this.
my @mibdirs = grep { -d and $_ !~ m#/EXTRAS$# } glob( catdir($ENV{MIBHOME},'*') );
$ENV{MIBDIRS} = join ':', @mibdirs;

# index the MIBs in MIBHOME
print "\N{EYES} Building MIBs index\n";
my ($mib_for_file, $mib_files, $vendor_mibs, $mib_vendors) = mkindex();

my $vendor = shift;
if ($vendor and !exists $vendor_mibs->{$vendor}) {
  print "error: vendor arg must exist in MIBHOME\n";
  exit(1);
}
my @mibs = (defined $vendor) ? @{$vendor_mibs->{$vendor}} : (sort keys %$mib_files);

foreach my $mib (@mibs) {
  status("Testing $mib");
  my $check = qx(snmptranslate -Le -m '$mib' bork 2>&1);
  # kill last line (our bogus error)
  $check =~ s/^.*\Z//m;
  if ($check !~ /^\s*$/){
    blank();
    print RED, "\N{HEAVY BALLOT X} ", CYAN, 'Errors from ', MAGENTA,
      $mib, RESET, FAINT, ' in ',
      (join ',', @{ $mib_files->{$mib} }), "\n", RESET;
    print $check;
  }
}

blank();
print "\N{BLACK FLAG} Checks done.\n";
exit(0);
