#! /bin/bash

# shellcheck disable=SC2094
# ? This is done to ignore the message "Make sure not to read and write
# ? the same file in the same pipeline", which is a result of ${DATABASE}
# ? being used below. (This disables the message file-wide.)

# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh

DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
ALIAS_DATABASE="/tmp/docker-mailserver/postfix-virtual.cf"
QUOTA_DATABASE="/tmp/docker-mailserver/dovecot-quotas.cf"
MAILDEL=false

function __usage
{
  echo -e "\e[35mDELMAILUSER\e[31m(\e[93m8\e[31m)

\e[38;5;214mNAME\e[39m
    delmailuser - delete a user and related data

\e[38;5;214mSYNOPSIS\e[39m
    ./setup.sh email del [ OPTIONS ] { <MAIL ADDRESS> [<MAIL ADDRESS 2>\e[31m...\e[39m] \e[31m|\e[39m help }

\e[38;5;214mDESCRIPTION\e[39m
    Delete a mail user, aliases, quotas and mail data.

\e[38;5;214mOPTIONS\e[39m
    -y
        Indicate that \e[1mall mail data\e[22m is to be deleted without another prompt.

    -h
        Show this help dialogue.

\e[38;5;214mEXAMPLES\e[39m
    ./setup.sh email del -y test@domain.com another-test@domain.com
        Delete all mail data for the users 'test' and 'another-test'
        and do not prompt to ask if all mail data should be deleted.

    ./setup.sh email del woohoo@some-domain.org
        Delete the mail user, quotas and aliases, but ask again whether
        mailbox data should be deleted.

\e[38;5;214mEXIT STATUS\e[39m
    Exit status is 0 if command was successful, and 1 if there was an error.
"
}

if [[ ${1} == 'help' ]]
then
  __usage
  exit 0
fi

while getopts ":yYh" OPT
do
  case ${OPT} in
    y | Y )
      MAILDEL=true
      ;;

    h )
      __usage
      exit 0
      ;;

    * )
      __usage
      echo "The option ${OPT} is unknown." >&2
      exit 1
      ;;

  esac
done
shift $((OPTIND-1))

[[ -z ${*} ]] && { usage ; errex "No user specifed" ; }
[[ -s ${DATABASE} ]] || exit 0

if ! ${MAILDEL}
then
  read -r -p "Do you want to delete the mailbox as well (removing all mails) ? [Y/n] " MAILDEL_CHOSEN
  if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]
  then
    MAILDEL=true
  fi
fi

(
  flock -e 200
  ERROR=false

  for USER in "${@}"
  do
    # very simple plausibility check
    [[ ${USER} != *'@'*'.'* ]] && errex "No valid address: ${USER}"

    declare -a MAILARR
    MAILARR[0]="${USER%@*}"
    MAILARR[1]="${USER#*@}"

    # ${USER} must not contain /s and other syntactic characters
    UNESCAPED_USER="${USER}"
    USER=$(escape "${USER}")

    if [[ -f ${DATABASE} ]]
    then
      if ! sed -i "/^${USER}|/d" "${DATABASE}"
      then
        echo "${UNESCAPED_USER} couldn't be deleted in ${DATABASE}." >&2
        ERROR=true
      fi
    fi

    if [[ -f ${ALIAS_DATABASE} ]]
    then
      # delete all aliases where the user is the only recipient( " ${USER$}" )
      # delete user only for all aliases that deliver to multiple recipients ( ",${USER}" "${USER,}" )
      if sed -i \
        -e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \
        "${ALIAS_DATABASE}"
      then
        echo "${UNESCAPED_USER} and potential aliases deleted."
      else
        echo "Aliases for ${UNESCAPED_USER} couldn't be deleted in ${ALIAS_DATABASE}." >&2
        ERROR=true
      fi
    fi

    # remove quota directives
    if [[ -f ${QUOTA_DATABASE} ]]
    then
      if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}"
      then
        echo "Quota for ${UNESCAPED_USER} couldn't be deleted in ${QUOTA_DATABASE}." >&2
      fi
    fi

    if ! ${MAILDEL}
    then
      echo "Leaving the mailbox untouched. If you want to delete it at a later point use 'sudo docker exec mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}'"
      exit 0
    fi

    if [[ -e "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]]
    then
      if rm -R "/var/mail/${MAILARR[1]}/${MAILARR[0]}"
      then
        echo "Mailbox deleted."
      else
        echo "Mailbox couldn't be deleted." >&2
        ERROR=true
      fi
    else
      echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist." >&2
      ERROR=true
    fi
  done

  ${ERROR} && errex 'Errors encountered.'

) 200< "${DATABASE}"

exit 0
