#!/bin/bash
unset HISTFILE
USERNAME=$1
PASSWORD=$2
MYSQL_PASSWORD=$3

set -ex

useradd -g users -G sudo -s /bin/bash -m $USERNAME
echo -e "$PASSWORD\n$PASSWORD" | passwd $USERNAME
mysql -u root -p$MYSQL_PASSWORD mysql -e "CREATE USER '$USERNAME'@'localhost' IDENTIFIED WITH mysql_native_password BY '$PASSWORD'; GRANT ALL PRIVILEGES ON *.* TO '$USERNAME'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES"

mkdir /home/$USERNAME/public_html
chmod +x /home/$USERNAME/public_html
echo 'Dit bestand staat in je <u>public_html</u> directory!' >/home/$USERNAME/public_html/index.html
chown -R $USERNAME:users /home/$USERNAME/public_html
chmod g-w /home/$USERNAME
# Ubuntu 21.04 sets home directories to world-unreadable by default
# This is fine, but we want Apache userdir to still work.
# Therefore, add www-data to the ACL with rx permissions
setfacl -m u:www-data:rx /home/$USERNAME
