#!/bin/bash

# Disable startup MySQL on boot in Ubuntu
# Service mysqld doesn't start on boot in Fedora and CentOS
# Delete config property 'bind-address' for remote mode (0.0.0.0)

if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

if [ "$DISTRO_NAME" = "ubuntu" ]; then
    if [ -e "/etc/init/mysql.conf" ]; then
        sed -i "s/start on runlevel \[.*\]/start on never runlevel [2345]/g" /etc/init/mysql.conf
        # The mysql configuration file name and directory changed in Ubuntu Xenial.
        # TODO(shuyingya): delete this if statement when we not support trusty
        if [ "$DIB_RELEASE" = "trusty" ]; then
            sed -i '/bind-address/d' /etc/mysql/my.cnf
        elif [ "$DIB_RELEASE" = "xenial" ]; then
            sed -i '/bind-address/d' /etc/mysql/mysql.conf.d/mysqld.cnf
        fi
    else
        update-rc.d -f mysql remove
    fi
fi

# Script for starting mysql

install -D -g root -o root -m 0755 $(dirname $0)/start-mysql.sh /opt/start-mysql.sh
