# -*-perl-*-

# $Id: generate.plm,v 1.4 2005/02/21 20:56:31 thomas Exp $

# Generate a description file from an existing system

# $Log: generate.plm,v $
# Revision 1.4  2005/02/21 20:56:31  thomas
# Help out the stinking pile known as help
#
# Revision 1.3  2004/07/22 19:12:10  thomas
# use global help function
#
# Revision 1.2  2003/12/15 16:28:34  thomas
# check-in for 0.1.1
#
# Revision 1.1.1.1  2003/12/02 14:47:34  thomas
# Initial version before export to 0.1.1
#

package What::Command::generate;

use strict;
use Getopt::Long;

use base qw(What::Command);

=head1 NAME

What::Command::generate - build configuration data file from live system

=head1 SYNOPSIS

what generate [--help] [--file <output-file>] <type> [<parmeters...>]

=head1 DESCRIPTION

This module calls the B<generate> function in the configuration module
B<type> to capture the current configuration and write it to file
F<output-file>, or F<STDOUT> if no B<--file> flag is given.

B<type> identifies the type of configuration object to generate (1 per
invocation).  The parameters depend on the type of configuration
object being generated.  See the B<generate> subcommand in the
L<What::Mod::B<type>> man page for the specific configuration
object.

=cut

our $helpmsg = "build configuration data file from live system";

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

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

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

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

  generate ($what, @_);

  $what->write ($file);
}

###############################################################################
# Generate a configuration for the requested object type from the running
# system.

sub generate {
  my ($what) = shift;
  my ($type) = shift;

  if (!defined $type) {
    $what->err ("generate: object type required but missing!\n");
    return -1;
  }

  my $section = $what->find_section ('default', 1);
  my $class = $section->find_node ($section, $type, 1);
  $class->generate ($section, $what, @_);

  return 1;
}

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

1;
