#!/bin/sh

if [ ! -e /var/lib/mysql/mysql ]; then
    cd /var/lib/mysql
    mariadb-install-db --auth-root-authentication-method=normal \
        --datadir /var/lib/mysql
fi

if [ ! -e /var/lib/mysql/wordpress ]; then
    mariadbd &

    sleep 5

    mariadb --user root <<EOF
CREATE DATABASE $DB_DB;
CREATE USER $DB_USER@'%' IDENTIFIED BY '$(cat /run/secrets/db_password)';
GRANT ALL PRIVILEGES ON $DB_DB.* TO $DB_USER@'%';
EOF

    killall mariadbd
fi

exec "$@"