#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2005 Mitel Networks Corporation
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 		
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 		
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
#----------------------------------------------------------------------
package esmith;

use strict;
use Errno;
use DirHandle;

my $event = shift || die "must give event name parameter";
chdir "/etc/e-smith/events/$event" or die "Couldn't chdir to event directory /etc/e-smith/events/$event: $!";
my $dh = DirHandle->new("services2adjust");

exit(0) unless $dh; # Nothing to do

use esmith::ConfigDB;
use esmith::util;

my %param2char = (
	down => 'd',
	stop => 'd',
	up => 'u',
	start => 'u',
	restart => 't',
	sigterm => 't',
	adjust => 'h',
	reload => 'h',
	sighup => 'h',
	sigusr1 => '1',
	sigusr2 => '2',
	once => 'o',
	pause => 'p',
	alarm => 'a',
	interrupt => 'i',
	quit => 'q',
	kill => 'k',
	exit => 'x',
    );

sub adjust_supervised_service
{
    my ($s, @actions) = @_;
    my $m = "control fifo for service $s: ";
    unless (open(C, ">/service/$s/supervise/control"))
    {
	warn "Couldn't open $m$!";
	return;
    }
    foreach my $p (@actions)
    {
	my $c = $param2char{$p};
	unless ($c)
	{
	    warn "Unrecognised param $p for service $s\n";
	    next;
	}
	warn "adjusting supervised $s ($p)\n";
	unless (print C $c)
	{
	    warn "Couldn't write to $m$!";
	    return;
	}
    }
    warn  "Couldn't close $m$!" unless close(C);
}

my $conf = esmith::ConfigDB->open_ro || die "Couldn't open config db";

foreach my $service (grep { !/^\./ } $dh->read())
{
    my $s = $conf->get($service);
    unless ($s)
    {
	warn "No conf db entry for service $service\n";
	next;
    }
    my $f =  "services2adjust/$service";
    
    my @actions;
    if (-l "$f")
    {
	@actions = ( readlink "$f" );
    }
    else
    {
	if (open(F, $f))
	{
	    # Read list of actions from the file, and untaint
	    @actions = map { chomp; /([a-z]+[12]?)/ ; $1 } <F>;
	    close(F);
	}
	else
	{
	    warn "Could not open $f: $!";
	}
    }

    # if service is supervised and not handled by systemd
    if (-d "/service/$service" && glob("/etc/rc7.d/S??$service"))
    {
	my $enabled = ($s->prop('status') || 'disabled') eq 'enabled';
	adjust_supervised_service($service,
		# stop the service if it is now disabled
		$enabled ? () : 'down',
		# Send the specified signal(s) to the running daemon
		@actions,
		# bring the service up if it is enabled (and we're not
		# stopping it or running it once)
		($enabled && !grep { /^(down|stop|d|once|o)$/ } @actions) ? 'up' : (),
	    );
    }
    # for service handled by former sysvinit or directly with systemd
    else
    {
        my $enabled = ($s->prop('status') || 'disabled') eq 'enabled';
        # bring the service up if it is enabled (and we're not stopping it or running it once, or using signal able to start it)
        unshift(@actions,'start') if ($enabled && !grep { /^(down|stop|d|once|o|start|restart|reload-or-restart)$/ } @actions) ;
	# stop the service if it is disabled
	@actions = ('stop') unless  $enabled;
	foreach (@actions)
	{
	    warn "adjusting non-supervised $service ($_)\n";
	    esmith::util::serviceControl(
		    NAME => $service,
		    ACTION => $_,
		);
	}
    }
}

