package RackMan::Format::PXE;

use strict;
use Carp;
use RackMan::File;
use RackMan::Template;

use constant {
    CONFIG_SECTION  => "format:pxe",
    DEFAULT_PATH    => "/etc/pxe/hosts",
    TEMPLATE_PATH   => "/etc/pxe/host.pxe.conf.tmpl",

# write()
# -----
sub write {
    my ($class, $args) = @_;

    my $rackdev = $args->{rackdev};
    my $rackman = $args->{rackman};
    my $config  = RackMan::File->new;

    # fetch the list of regular MAC addresses
    my @mac_addrs = $rackdev->regular_mac_addrs;

    # load and populate the template
    my $tmpl_path = $rackman->options->{pxe_template}
        || $rackman->config->val(CONFIG_SECTION, "template", TEMPLATE_PATH);
    print "  = reading $tmpl_path\n" if $args->{verbose};
    my $tmpl = RackMan::Template->new(filename => $tmpl_path);
    $tmpl->populate_from($rackdev, $rackman);

    # generate the config

    # write the configuration on disk
    (my $config_name = lc "01-$mac_addrs[0]{l2address_text}") =~ s/:/-/g;
    $config->path($rackman->config->val(CONFIG_SECTION, "path", DEFAULT_PATH));
    print "  + writing ", $config->fullpath, $/ if $args->{verbose};
    my $scm = $rackman->get_scm({ path => $config->path });
    $scm->commit($config->name, "generated by $class / $::PROGRAM v$::VERSION");




=head1 NAME

RackMan::Format::PXE - Generate the PXE config for a given RackObject


    use RackMan::Format::PXE;

        rackdev => $rackdev,  # a RackMan::Device instance
        rackman => $rackman,  # a RackMan instance


This module generates the PXE configuration file for a given RackObject,
based on a template provided by the user.

=head1 METHODS

=head2 write

Generate the file.


Arguments are expected as a hashref with the following keys:


=item *

C<rackdev> - I<(mandatory)> a RackMan::Device instance

=item *

C<rackman> - I<(mandatory)> a RackMan instance

=item *

C<verbose> - I<(optional)> boolean, set to true to be verbose



See L<RackMan::Template/"TEMPLATE PARAMETERS"> for more details about
the available parameters.


This module gets its configuration from the C<[format:dhcp]> section
of the main F<rack.conf>, with the following parameters:


=item *

C<path> - specify the location to store the generated files

=item *

C<template> - specify the path of the template; can be overriden by
the C<--pxe-template> option


=head1 SEE ALSO


=head1 AUTHOR

Sebastien Aperghis-Tramoni