# wait result for max 10 seconds
wait_status() {
	__mode=$1
	__time=$2

	[ -n "$__time" ] && __until=$[$__time * 2] || __until=20

	case "$__mode" in
		start) __exp=0 ;;
		stop)  __exp=1 ;;
	esac

	__res=1
	for i in `seq 1 $__until`
	do
		mysql_status

		if [ $? -eq $__exp ]; then
			__res=0
			break
		fi

		sleep 0.5
	done

	return $__res
}

mysql_status() {
	#mysqladmin ping &> /dev/null
	#_chk=$?
	_chk=`pidof mysqld`

	[ "$_chk" != "" ] && _chk=0 || _chk=1
	return $_chk
}

start_check() {
	sleep 1
	mysql_status

	if [ $? -eq 0 ]; then
		echo_success
		__res=0
	else
		echo_failure
		__res=1
	fi
	echo

	return $__res
}

already_check() {
	mysql_status

	if [ $? -eq 0 ]; then
		echo_success
		echo
		exit 0
	fi
}

# init function
MySQLInit() {
	local minit init

	minit=0
	uinit=0

	/usr/sbin/usermod -G 2 mysql &> /dev/null || true
	userchk=$(grep '^mysql:' /etc/passwd)

	if [ -z "${userchk}" ]; then
		useradd -M -r -G 2 -d ${datadir} -s /sbin/nologin -c "MySQL server" mysql 2> /dev/null || true
		uinit=1
	fi

	# check default mysql database
	if [ ! -d "${datadir}" ]; then
		mkdir -p ${datadir}/{mysql,test} &> /dev/null
		chmod 750 ${datadir} &> /dev/null
		minit=1
	elif [ ! -d "${datadir}/mysql" ]; then
		mkdir -p ${datadir}/{mysql,test} &> /dev/null
		minit=1
	fi

	# check log directory
	[ ! -d "${logdir}" ] && {
		mkdir -p ${logdir} &> /dev/null
		chown -R mysql:root ${logdir} &> /dev/null
		chmod 750 ${logdir} &> /dev/null
	}

	[ ! -d "${piddir}" ] && {
		mkdir -p ${piddir} >& /dev/null
		chown -R mysql:root ${piddir} &> /dev/null
	}

	[ ! -f "${datadir}/mysql/user.MYI" ] && minit=1

	if [ ${minit} -eq 1 ]; then
		chown -R mysql:mysql ${datadir} &> /dev/null
		[ -x /sbin/restorecon ] && /sbin/restorecon "${datadir}"

		#Initiate database
		[ ${minit} -eq 1 ] && {
			action $"Initializing MySQL database: " \
				mysql_install_db --user=mysql \
							--datadir="${datadir}" \
							--basedir=/usr &> /dev/null
		}
		# Change permissions again to fix any new files.
		chown -R mysql ${datadir} &> /dev/null
		# Fix permissions for the permission database so that only the user
		# can read them.
		chmod -R og-rw ${datadir}/mysql &> /dev/null
	fi
}

# extract value of a MySQL option from config files
# Usage: get_mysql_option SECTION VARNAME DEFAULT
# result is returned in $result
# We use my_print_defaults which prints all options from multiple files,
# with the more specific ones later; hence take the last match.
get_mysql_option() {
	[ -n $4 ] && myconf="-c /etc/mysql/$4.cnf" || myconf="/etc/mysql/my.cnf"
	result=`/usr/bin/my_print_defaults $myconf "$1" | sed -n "s/^--$2=//p" | tail -n 1`
	if [ -z "$result" ]; then
		# not found, use default
		result="$3"
	fi
}

#
# SANDBOX API
#

myaction() {
	local STRING rc

	STRING=$1
	echo -n "$STRING "
	shift

	if [ $1 = 2 ]; then
		echo_passed $"$STRING"
		echo
		return $?
	fi

	"$@" && success $"$STRING" || failure $"$STRING"
	rc=$?
	echo
	return $rc
}

errmsg() {
		echo "$*" >& /dev/stderr
}

get_sandbox_list() {
	if [ $UID -eq 0 ]; then
		/bin/ls /etc/mysql/*.cnf 2> /dev/null
	else
		cd /var/run/mysqld >& /dev/null
		for sock in $(ls *.sock 2> /dev/null)
		do
			sock=$(echo ${sock} | sed 's/\.sock//g')
			if [ "${sock}" = "mysql" ]; then
				echo "default"
			else
				echo ${sock%-*}
			fi
		done
		cd - >& /dev/null
	fi
}

get_sbox_list() {
	local sbox
	for sbox in `get_sandbox_list`
	do
		echo ${sbox##*/} | /bin/sed 's/\.cnf$//g'
	done
}

check_sandbox() {
	local sbox
	for sbox in `get_sandbox_list`
	do
		if [ $UID -eq 0 ]; then
			[ "$1" = "default" ] && fname="my" || fname="$1"
			[ "/etc/mysql/$fname.cnf" = "$sbox" ] && return 0
		else
			[ "$1" = "$sbox" ] && return 0
		fi
	done

	return 1
}

get_sandbox_no() {
	local no=0
	local sbox

	for sbox in `get_sandbox_list`
	do
		no=$[$no + 1]
	done

	return $no
}

# Local variables:
# tab-width: 4
# c-basic-offset: 4
# End:
# vim: set filetype=sh noet sw=4 ts=4 fdm=marker:
# vim<600: noet sw=4 ts=4:
