# -*- perl -*-
# vim: syntax=perl

# $Id: allhosts.plm,v 1.8 2006/11/22 13:48:12 thomas Exp $

# Generate a description file from an existing system

# $Log: allhosts.plm,v $
# Revision 1.8  2006/11/22 13:48:12  thomas
# Properly create module options
#
# Revision 1.7  2005/05/18 18:01:17  thomas
# delete an allhosts host
#
# Revision 1.6  2005/04/11 14:40:05  thomas
# Sort list of modules for allhosts -l
#
# Revision 1.5  2005/02/21 20:56:31  thomas
# Help out the stinking pile known as help
#
# Revision 1.4  2004/11/10 17:22:44  thomas
# use %options instead of @options
#
# Revision 1.3  2004/10/13 16:02:30  thomas
# host names no longer -h options; now @ARGV
#
# Revision 1.2  2004/07/22 19:12:10  thomas
# use global help function
#
# Revision 1.1  2004/06/08 14:12:52  thomas
# Command-line interface to AllHosts
#

=head1 NAME

What::Command::allhosts - Read all host configs and do stuff

=head1 SYNOPSIS

what allhosts [B<-h|--help>]
S<[B<(--option|-o)> I<sub-module>:I<attribute>=I<value>...]>
S<[B<(--module|-m)> I<sub-module> ...]>
[I<host...>]

what allhosts [B<-h|--help>]
S<[B<(--option|-o)> I<sub-module>:I<attribute>=I<value>...]>
S<[B<(--module|-m)> I<sub-module> ...]>
S<[B<(--delete|-d)>]>
I<host...>

=head1 DESCRIPTION

Read B<What> configurations for a list of hosts, or all hosts known to
B<What>, and execute a list of B<AllHosts> modules, or all known
modules, for each host.

=head1 OPTIONS

=over 4

=item B<-o> I<sub-module>:I<attribute>=I<value> ...

Pass options to B<AllHosts> sub-modules.  This option can be given
multiple times.  However, a given sub-module and only receive a single
value for any given B<attribute>.

=item B<-m> I<sub-module> ...

Run only the given list of sub-modules.  Specify B<-m> once for each
module to be run.  If B<-m> is not specified, B<allhosts> will iterate
over all known sub-modules.

=item B<-h> I<host> ...

Run the requested sub-modules for only the specified list of hosts.
Specify B<-h> once for each host to be (re-)configured.  If B<-h> is
not specified, iterate over all hosts known to B<What>.

=item B<--delete|-d>

Delete allhosts data for the list of hosts.  Unlike the default usage,
this usage B<requires> a list of host names, and does not default to
all hosts known to the system (for obvious reasons).  fqdn checks will
be done so that short hostnames can be given on the command line.

=back

=head1 SEE ALSO

What::AllHosts
What::AllHosts::BuildDepDB,
What::AllHosts::StashedTickets

=cut

package What::Command::allhosts;

use strict;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;

use base qw(What::Command);
use What::AllHosts;

our $helpmsg = 'Read all host configs and do stuff';

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

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

  my ($res, $help) = (0, 0);
  my (%options, @modules, @hosts, $listmodules, $delete);

  Getopt::Long::Configure ("bundling");
  $res = GetOptions (
		     'help|h'        => \$help,
		     'option|o=s'    => \%options,
		     'module|m=s'    => \@modules,
		     'listmodules|l' => \$listmodules,
		     'delete|d'      => \$options{delete},
		    );

  @hosts = @ARGV;

  $res or return $what->err ("Bad option passed to allhosts command!\n");

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

  if ($listmodules) {
    @modules = What::AllHosts->getmodules;
    print "Modules known to allhosts:\n";
    foreach (sort @modules) {
      print "  $_ -- Try man -l /s/what-0.1.31/man/man3/What::AllHosts::$_.3\n";
    }
    return 0;
  }

  # Re-parse module options
  foreach (keys %options) {
    if ($_ =~ /(.*):(.*)/) {
      $options{$1}->{$2} = $options{$_};
    }
  }

  # Run the default usage.  --delete is parsed by AllHosts->do
  What::AllHosts->do ($what, {%options}, [@modules], [@hosts]);

  return 0;
}


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

1;

