#!/usr/bin/perl

#----------------------------------------------------------------------
# copyright (C) 2002 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
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::ConfigDB;
use esmith::util;

my $conf = esmith::ConfigDB->open || die "Could not open config DB\n";
my $mysql = $conf->get('mysql');
exit 0 unless defined $mysql;

my $status = $mysql->prop("status") || "disabled";
exit 0 unless $status eq "enabled";

my $pidfile = "/var/run/mysqld/mysqld.pid";

if (-f $pidfile)
{
    open(PIDFILE,$pidfile) or die("OPEN ERROR $pidfile $!");
    my $pid = <PIDFILE>;
    close(PIDFILE) or die("CLOSE ERROR $pidfile $!");
    chomp($pid);

    if ($pid)
    {
	exit 0 if kill(0, $pid); # mysqld is running
	warn("Pid file found but mysqld not running\n");
    }
    else
    {
	warn("Empty mysqld pid file found\n");
    }
}

foreach my $process (qw(mysqld mysql.init))
{
    # Do we want to capture status here?
    esmith::util::serviceControl
    (
	NAME   => $process,
	ACTION => 'start'
    ) || warn "Couldn't restart $process";
}

exit(0);
