#!/bin/bash -e

. ./xi-sys.cfg

##########################################
# FUNCTIONS FOR MYSQL
##########################################
update_mysql_root_password() {
    case "$dist" in
        ubuntu22 | ubuntu24 )
            # This method of authentication is deprecated and subject for removal ref: https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html
            mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '$1';" &>/dev/null
            ;;
        debian11 | debian12 )
            mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED by '$1';" &>/dev/null
            ;;
        *)
            mysqladmin -u root password "$1"
            ;;
    esac
}

##########################################
# INITIALIZE MYSQL
##########################################

# See if user already initialized MySQL
if [ -f installed.mysql ]; then
    echo "MySQL already initialized - skipping."
    exit 0
fi

# Check that mysql is installed
if [ ! -f "/usr/bin/mysql" ]; then
    echo "ERROR: MySQL not installed - exiting." >&2
    exit 1
fi
echo "MySQL installed OK - continuing..."

echo "Starting MySQL..."
if ! service "$mysqld" restart; then
    echo "ERROR: MySQL failed to start - exiting." >&2
    exit 1
fi

echo "Initializing MySQL..."

# Test for null MySQL root password
if mysqlshow -u root &>/dev/null; then
    # Set the password to random password
    mysqlpass="$(tr -dc '[:alnum:]' < /dev/urandom | head -c 20)"
    update_mysql_root_password "$mysqlpass"
    echo "MySQL root password is now set to: $mysqlpass"
# Test for pre-saved password (from ./fullinstall script)
elif mysqlshow -u root -p"$mysqlpass" &>/dev/null; then
    echo "Saved password '$mysqlpass' worked..."
else
    for i in 1 2 3; do
        if [ "$INTERACTIVE" = "True" ]; then
            # Ask for the password
            echo "Enter the MySQL root password to continue..."
            read -p "MySQL Root Password: " pass
        fi

        # Test the password
        if mysqlshow -u root -p"$pass" &>/dev/null; then
            echo "Password validated."
            mysqlpass="$pass"
            break
        else
            echo "Password failed." >&2
            [ $i -eq 3 ] && exit 1
        fi
    done
fi

# we only automatically tune mysql if we just installed it
# see scripts/mysql_tune.sh
if [ "$TUNE_MYSQL" = "True" ]; then

    add=0
    mycnf="/etc/my.cnf"

    if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
        add=1
        mycnf="/etc/mysql/conf.d/mysql.cnf"
        if [ ! -f $mycnf ]; then
            mycnf="/etc/mysql/my.cnf"
            add=0
        fi
    fi

    if [ "$dist" == "el8" ] || [ "$dist" == "el9" ]; then
        mycnf="/etc/my.cnf.d/mysql-server.cnf"
    fi

    # backup the cnf file first
    mycnfbak="$mycnf.bak"
    mycnfnew="$mycnf.new"
    cp $mycnf $mycnfbak

    if [ -f $mycnf ]; then

        if [ $add -eq 1 ]; then
            ADD="\n\n[mysqld]\ntmp_table_size=64M\nmax_heap_table_size=64M\nkey_buffer_size=32M\ntable_open_cache=32\ninnodb_file_per_table=1\nmax_allowed_packet=512M\nsort_buffer_size=32M\nmax_connections=1000\nopen_files_limit=4096\n"

            # Special case for ubuntu 22, and 24 which run MySQL 8
            if [ "$dist" == "ubuntu22" ] || [ "$dist" == "ubuntu24" ]; then
                ADD="$ADD\nsql_mode=NO_ENGINE_SUBSTITUTION\n"
            else 
                ADD="$ADD\nquery_cache_size=16M\nquery_cache_limit=4M\nsql_mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\n"
            fi

            echo -e "$ADD" >> $mycnf
        else

            # Note: If you're changing the tuning options here, please also change mysql_tune.sh
            REPLACEMENT='s/\[mysqld\]/\[mysqld\]\ntmp_table_size=64M\nmax_heap_table_size=64M\nkey_buffer_size=32M\ntable_open_cache=32\ninnodb_file_per_table=1\nmax_allowed_packet=512M\nsort_buffer_size=32M\nmax_connections=1000\nopen_files_limit=4096\ngroup_concat_max_len=8388608\n'

            if [ "$dist" == "el8" ] || [ "$dist" == "el9" ]; then
                # query cache is removed in MySQL 8 but not in any MariaDB (yet).
                REPLACEMENT="$REPLACEMENT\nsql_mode=NO_ENGINE_SUBSTITUTION\n"
            else
                REPLACEMENT="$REPLACEMENT\nquery_cache_size=16M\nquery_cache_limit=4M\n"
            fi

            REPLACEMENT="$REPLACEMENT/"

            if ! sed -i "$REPLACEMENT" $mycnf; then
                successful=0
            else
                successful=1
            fi

        fi

        # if mysql doesn't restart, then we need to revert our changes
        if ! service "$mysqld" restart; then

            # keep a copy of the mycnf that failed for support
            cp $mycnf $mycnfnew
            cp $mycnfbak $mycnf

            # if it fails again we're out
            if ! service "$mysqld" restart; then
                echo "ERROR: MySQL failed to start - exiting." >&2
                exit 1
            fi
        fi
    fi
fi

# Update xi-sys.cfg with MySQL password for later use by subcomponent install
if ! ./xivar mysqlpass "$mysqlpass"; then
    echo "ERROR: Failed to update xi-sys.cfg with MySQL password - exiting." >&2
    exit 1
fi

# Update automysqlbackup script
if ! sed -i -e "s/PASSWORD=.*/PASSWORD=$mysqlpass/g" nagiosxi/automysqlbackup; then
    echo "ERROR: Failed to update automysqlbackup with MySQL password - exiting." >&2
    exit 1
fi

echo "MySQL initialized OK"
touch installed.mysql

