#!/bin/bash
datadir="/var/opt/rh/rh-mariadb102/lib/mysql"

# We have to re-enable SCL environment, because /sbin/service
# clears almost all environment variables.
# Since X_SCLS is cleared as well, we lose information about other
# collections enabled.
. /opt/rh/rh-mariadb102/service-environment
for sclname in $RH_MARIADB102_SCLS_ENABLED ; do
    . /opt/rh/$sclname/enable
    export X_SCLS="$X_SCLS $sclname"
done

# we want start daemon only inside "scl enable" invocation
if ! scl_enabled $sclname ; then
    echo "Collection $sclname has to be listed in /opt/rh/rh-mariadb102/service-environment"
    exit 1
fi

if [ ! -f $datadir/mysql/user.frm ]
then
    touch /var/log/mariadb102/mariadb.log
    chown mysql:mysql /var/log/mariadb102/mariadb.log
    chmod 0640 /var/log/mariadb102/mariadb.log
    echo "Initializing mariadb102 database"
    /opt/rh/rh-mariadb102/root/usr/libexec/mysql-prepare-db-dir
    ret=$?
    if [ $ret -ne 0 ] ; then
        echo "Initialization of MySQL database failed." >&2
        echo "Perhaps /etc/opt/rh/rh-mariadb102/my.cnf is misconfigured." >&2
        # Clean up any partially-created database files
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
            rm -rf "$datadir"/*
        fi
        exit $ret
    fi
    # set root password , 104 and above have a different syntax and allow root passwordless access
    if [ 102 -le  103 ]
    then
      /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --bootstrap --datadir="$datadir" --user="mysql" < /var/lib/mysql/set.password
    else
      /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --bootstrap --datadir="$datadir" --user="mysql" < /var/lib/mysql/set.password2
    fi
    # upgrade does not need to be run on a fresh datadir
    #echo "5.7.24" >"$datadir/mysql_upgrade_info"
    # In case we're running as root, make sure files are owned properly
    chown -R "mysql:mysql" "$datadir"

    if [ -f /home/e-smith/db/mariadb102/mysql.dump ]
    then
        /sbin/e-smith/expand-template /etc/e-smith/sql/init102/00_restore_dumped_dbs
    fi
else
   # else we set root password anyway ! just to be sure ! 104 and above have a different syntax and allow root passwordless access
   if [ 102 -le  103 ]
   then
     /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --bootstrap --datadir="$datadir" --user="mysql" < /var/lib/mysql/set.password
   else
     /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --bootstrap --datadir="$datadir" --user="mysql" < /var/lib/mysql/set.password2
   fi
   exit 0
fi
