#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-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)/vmail.sh

help()
{
  thisfilename=$(basename -- "$0")
  echo "$thisfilename"
  echo "Add domain to vmail system"
  echo ""
  echo "usage: $thisfilename -d <domain> [-l <limit>] [-q <quota>] [-r <ratelimit>] [-s <status>]"
  echo ""
  echo "  -h              Print this help."
  echo "  -d              Domain to add to vmail system."
  echo "  -l <limit>      Maximum number of mailboxes for this domain."
  echo "  -q <quota>      Default mailbox quota in GB."
  echo "  -r <ratelimit>  Default mailbox sending rate limit per hour, multiplied by 10 for limit per day."
  echo "  -s <status>     1 for enabled, 0 for disabled."
  echo ""
  echo "                  Defaults for all Options are configured in database."
  echo "                  NULL for LIMIT or QUOTA means no limit."
  exit
}

vmail:getoptions "$@"

# check for domain
if [[ -z $domain ]]; then
  echo "ERROR: domain name is required"
  exit 1
fi

# set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts=" -s -r -N -e"

# check if vmail domain dir exits
if [ -d $VMAIL_DIR/$domain ]; then
  echo "ERROR: Directory $VMAIL_DIR/$domain already exists."
  exit 1
fi

# check if domain exists in vmail database
dbquery="SELECT id from vm_domains WHERE domain='$domain';";
domains_id=`$dbcmd $dbcmdopts "$dbquery"`
if [[ -z $domains_id ]]; then
  # looks good, build SQL
  dbquery="INSERT INTO vm_domains SET domain='$domain'";
  if [[ ! -z "$status" ]] ; then
    if [[ "$status" == 0 ]] || [[ "$status" == 1 ]]; then
      dbquery="$dbquery, status='$status'"
    else
      echo "ERROR: status (-s) must be 1 or 0"
      exit 1
    fi
  fi
  if [[ ! -z "$limit" ]] ; then
    if [[ "$limit" == "NULL" ]]; then
      dbquery="$dbquery, mbox_limit=NULL"
    elif [[ "$limit" =~ ^[0-9]+$ ]]; then
      dbquery="$dbquery, mbox_limit='$limit'"
    else
      echo "ERROR: limit (-l) must numeric or NULL"
      exit 1
    fi
  fi
  if [[ ! -z "$quota" ]] ; then
    if [[ "$quota" == "NULL" ]]; then
      dbquery="$dbquery, mbox_quota_default=NULL"
    elif [[ "$quota" =~ ^[0-9]+$ ]]; then
      dbquery="$dbquery, mbox_quota_default='$quota'"
    else
      echo "ERROR: quota (-q) must numeric or NULL"
      exit 1
    fi
  fi
  if [[ ! -z "$ratelimit" ]] ; then
    if [[ "$ratelimit" == "NULL" ]]; then
      dbquery="$dbquery, mbox_ratelimit_default=NULL"
    elif [[ "$ratelimit" =~ ^[0-9]+$ ]]; then
      dbquery="$dbquery, mbox_ratelimit_default='$ratelimit'"
    else
      echo "ERROR: ratelimit (-r) must numeric or NULL"
      exit 1
    fi
  fi
  # add domain to vmail database
  eval $dbcmd $dbcmdopts "\"$dbquery\""
  # create vmail directory for domain
  if [[ ! -d "$VMAIL_DIR" ]] ; then
    install -o vmail -g vmail -m 750 -d $VMAIL_DIR
  fi
  if [[ ! -d "$VMAIL_DIR/$domain" ]] ; then
    install -o vmail -g vmail -m 750 -d $VMAIL_DIR/$domain
  fi
elif [[ "$domains_id" -gt 0 ]] ; then
  echo "ERROR: $domain already exists in vmail database."
  exit 1
else
  echo "ERROR: System error querying vmail database"
  exit 1
fi