#!@PERL@
#######################################################################
#
# Conserver LCFG Component
#
# @AUTHOR@
# Version @VERSION@ : @DATE@
#
# @MSG@
#
#######################################################################

@ENCODING@
package LCFG::Conserver;
@ISA = qw(LCFG::Component);

use strict;
use LCFG::Component;
use File::Copy;

##########################################################################
# Globals
##########################################################################

my $conservercf         = '/etc/conserver.cf';
my $conserverpasswd     = '/etc/conserver.passwd';
my $conserverconfdir    = '/var/lcfg/conf/conserver';
my $pidfile             = '/var/run/conserver.pid';
my $conserverinitscript = '/etc/rc.d/init.d/conserver';

##########################################################################
sub Configure() {
##########################################################################

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

  my $status = LCFG::Template::Substitute('@TEMPLATE@/conserver.cf.template',
                                          "$conserverconfdir/conserver.cf",
                                          0,
                                          $res); 
  unless (defined($status)) {
    $self->LogMessage($@);
    $self->Fail('failed to create config file conserver.cf (see logfile)');
  }

  $status = LCFG::Template::Substitute('@TEMPLATE@/conserver.passwd.template',
                                       "$conserverconfdir/conserver.passwd",
                                       0,
                                       $res);
  unless (defined($status)) {
    $self->LogMessage($@);
    $self->Fail('failed to create passwd file conserver.passwd (see logfile)');
  }

  copy("$conserverconfdir/conserver.cf",     $conservercf);
  copy("$conserverconfdir/conserver.passwd", $conserverpasswd);

  if ( -f $pidfile ) {
    open(PIDFILE, $pidfile) or $self->Fail("failed to open $pidfile: $?");
    while (<PIDFILE>) {
      chomp;
      kill 'HUP', $_;
    }
    close PIDFILE; 
  }

}

##########################################################################
sub Start() {
##########################################################################

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

  my $status = system($conserverinitscript, 'start');
  $self->Error("daemon was not started: $?") unless $status == 0;

}

##########################################################################
sub Stop() {
##########################################################################

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

  my $status = system($conserverinitscript, 'stop');
  $self->Error("daemon was not stopped: $?") unless $status == 0;

}

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

new LCFG::Conserver(@TESTPERLV@) -> Dispatch();
