#!/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="[sender regex]"
SENDER_REGEX=
# shellcheck disable=SC2317,SC2329
function handle_extra_args() {
    SENDER_REGEX="$1"
}
handle_args "$@"
[[ -z "$SENDER_REGEX" ]] && usage

mapfile -t IDS < <( JUST_TRY=0 run_other_tool mail_from_sender "$SENDER_REGEX" )
if [[ ${#IDS[@]} -eq 0 ]]; then
    info "No email found with sender pattern '$SENDER_REGEX'"
    exit 0
fi
[[ ${#IDS[@]} -gt 0 ]] && run_other_tool drop_mail "${IDS[@]}" "from-$SENDER_REGEX"
