#!/bin/sh

# Wait for the server to come up
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
        sleep 1
done

exit $ret
