#!/bin/bash
# vim: shiftwidth=4 tabstop=4 expandtab

LIB_DIR="$( realpath "$( dirname "$( realpath "$0" )" )/../lib/ee-postfix-tools" )"
if [[ -e "$LIB_DIR/common" ]]; then
    # shellcheck source=/dev/null
    source "$LIB_DIR/common"
else
    echo "Failed to load ee-postfix-tools common lib"
    exit 1
fi

# shellcheck disable=SC2034
EXTRA_SHORT_USAGE="[email IDs] [bkp desc]"
IDS=()
# shellcheck disable=SC2317,SC2329
function handle_extra_args() {
    [[ $# -lt 1 ]] && usage
    if [[ $# -ge 2 ]]; then
        DESC="${*: -1}"
        IDS=( "${@:1:$#-1}" )
    else
        IDS=( "$@" )
        DESC=""
    fi
}
handle_args "$@"
[[ ${#IDS[@]} -eq 0 ]] && usage

# Backup emails
run_other_tool backup_mail "${IDS[@]}" "$DESC" || \
    fatal_error "Failed to backup email(s)!"

# Drop emails
for ID in "${IDS[@]}"; do
    run_postfix_command postsuper -d "$ID" || \
        log ERROR "Failed to delete email $ID"
done
