#!/usr/bin/perl
# ------------------------------------------------------------------------------
#  Author: Serg Pososhenko
#  Edited by:
#  QA by:  Christopher C Gettings
#  Copyright: videoNEXT Network Solutions LLC
# ------------------------------------------------------------------------------
# sending HUP to emailer.php after new configuration stored

use strict;
use SKM::PM;
use XML::Simple;
use SKM::DB "DBLocal";
use Data::Dumper;

my $status = ReadStatus();
print "status=$status\n";

if ($status)
{
	# for emailer.php
	my $pid =`ps ax | grep emailer.php | grep -v grep | cut -c 1-6 | xargs kill`;
	if($pid != 0) {
		kill HUP => $pid; #`ps ax|grep emailer|grep -v grep|grep -v $$|cut -c 1-6`;
	}
	toggleProcess(1, 'emailer');

	# for event_emailer.php 
	my $pid =`ps ax | grep event_emailer.php | grep -v grep | cut -c 1-6 | xargs kill`;
	if($pid != 0) {
		kill HUP => $pid; #`ps ax|grep event_emailer|grep -v grep|grep -v $$|cut -c 1-6`;
	}
	toggleProcess(1, 'event_emailer');
}
else
{
	toggleProcess(0, 'emailer', 'event_emailer');
}

sub ReadStatus
{
	my $status = 0;
	my $dbl;
	eval {
		$dbl = DBLocal({PrintError => 0, RaiseError => 1});
		my $ra = $dbl->selectrow_arrayref("SELECT data FROM dyna_data WHERE typeid='Emailer'");
		my $xml = XMLin($ra->[0]);
		$status = $xml->{CONFIG}{STATUS} eq 'YES' ? 1 : 0;
	};
	if ($@) {
		die "$@\n";
	}
	eval { $dbl->disconnect } if $dbl;
	
	return $status;
}
