#!/bin/bash

[ ! -z "$MYSQL_LIB_UTIL" ] && return 0
MYSQL_LIB_UTIL=true

# Source the abstract library first and override its definitions below.
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/db "db"

function wait_to_start_db {
    i=0
    while (( ! echo ";" | mysql -u root -S "${MYSQL_DIR}/socket/mysql.sock" > /dev/null 2>&1) || [ ! -f ${MYSQL_DIR}/pid/mysql.pid ]) && [ $i -lt 30 ]
    do
        sleep 1
        i=$(($i + 1))
    done
}


function wait_to_start_db_as_user {
    if [ -n "$OPENSHIFT_MYSQL_DB_USERNAME" ]
    then
        i=0
        sleep 1
        while (( ! echo ";" | mysql -h $OPENSHIFT_MYSQL_DB_HOST -P $OPENSHIFT_MYSQL_DB_PORT -u $OPENSHIFT_MYSQL_DB_USERNAME --password="$OPENSHIFT_MYSQL_DB_PASSWORD" > /dev/null 2>&1) || [ ! -f ${MYSQL_DIR}/pid/mysql.pid ]) && [ $i -lt 30 ]
        do
            sleep 1
            i=$(($i + 1))
        done
    fi
}

function prepare_gear_for_standalone_mysql() {
    # Find an open localhost IP
    PROXY_IP=`find_open_ip $uid $uuid`

    # Add apache vhost configuration.
    create_standard_path_env_var
    import_env_vars
    $CART_INFO_DIR/bin/deploy_httpd_proxy.sh $application $namespace $uuid $PROXY_IP

    disable_stale_detection

    restart_httpd_graceful
}

