# -*-perl-*-

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

# Generate a description file from an existing system

# $Log: merge.plm,v $
# Revision 1.6  2005/02/21 20:56:31  thomas
# Help out the stinking pile known as help
#
# Revision 1.5  2004/07/22 19:12:10  thomas
# use global help function
#
# Revision 1.4  2004/03/17 21:19:21  thomas
# Support variables in includes
#
# Revision 1.3  2004/03/05 15:13:16  thomas
# cconf->what
#
# Revision 1.2  2004/02/10 16:40:11  thomas
# pre-0.1.5 fork
#
# Revision 1.1.1.1  2003/12/02 14:47:34  thomas
# Initial version before export to 0.1.1
#

=head1 NAME

What::Command::merge - merge a set of what files and print results

=head1 SYNOPSIS

what merge [--help] [--file|-f I<output-file>] I<input-file...>

=head1 DESCRIPTION

Merge a bunch of B<what> objects.

B<Fill in Later>

=cut

package What::Command::merge;

use strict;
use Getopt::Long;
use Sys::Hostname;
use base qw(What::Command);

our $helpmsg = "merge a set of what files and print results";

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

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

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

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

  my $default_section = $what->find_section ('default', 1);

  if ($me) {
    my $hostname = hostname;
    $res = $default_section->include ($what, {}, -1, {}, $hostname);
    $res or $res = $default_section->include ($what, {}, -1, {}, 'default');
    if (!$res) {
      $what->err("Can't find valid config file for host $hostname!\n");
      exit 1;
    }
  } else {
    $default_section->include ($what, {}, -1, {}, @ARGV);
  }

  $what->write ($file);
}

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

1;
