#! /usr/bin/perl

#  altwalk - an snmpwalk(1)-like utility that uses Altoids.pm
#  Copyright (C) 1997  Dave Plonka
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id: altwalk.in,v 1.16 1999/01/27 21:27:48 dplonka Exp $
# Dave Plonka <plonka@doit.wisc.edu>

use lib '/usr/local/lib/perl5/site_perl/5.10.0';

require "getopts.pl";

# try to do the altoids "hints" file...
do "altoids.pl";

use Altoids 1.007;

$script = $0;
$script =~ s:^.*/::;

if (!&Getopts("hd:Oo:Vvqn") || $opt_h || ($opt_o && $opt_O) || 2 > $#ARGV) {
   print STDERR <<_EOF_
usage: $script [ -hvnqO ] [ -d delim ] [ -o file.oid ] gateway community name
                 -h - help
                 -d delim - delimiter OID segments (defaults to '.')
		 -v - verbose
		 -q - quiet (surpresses non-fatal warnings)
		 -n - don't load /usr/local/share/altoids/*.oid
		 -O - slurp *.oid in current directory
                 -o file - load this ".oid" file
_EOF_
   ;
   exit 2
}

if ($opt_v) {
   require 'Benchmark.pm' && import Benchmark
}

$Altoids::verbose = $opt_V;
$Altoids::quiet = $opt_q;

###############################################################################
# populate a translation base with info from any SunNet Manager(?) OID files
# such as those produced by MIB2SCHEMA. (http://www.cisco.com/public/mibs/oid)

@oidfiles = $opt_n? () : </usr/local/share/altoids/*.oid>;

if ($opt_O) { # grab all the .oid files from the current directory...
   push(@oidfiles, <*.oid>)
} elsif ($opt_o) {
   push(@oidfiles, $opt_o)
}

my($t0, $t1);
if ($opt_v) {
   print STDERR "Constructing Altoids...";
   $t0 = new Benchmark
}
# $alt = Altoids->new({ sysORLastChange => $sysORLastChange }, @oidfiles);
$alt = Altoids->new(\%altoids, @oidfiles);
if ($opt_v) {
   $t1 = new Benchmark;
   print STDERR " ", timestr(timediff($t1, $t0)), "... done.\n"
}

$alt->open(shift(@ARGV), shift(@ARGV), 161);

foreach $name (@ARGV) {
   if (!($name =~ m/^(\d+\.)*\d+$/) && '' eq $alt->oid($name)) {
      warn "Don't know much about \"$name\".\n";
      next
   }

   my $var = $alt->oid($name);
   if ('' eq $var) {
      print STDERR "Could not translate \"$name\" to OID\n" if $MAIN::opt_v;
      next
   }
   
   print STDERR "Walking \"$name\" (\"$var\")... " if $opt_v;

   my %retblock = $alt->walk($name);

   print STDERR "done.\n" if $opt_v;

   die if ($retblock{-1} == -1);

   hashprint('', %retblock);
}

$alt->close();

exit;

###############################################################################

sub hashprint {
   my $prefix  = shift @_;
   my %hash  = @_;
   my ($val, $key);
   my $delim = ($opt_d? $opt_d : '.');

   while (($key, $val) = each(%hash)) {
      if ('HASH' eq ref($val)) {
         hashprint($prefix? $prefix . $delim . $key : $key, %{$val})
      } else {
         print $prefix, $delim, "$key = $val\n"
      }
   }
}
