#!/sbin/runscript
#

: ${MYSQLD_PIDFILE:=/var/run/mysqld/mysqld.pid}
: ${MYSQLD_DATADIR=/var/lib/mysql}

depend()
{
	need net localmount
	provide mysqld
}

start_pre()
{
	if [ ! -d /var/run/mysqld ]; then
		mkdir -p /var/run/mysqld
		chown mysqld:mysqld /var/run/mysqld
	fi

	if [ ! -d ${MYSQLD_DATADIR} ]; then
		ebegin "Initializing MySQL datadir: ${MYSQLD_DATADIR}"
		mkdir -p ${MYSQLD_DATADIR}
		/usr/bin/mysql_install_db --datadir=${MYSQLD_DATADIR} \
			--user=mysqld 2>&1 >/dev/null && \
		chown -R mysqld:mysqld ${MYSQLD_DATADIR}
		eend $?
	fi
}

start()
{
	ebegin "Starting MySQL server"
	start-stop-daemon --start --user mysqld:mysqld \
		--pidfile ${MYSQLD_PIDFILE} --background \
		--exec /usr/sbin/mysqld -- \
		--basedir=/usr --datadir=${MYSQLD_DATADIR} \
		--user=mysqld --pid-file=${MYSQLD_PIDFILE} \
		--external-locking ${MYSQLD_ARGS} 2>&1 >/dev/null
	eend $?
}

stop()
{
	ebegin "Stopping MySQL server"
	start-stop-daemon --stop --pidfile ${MYSQLD_PIDFILE}
	eend $?
}
