#!/bin/sh

set -e

# Check if running as root
if [ `id -u` -ne 0 ]; then
   echo "Error: $0 must be run as root" 1>&2
   exit 1
fi

systemctl start mysql

# Check if the MySQL database empire exists.
# If the DB does not exist, it will create the DB, the DB user and the
# user password.
if ! mysql -e "use empire" > /dev/null 2>&1; then
    echo "Create mysql database empire"
    mysql -Bse "CREATE DATABASE IF NOT EXISTS empire;"
    mysql -Bse "CREATE USER IF NOT EXISTS empire_user@localhost IDENTIFIED BY 'empire_password';"
    mysql -Bse "GRANT ALL ON empire.* TO empire_user@localhost;"
    mysql -Bse "FLUSH PRIVILEGES;"
fi

cd /usr/share/powershell-empire
exec python3 empire.py "$@"
