#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          mysql-ndb-mgm
# Required-Start:    $syslog
# Required-Stop:     $syslog mysql
# Should-Start:      $local_fs $remote_fs $network $named $time
# Should-Stop:       $local_fs $remote_fs $network $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the mysql database cluster management daemon
# Description:       Controls the MySQL NDB Management Node daemon "ndb_mgmd".
### END INIT INFO
#
set -e
set -u
${DEBIAN_SCRIPT_DEBUG:+ set -v -x}

# Variables
SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
DAEMON=/usr/sbin/ndb_mgmd
CONF=/etc/mysql/ndb_mgmd.cnf
export HOME=/etc/mysql/

# Safeguard (relative paths, core dumps..)
cd /
umask 077

# Exit *silently* if we're not supposed to be started.
#
# The Debian scripts should execute these scripts to stop and start
# the daemon when upgrading if it is started. On the other hand it should
# remain silently if the server has not even been configured.
# See /usr/share/doc/mysql-server-*/README.Debian for more information.
test -x $DAEMON || exit 0
test -r $CONF || exit 0
. /lib/lsb/init-functions

#
# main()
#
case "${1:-''}" in
  'start')
	# Start daemon
	log_daemon_msg "Starting MySQL NDB Management Node" "ndb_mgmd"
	# --pid-file does not work as the daemon forks itself with $PID=$PID+1
	if start-stop-daemon \
		--start \
		--exec $DAEMON \
		--user mysql \
		-- \
		-f $CONF
	then
	  log_end_msg 0
	else
	  log_end_msg 1
	  log_warning_msg "Please take a look at the syslog."
	  exit 1
	fi			  
	;;

  'stop')
  	log_daemon_msg "Stopping MySQL NDB Management Node" "ndb_mgmd"	
	if start-stop-daemon \
		--stop \
		--oknodo \
		--exec $DAEMON
	then
	  log_end_msg 0
	else
	  log_end_msg 1
	  exit 1
        fi
	;;

  'restart'|'force-reload')
	set +e; $SELF stop; set -e
	$SELF start 
	;;

  *)
	echo "Usage: $SELF start|stop|restart|force-reload"
	exit 1
	;;
esac

