#!/bin/sh -e
#
# MySQL daemon start/stop script.
#
# Debian version. Based on the original by TcX.
#

test -x /usr/sbin/mysqld || exit 0

export PATH=/bin:/usr/bin

pid_file=/var/run/mysqld/mysqld.pid
if [ -r /etc/mysql/my.cnf ]; then
    tmp=`grep '^pid-file' /etc/mysql/my.cnf | sed 's/.*=[\t ]*//'`
    if [ -n "$tmp" ]; then
	pid_file=$tmp
    fi
fi

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

case "$1" in
  'start')
	# Start daemon
	echo -n "Starting MySQL database server: mysqld"	
	/usr/bin/safe_mysqld > /dev/null 2>&1 &
	sleep 2
	if test -f "$pid_file"; then
	    echo "."
	else
	    echo "...failed."
	fi
	;;

  'stop')
	# Stop daemon. 
	# We use a signal here to avoid having to know the root password.
	# We can't use mysqladmin here since it is in mysql-client.deb
	# which my absent during installation process.
	echo -n "Stopping MySQL database server: mysqld"	
	if test -f "$pid_file"; then
		kill `cat $pid_file`
	        echo "."
	        # mysqld should remove the pid_file when it exits.
	else
	    	echo "...failed."
	fi
	;;

  'restart')
	$0 stop
	$0 start 
	;;

  'reload'|'force-reload')
  	echo -n "Reloading MySQL database server: mysqld"
	/usr/bin/mysqladmin reload
	echo "."
	;;

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

