#!/bin/bash

db_name="zentyal"
db_user="zentyal"

create_db()
{
    PASSWD_FILE='/var/lib/zentyal/conf/zentyal-mysql.passwd'

    if [ -s $PASSWD_FILE ]
    then
        PASSWD=`cat $PASSWD_FILE`
    else
        PASSWD=`tr -dc A-Za-z0-9 < /dev/urandom | head -c8`
        echo -n $PASSWD > $PASSWD_FILE
        chmod 400 $PASSWD_FILE
    fi

    echo "Creating the $db_name database"
    echo "CREATE DATABASE $db_name;
          GRANT ALL ON $db_name.* TO '$db_user'@'localhost' IDENTIFIED BY \"$PASSWD\";
          FLUSH PRIVILEGES;" | mysql --defaults-file=/etc/mysql/debian.cnf

    perl -MEBox -MEBox::Util::SQL -e'EBox::init(); EBox::Util::SQL::createCoreTables(); 1';

    mysqladmin -u root password "$PASSWD"
}

# FIXME: Is all this needed with mysql? As it uses upstart probably it is always alive
# or we even can manage it...
# Check if we can connect to postgresql (with one retry after restart)
#su postgres -c 'psql -Alt' >& /dev/null || /etc/init.d/postgresql-8.4 restart
#su postgres -c 'psql -Alt' >& /dev/null || exit 1

if ! [ -d /var/lib/mysql/$db_name ]; then
    create_db
    touch /var/lib/zentyal/.db-created
fi

