#!/bin/bash
# Copyright (C) 2025 CEA - All Rights Reserved
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.


if [ "$(id -u)" != "0" ]; then
   echo "[!] This script must be run as root" 1>&2
   exit 1
fi

echo "Installing mysql-server and mysql-client..."
apt-get install -y mysql-server mysql-client

NEW_USER="mirai_user"
NEW_USER_PASSWORD="mirai_password"

sudo mysql << EOF
DROP DATABASE IF EXISTS mirai;
CREATE USER IF NOT EXISTS '$NEW_USER'@'localhost' IDENTIFIED BY '$NEW_USER_PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO '$NEW_USER'@'localhost' WITH GRANT OPTION;
EOF

mysql -u"$NEW_USER" -p"$NEW_USER_PASSWORD" < db.sql