#!/bin/sh

# Start the system database if not already running
#-------------------------------------------------

[ -x /etc/rc.d/init.d/system-mysqld ] || exit 0

if [ ! -e /var/lib/system-mysql/mysql.sock ]; then
    /sbin/chkconfig system-mysqld on
    /etc/rc.d/init.d/system-mysqld start
    sleep 3
fi

# Create default password if one is not set
#------------------------------------------

/usr/clearos/sandbox/usr/bin/mysqladmin -uroot -hlocalhost status >/dev/null 2>&1
dbret=$?

if [ $dbret -eq 0 ]; then
    umask 0177
    PASSWORD=`openssl rand -base64 30`
    echo "password = $PASSWORD" > /var/clearos/system_database/root
    /usr/clearos/sandbox/usr/bin/mysqladmin -uroot -hlocalhost password $PASSWORD >/dev/null 2>&1
    /usr/clearos/sandbox/usr/bin/mysqladmin -uroot -p$PASSWORD -hlocalhost flush-privileges >/dev/null 2>&1

    # Drop test database if it exists (cruft)
    /usr/clearos/sandbox/usr/bin/mysqladmin -uroot -p$PASSWORD -hlocalhost -f drop test >/dev/null 2>&1
fi
