#!/bin/sh
#
# (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
# (c) 2007-2010 Mandriva, http://www.mandriva.com
#
# $Id$
#
# This file is part of Mandriva Management Console (MMC).
#
# MMC 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.
#
# MMC 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 MMC.  If not, see <http://www.gnu.org/licenses/>.
### BEGIN INIT INFO
# Provides:          mmc-agent
# Required-Start:    $remote_fs slapd
# Required-Stop:     slapd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MMC agent
### END INIT INFO

# Redhat based distributions specific
# chkconfig: 345 99 60
# description: MMC agent

# Variables
DESC="Mandriva Management Console : mmc-agent"
DAEMON="/usr/sbin/mmc-agent"
PIDFILE="/var/run/mmc-agent.pid"

test -x $DAEMON || exit 0

RETVAL=0

# Return PID of already running mmc-agent, if any
# If not, return 0
get_running_pid() {
    local pid=0
    if [ -e ${PIDFILE} ]
    then
	if ps --no-headers `cat ${PIDFILE}` > /dev/null
	then
	    # PIDFILE content correspond to a pid
	    pid=`cat ${PIDFILE}`
	else
	    # PIDFILE content is errorneous, 
	    # remove the file
	    rm -f ${PIDFILE}
	fi
    fi

    echo $pid
}

# Function to start daemon
start () {
    echo -n "Starting ${DESC} : "
    pid=$(get_running_pid)
    # Start daemon if not running
    if [ $pid -eq 0 ]
    then
        ${DAEMON} >/dev/null 2>&1
        RETVAL=$?
        # If return code is 0, everything went fine
	if [ ${RETVAL} -eq 0 ]
	then
            echo "done."
	else
            echo "failed."
	fi
    else
	echo "already running."
        RETVAL=0
    fi
}

# Function to stop daemon
stop () {
    echo -n "Stopping ${DESC} : "
    pid=$(get_running_pid)
    if [ $pid -ne 0 ]
    then
        # If there's one, get the pid and send SIG 15
        kill -15 $pid
        # Get return code
        RETVAL=$?
    else
	RETVAL=0
    fi
    if [ $RETVAL -eq 0 ]
    then
	rm -f ${PIDFILE}
        echo "done."
    else
        echo "failed."
    fi
}

# Function to force the daemon to stop (SIGKILL)
force_stop () {
    echo -n "Stopping (forced) ${DESC} : "
    pid=$(get_running_pid)
    if [ $pid -ne 0 ]
    then
        kill -9 $pid
        # Get return code
        RETVAL=$?
    else
        RETVAL=0
    fi
    if [ $RETVAL -eq 0 ]
    then
	rm -f ${PIDFILE}
        echo "done."
    else
        echo "failed."
    fi
}

# Function to restart (run stop, then start)
restart() {
    stop
    sleep 2
    start
}

# Function to run the service in debug mode
debug_run() {
    echo "$0: Starting MMC agent in debug mode ..."
    ${DAEMON} -d 2>&1
    echo "$0: Exit status: $?"
    RETVAL=0
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    force-stop)
        force_stop
        ;;
    restart | force-reload)
	    restart
        ;;
    debug-run)
        debug_run
        ;;
    *)
        echo "Usage: ${0} {start|stop|restart|force-reload|force-stop|debug-run}"
        exit 1
esac

exit $RETVAL
