#!@PERL@
#######################################################################
#
# Example LCFG Component in Perl
#
# @LCFG_AUTHOR@
# Version @LCFG_VERSION@ : @LCFG_DATE@
#
#@MSG@
#
#######################################################################

package LCFG::Component::PerlEx;
use strict;
use warnings;

use base qw(LCFG::Component);

use IPC::Run ();
use LCFG::Template qw($PERLTMPL);

##########################################################################
sub Configure {
##########################################################################

    my ( $self, $res, @args ) = @_;

########################################################################
# Recreate a configuration file when we get a reconfigure
# call.
########################################################################

    my $template   = $res->{template}{VALUE};
    my $configfile = $res->{configfile}{VALUE};

    my $status = LCFG::Template::Substitute
        ( $template, $configfile, $PERLTMPL, $res );

    if ( ! defined $status ) {
        $self->LogMessage($@);
        $self->Fail( "failed to create config file (see logfile)");
    }

    if ( $status == 1 ) {
        $self->LogMessage("configuration changed");
    }

    # At this point, we should check if the daemon is running, and
    # if so notify it of any changes (if necessary)

    return;
}

##########################################################################
sub Start {
##########################################################################

    my ( $self, $res, @args ) = @_;

########################################################################
# If you want to run an external daemon program, you should start it
# here. If you don't have a daemon, you don't need a Start() routine
# at all.
########################################################################

    return $self->call_initd_script( $res, 'start' );
}

##########################################################################
sub Stop {
##########################################################################

    my ( $self, $res, @args ) = @_;

########################################################################
# If you want to run an external daemon program, you should stop it
# here.  If you don't have a daemon, you don't need a Stop() routine
# at all.
########################################################################

    return $self->call_initd_script( $res, 'stop' );
}

##########################################################################
# Internal helper functions
##########################################################################

sub call_initd_script {
    my ( $self, $res, $method ) = @_;

    my $tool = $res->{tool_init}{VALUE};

    if ( !defined $tool || $tool =~ m/^\s*$/ ) {
      $self->Fail("No tool defined for managing the stop/start for daemon");
    }

    if ( $self->{_DEBUG} ) {
        $self->Debug("$tool $method called");
    }

    my ( $in, $out, $err );
    my $success = IPC::Run::run [ $tool, $method ], \$in, \$out, \$err;
    if ($out) {
        $self->LogMessage($out);
    }
    if ($err) {
        $self->LogMessage($err);
    }
    if ( !$success ) {
        $self->Fail("$tool $method failed (see logfile)");
    }

    return;
}

##########################################################################
# Dispatch methods
##########################################################################

LCFG::Component::PerlEx->new()->Dispatch();
