#!/bin/bash
set -e

# Handle the first-time initialization of MariaDB
DBDIR="/var/db/mysql"
DBUSER="mysql"
DBGROUP="mysql"

if [[ -e "$DBDIR/eopkg_mariadb" ]]; then
    exit 0
fi

if [[ ! -d "$DBDIR" ]]; then
    install -D -d -m 00755 "$DBDIR"
fi

chown -R "$DBUSER":"$DBGROUP" "$DBDIR"
/usr/bin/mariadb-install-db --user=$DBUSER --skip-name-resolve --datadir="$DBDIR" --basedir=/usr
echo "configured" > "$DBDIR/eopkg_mariadb"
chown -R "$DBUSER":"$DBGROUP" "$DBDIR"
