#!/bin/bash -e

. /usr/share/debconf/confmodule

chown mysql:mysql /var/log/mysql.graphite /opt/mysql.graphite 

 
test -d /opt/mysql.graphite/data && chown mysql:mysql /opt/mysql.graphite/data
 
test -d /opt/mysql.graphite/bin-logs && chown mysql:mysql /opt/mysql.graphite/bin-logs
 
test -d /opt/mysql.graphite/relay-logs && chown mysql:mysql /opt/mysql.graphite/relay-logs

[ ! -e /usr/lib/libtcmalloc_minimal.so ] && [ -e /usr/lib/libtcmalloc_minimal.so.4.1.2 ] && ln -s /usr/lib/libtcmalloc_minimal.so.4.1.2 /usr/lib/libtcmalloc_minimal.so

test -e /opt/tmp || mkdir /opt/tmp && chmod go+wt /opt/tmp

if grep -q FIXME /etc/mysql/graphite.conf.d/server-id.cnf; then
    echo "generating /etc/mysql/graphite.conf.d/server-id.cnf"
    serverid=`od -An -N2 -l /dev/random`;
    echo -e "[mysqld]\nserver-id = $serverid" > /etc/mysql/graphite.conf.d/server-id.cnf
fi


    if [ ! -e /etc/mysql/graphite.conf.d/wsrep.cnf ]; then
        echo "generating /etc/mysql/graphite.conf.d/wsrep.cnf"
        /usr/bin/generate-mysql-wsrep-conf-graphite ||:
    fi


/usr/sbin/regenerate-mysql-monitor-monrun ||:

/usr/sbin/update-rc.d mysql.graphite start 19 2 3 4 5 . stop 10 0 1 6 .
