#!/bin/sh

daemon_pid="$1"

# Wait for the server to come up or for the mysqld process to disappear
ret=0
while /bin/true; do
        RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="<%= @socket_file %>" --user=UNKNOWN_MYSQL_USER ping 2>&1`
        mret=$?
        if [ $mret -eq 0 ]; then
            break
        fi
        # exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
        # anything else suggests a configuration error
        if [ $mret -ne 1 -a $mret -ne 11 ]; then
            ret=1
            break
        fi
        # "Access denied" also means the server is alive
        echo "$RESPONSE" | grep -q "Access denied for user" && break

        # Check process still exists
        if ! /bin/kill -0 $daemon_pid 2>/dev/null; then
            ret=1
            break
        fi
        sleep 1
done

exit $ret
