#!/bin/bash
#
# MySQL daemon start/stop script.
#
# Debian version. Based on the original by TcX.
#
set -e
set -u
${DEBIAN_SCRIPT_DEBUG:+ set -v -x}

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

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
CONF=/etc/mysql/my.cnf
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
RUNDIR=/var/run/mysqld/

# priority can be overriden and "-s" adds output to stderr
ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"

# Safeguard (relative paths, core dumps..)
cd /
umask 077
export PATH=/bin:/usr/bin

# mysqladmin likes to read /root/.my.cnf. This is usually not what I want
# as many admins e.g. only store a password without a username there and
# so break my scripts.
export HOME=/etc/mysql/

## fetch a particular option from mysql's invocation
#
# usage: void mysqld_get_param option
mysqld_get_param() {
	/usr/sbin/mysqld --print-defaults \
		| tr " " "\n" \
		| grep -- "--$1" \
		| tail -n 1 \
		| cut -d= -f2
}

## Checks if there is a server running and if so if it is accessible.
#
# check_alive insists on a pingable server
# check_dead also fails if there is a lost mysqld in the process list
#
# Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
mysqld_status () {
    ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
    
    ps_alive=0
    pidfile=`mysqld_get_param pid-file`
    if [ -f "$pidfile" ]; then
        if ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
    fi
    
    if [ "$1" = "check_alive"  -a  $ping_alive = 1 ] ||
       [ "$1" = "check_dead"   -a  $ping_alive = 0  -a  $ps_alive = 0 ]; then
	return 0 # EXIT_SUCCESS
    else
  	if [ "$2" = "warn" ]; then
  	    /bin/echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
	fi
  	return 1 # EXIT_FAILURE
    fi
}

#
# main()
#

case "${1:-''}" in
  'start')
	# check for config file
	if [ ! -r $CONF ]; then
	  /bin/echo -e "\nWARNING: $CONF cannot be read. See README.Debian."
        fi 
	# check for /var/run/mysqld/ which maybe have only been on a tempfs
	if [ ! -d $RUNDIR ]; then
	  install --directory --owner=mysql --mode=755 $RUNDIR
	fi
	# Start daemon
	echo -n "Starting MySQL database server: mysqld"
	if mysqld_status check_alive nowarn; then
	   echo "...already running."
	else
  	    /usr/bin/mysqld_safe > /dev/null 2>&1 &
	    for i in 1 2 3 4 5 6; do
                sleep 1
	        if mysqld_status check_alive nowarn ; then break; fi
            done
	    if mysqld_status check_alive warn; then
                echo "."
	        # Now start mysqlcheck or whatever the admin wants.
	        /etc/mysql/debian-start
	    else
	        echo "...failed."
	        /bin/echo -e "\tPlease take a look at the syslog."
	    fi
	fi

	if $MYADMIN variables | egrep -q have_bdb.*YES; then
	    /bin/echo "BerkeleyDB is obsolete, see /usr/share/doc/mysql-server/README.Debian.gz" | $ERR_LOGGER -p daemon.info
	fi

	;;

  'stop')
	# * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
	# at least for cron, we can rely on it here, too. (although we have 
	# to specify it explicit as e.g. sudo environments points to the normal
	# users home and not /root)
	echo -n "Stopping MySQL database server: mysqld"	
	if ! mysqld_status check_dead nowarn; then
	  set +e
	  shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
	  set -e
	  if [ "$r" -ne 0 ]; then
	    /bin/echo -e -n "...failed.\n$shutdown_out\nKilling MySQL database server by signal: mysqld"
	    killall -15 mysqld
            server_down=
	    for i in 1 2 3 4 5 6 7 8 9 10; do
              sleep 1
              if mysqld_status check_dead nowarn; then server_down=1; break; fi
            done
          if test -z "$server_down"; then killall -9 mysqld; fi
	  fi
        fi

        if ! mysqld_status check_dead warn; then
	  echo "...failed."
	  echo "Please stop MySQL manually and read /usr/share/doc/mysql-server/README.Debian!"
	  exit -1
	else
	  echo "."
        fi
	;;

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

  'reload'|'force-reload')
  	echo -n "Reloading MySQL database server: mysqld"
	$MYADMIN reload
	echo "."
	;;

  'status')
	if mysqld_status check_alive nowarn; then
	  $MYADMIN version
	else
	  echo "MySQL is stopped."
	fi
  	;;

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

