#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# load include file
source $(dirname $0)/vhost.sh

help()
{
  thisfilename=$(basename -- "$0")
  echo "Creates MySQL database and db user for specified virtualhost."
  echo ""
  echo "usage: $thisfilename -d <domain> [OPTIONS]"
  echo ""
  echo "  -h            Print this help."
  echo "  -d <domain>   Domain name of VirtualHost to add db for."
  echo "  -u <username> Username for accessing the database. Optional, autogenerated if none specified."
  echo "  -p <password> Password for username. Optional, random password generated if none specified."
  echo "  -w            Write db info to /srv/www/domain/.my.cnf and create include in /home/username/.my.cnf."
  echo "  -v            Verbose - output newly created db info to console."
  echo ""
  echo "                MySQL database names is based on virtualhost with . replaced by the word 'dot'"
  echo "                and - replaced by the word 'dash'. If username is autogenerated it is based on"
  echo "                the shell username & the virtualhost name."
  echo "                e.g. for virtualost example.com the db name will be 'exampledotcom' and the"
  echo "                username will be examplec@example.com."
  echo "                It is highly recommended to use either the -w or -v option if you don't use -p."
}

vhost:getoptions "$@"

# check for domain (virtualhost)
if [[ -z $domain ]]; then
  echo "domain is required"
  exit
fi

# make sure virtualhost exists
if [[ ! -d /srv/www/$domain ]]; then
  echo "virtualhost $domain does not exist"
  exit 1
fi

# set database name
database=${domain//./dot}
database=${database//-/dash}

# make sure database doesn't already exist
if [[ -d /var/lib/mysql/$database ]]; then
  echo "database $database already exists"
  exit 1
fi

# get & set username of virtualhost
vhost_username=$(stat -c '%U' /srv/www/$domain)

# check for and set mysql username
if [[ -z $username ]]; then
  username=$vhost_username@$domain
fi

# check for and set mysql password
if [[ -z $password ]]; then
  password=`/usr/bin/pwgen 16 1`
fi

mysqladmin create $database
mysql -e "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';"
mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';"
mysqladmin flush-privileges

# save mysql db info to file
if [[ -n $write ]]; then
  touch /srv/www/$domain/.my.cnf
  chown $vhost_username:$vhost_username /srv/www/$domain/.my.cnf
  chmod 640 /srv/www/$domain/.my.cnf
  echo '[client]' > /srv/www/$domain/.my.cnf
  echo "host=127.0.0.1" >> /srv/www/$domain/.my.cnf
  echo "user=$username" >> /srv/www/$domain/.my.cnf
  echo "password=$password" >> /srv/www/$domain/.my.cnf
  echo '[mysql]' >> /srv/www/$domain/.my.cnf
  echo "database=$database" >> /srv/www/$domain/.my.cnf
  touch /home/$vhost_username/.my.cnf
  chown $vhost_username:$vhost_username /home/$vhost_username/.my.cnf
  chmod 640 /home/$vhost_username/.my.cnf
  if [[ ! -f /home/$vhost_username/.my.cnf ]]; then
    echo "#[client]" > /home/$vhost_username/.my.cnf
    echo '!include' "/srv/www/$domain/.my.cnf" >> /home/$vhost_username/.my.cnf
  fi
fi

if [[ -n $verbose ]]; then
  echo "database=$database user=$username password=$password"
fi