#!/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);
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("$ENV{MIBHOME}/*");
$ENV{MIBDIRS} = join ':', @mibdirs;

# load mib_index2.txt mapping all current known MIBs
my ($vendor_for, $file_for, $mibs_for, $allmibs) = parse_index2();

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

foreach my $mib (sort @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 ',
      catfile($vendor_for->{$mib}, $file_for->{$mib}), "\n", RESET;
    print $check;
  }
}

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