#!/bin/bash
# Generated by CHEF
# Local modification will be overriden

<% if node['mariadb']['allow_root_pass_change'] -%>
<% if node['mariadb']['server_root_password'].empty? -%>
/usr/bin/mysqladmin -u root password "$1"
<% else -%>
/usr/bin/mysqladmin -u root -p'<%= node['mariadb']['server_root_password'] %>' password "$1"
<% end -%>

<% end -%>
password_flag=""
if [ "$1" ]; then
  password_flag="-p$1"
fi

<% if node['mariadb']['forbid_remote_root'] -%>
user_exist=`/usr/bin/mysql -u root ${password_flag} -D mysql -r -B -N -e "SELECT user from user where user = 'root' and host = '%'"`
if [ $user_exist == 'root' ]; then
  /bin/echo "DROP USER 'root'@'%';" | /usr/bin/mysql -u root ${password_flag}
fi
<% else -%>
/bin/echo "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '<%= node['mariadb']['server_root_password'] %>' WITH GRANT OPTION;" | /usr/bin/mysql -u root ${password_flag}
<% end -%>
