# -*-perl-*-

# $Id: dumpdata.plm,v 1.1 2005/04/26 14:33:37 thomas Exp $

# $Log: dumpdata.plm,v $
# Revision 1.1  2005/04/26 14:33:37  thomas
# Dump the /Data cache to stdout for host(s).
#

=head1 NAME

What::Command::dumpdata - dump the current data cache

=head1 SYNOPSIS

what dumpdata [--help] I<hostname...>

=head1 DESCRIPTION

Dump the current version of the /Data subtree cache to stdout for each
host listed on the command line.  Perform a I<gethostbyname()> call on
each host before performing the database lookup.

=cut

package What::Command::dumpdata;

use strict;
use Getopt::Long;
use Sys::Hostname;
use Data::Dumper;
use Net::hostent;
use What::Data;

use base qw(What::Command);

our $helpmsg = "dump the current version of the /Data subtree cache.";

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

sub main {
  my $self = shift;
  my $what = shift;
  local (@ARGV) = @_;

  my ($res, $help) = (0, 0);
  Getopt::Long::Configure ("bundling");
  if (!($res = GetOptions (
                           'help|h',   \$help,
                          ))) {
    $what->err ("Bad option passed to apply command!\n");
    return 0;
  }

  $help and return $self->help();

  my ($Data, $host, $data, $h);
  if (!defined ($Data = new What::Data)) {
    $what->err ("Failed to access /Data cache!\n");
    return 0;
  }

  foreach $host (@ARGV) {
    # Try the host as passed
    if (defined ($data = $Data->find ($host))) {
      print "Data for $host:\n";
      print Data::Dumper->Dump ([$data], [qw(Data)]);
    } else {
      if (!defined ($h = gethostbyname ($host))) {
	$what->err ("Failed host lookup on $host!\n");
	next;
      }
      if (defined ($data = $Data->find ($h->name))) {
      print "Data for " . $h->name . ":\n";
      print Data::Dumper->Dump ([$data], [qw(Data)]);
      } else {
	$what->err ("No data found for $host.\n");
      }
    }
  }
  return 0;
}

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

1;

