#!/bin/sh
#
# Wrapper for MySQL daemon.
# Copyright (C) 2001  Dmitry V. Levin <ldv@fandra.org>
#
# 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.
#

PROG="${0##*/}"

[ -n "$mysqld" -a -n "$logfile" -a -n "$chroot" -a -n "$pidfile" -a -n "$socket" ] ||
{
	echo "$PROG: called in uninitialized environment" >&2
	exit 1
}

# Source function library.
. /etc/init.d/functions

SourceIfNotEmpty /etc/sysconfig/mysqld

[ -n "$NICE" ] && NICE="-n $NICE" || NICE=

exec >>"$logfile" 2>&1

echo "`LC_TIME=C date '+%Y-%b-%d %T'` :: startup"

#pidlist=`pidof -o $$ -o $PPID -o %PPID $mysqld 2>/dev/null`
#if [ -n "$pidlist" ]; then
#	echo "$mysqld already running: $pidlist"
#	exit 1
#fi

trap '' SIGHUP SIGPIPE SIGINT SIGQUIT SIGTERM

while :; do
	rm -f "$socket" "$pidfile"
	nice $NICE $mysqld "$@"
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		msg=succeded
	elif  [ $RETVAL -eq 143 -o $RETVAL -eq 137 ]; then
		msg=killed
	else
		msg=failed
	fi
	echo "`LC_TIME=C date '+%Y-%b-%d %T'` :: execution $msg"

	[ -f "$pidfile" -a "$msg" != killed ] && finished= || finished=1

#	Do not kill other running processes
#	pidlist=`pidof -o $$ -o $PPID -o %PPID $mysqld 2>/dev/null`
#	if [ -n "$pidlist" ]; then
#		echo "$mysqld still running: $pidlist"
#		killproc "$mysqld"
#	fi

	[ -z "$finished" ] || break

	echo "`LC_TIME=C date '+%Y-%b-%d %T'` :: restart"
done

echo "`LC_TIME=C date '+%Y-%b-%d %T'` :: shutdown"
