#!/bin/sh

set -e

sudo bash <<END
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 ! runuser -u mysql -- mysql -e "use empire" > /dev/null 2>&1; then
    echo "Create mysql database empire"
    runuser -u mysql -- mysql -Bse "CREATE DATABASE IF NOT EXISTS empire;"
    runuser -u mysql -- mysql -Bse "CREATE USER IF NOT EXISTS empire_user@localhost IDENTIFIED BY 'empire_password';"
    runuser -u mysql -- mysql -Bse "GRANT ALL ON empire.* TO empire_user@localhost;"
    runuser -u mysql -- mysql -Bse "FLUSH PRIVILEGES;"
fi

END

cd /usr/share/powershell-empire
if [ ! -d .venv ]; then
    python3 -m venv .venv
    . .venv/bin/activate
    .venv/bin/python3 -m pip install .
    .venv/bin/python3 empire.py "$@"
else
    . .venv/bin/activate
    .venv/bin/python3 empire.py "$@"
fi
