# -*-perl-*-

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

# Generate a description file from an existing system

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

=head1 NAME

What::Command::apply -- apply a configuration to a system

=head1 SYNOPSIS

what apply [--help] I<input-file...>

=head1 DESCRIPTION

Apply the listed what configuration files to either the current
system, or a tree if the B<what --root> option is given.

B<Fill in Later>

=cut

package What::Command::apply;

use strict;
use Getopt::Long;
use Sys::Hostname;

use base qw(What::Command);

our $helpmsg = "apply a configuration to a system or pseudo-root";

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

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

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

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

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

  if ($me) {
    my $hostname = hostname;
    # Set the target variable to be the hostname regardless of whether or
    # not there is a host-specific config file, so that the host-specific 
    # k5login file gets included even if the host uses the 
    # default configuration
    $section->settarget ($what, $hostname);
    $res = $section->include ($what, {}, -1, {}, $hostname);
    $res or $res = $section->include ($what, {}, -1, {}, 'default');
    if (!$res) {
      $what->err("Can't find valid config file for host $hostname!\n");
      exit 1;
    }
  } else {
    $res = $section->include ($what, {}, -1, {}, @ARGV);
  }

  $what->apply ();
}

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

1;
