#!/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 email alias to vmail system"
  echo ""
  echo "usage: $thisfilename -e <email> -a <alias> [-h]"
  echo ""
  echo "  -h          Print this help."
  echo "  -e <email>  Existing email address that will receive alias emails."
  echo "  -a <alias>  Alias that will direct to existing email address."
  echo ""
  echo "              Aliases are for delivering an email to another address in the same domain."
  echo "              Use Forwarding for directing emails to an address in another domain."
  echo "              'email' should be the full address 'alias' just the local part (username)."
  echo "              .e.g to have info@example.com delivered (aliased) to joe@example.com do:"
  echo "              $thisfilename -e joe@example.com -a info"
}

vmail:getoptions "$@"

# check for email
if [[ -z $email ]]; then
  echo "email address is required"
  exit
fi

# check for alias
if [[ -z $alias ]]; then
  echo "alias is required"
  exit
fi

# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
if [ -z $domain_id ] ; then
  echo "ERROR: Domain $domain does not exist."
  exit 1
fi

# get mbox id, which also verfies that email address exists
mbox_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$mbox';"`
if [[ -z $mbox_id ]]; then
  # mbox does not exist, can't create alias
  echo "ERROR: Address to Alias To ($email) does not exist."
  exit 1
elif [[ $mbox_id -gt 0 ]]; then
  # verified mbox, check for existing alias
  existing_alias=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT vm_aliases.id FROM vm_aliases WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id='$mbox_id';"`
  if [[ -z $existing_alias ]]; then
    # existing alias does not exist, make sure mbox with alias name does not exist
    alias_mbox_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$alias';"`
    if [[ -z $alias_mbox_id ]]; then
      # mailbox does not exist, add alias
      dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_aliases SET vm_aliases.alias=\"$alias\", vm_aliases.mbox_id=\"$mbox_id\";'"
      eval $dbcmd
    else
      echo "ERROR: Email account $alias@$domain exists, can't create alias with the same name."
      exit 1
    fi
  else
    echo "ERROR: Alias for $alias To $email already exists."
    exit 1
  fi
else
  # db query error
  echo "ERROR: System error querying vmail database."
  exit 1
fi