#! /usr/bin/perl

#  altget - an snmpget(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: altget.in,v 1.6 1999/01/27 21:26:54 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.013;

$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.n[.n[...]] [ ... ]
                 -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
}

$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)
}

print STDERR "Constructing Altoids..." if $opt_v;
$alt = Altoids->new(\%altoids, @oidfiles);
print STDERR " done.\n" if $opt_v;

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

foreach (@ARGV) {
   if (!(($name, @iid) = split(m/\./, $_))) {
      print STDERR "bad argument format: \"$_\"\n";
      exit 1
   }

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

   print STDERR "Getting \"$name.", join('.', @iid), "\" (\"$var\")... " if $opt_v;

   %retblock = $alt->get($name, @iid);

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

   if (-1 == $retblock{-1}) {
      printf STDERR "get on \"$name.", join('.', @iid), "\" failed\n";
      next
   }

   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"
      }
   }
}
