#!/bin/bash


SHARED_BASE=usr/local/dh/mysql
VSQL_BASES=`/bin/ls -d /var/lib/vservers/{psmysql,wpsql}* 2>/dev/null` || true
OUTPUT_PATH=/${SHARED_BASE}/etc


find_mysql_services() {
        MYSQL_BASE=$1
        RESULT=

        cd $MYSQL_BASE
        for service in `ls -1`; do
                [ -d ${service}/data ] &&
                [ -L ${service}/base ] &&
                [ -L ${service}/bin ] &&
                [ -L ${service}/${service}-mysqld ] &&
                [ -L ${service}/mysqld--${service} ] &&
                RESULT="$RESULT $service"
        done
        echo $RESULT
}

export SERVERS=`find_mysql_services "/$SHARED_BASE"`
export SERVERS_LIMBO=`echo $SERVERS | tr " " "\n" | grep -- "-limbo$"`
export SERVERS_NORMAL=`echo $SERVERS | tr " " "\n" | grep -v -- "-limbo$"`

export SERVERS_VSQL=
for VSQL_BASE in $VSQL_BASES; do
        SERVERS_VSQL="$SERVERS_VSQL `find_mysql_services ${VSQL_BASE}/${SHARED_BASE}`"
done

echo -n $SERVERS > $OUTPUT_PATH/services
echo -n $SERVERS_NORMAL > $OUTPUT_PATH/services_normal
echo -n $SERVERS_LIMBO > $OUTPUT_PATH/services_limbo
echo -n $SERVERS_VSQL > $OUTPUT_PATH/services_vsql
