#!/bin/bash
#
# Initialize root password
# and secure MariaDB installation
#

INITDONE="/etc/eole/.galera"

[[ "$(CreoleGet dbEnable)" == "non" ]] && exit 0
[[ -e ${INITDONE} ]] && exit 0

ROLE=$(CreoleGet dbClusterPosition 'Leader')
SECURE_CMD="mysql_secure_installation"
PASSWORD=$(pwgen -1 10)
passwd="/usr/share/eole/sbin/mariadbPwd"
logdir="/var/log/eole"
logfile="${logdir}/maria-init.log"

[[ ! -d "${logdir}"  ]] && mkdir -p ${logdir}

if [[ ${ROLE} == "Leader" ]]
then
   service mysql start
   ${passwd} ${PASSWORD}

   # Run secure installation script
   ${SECURE_CMD} > ${logfile} 2>&1 <<__EOF__
${PASSWORD}
y
n
y
y
y
y
__EOF__
   if [[ ${?} -eq 0 ]]
   then
	   service mysql stop
	   touch ${INITDONE}
	   exit ${?}
   else
	   service mysql stop
	   exit ${?}
   fi
else
   exit 0
fi