#!/usr/bin/env bash
set -Eeuo pipefail

exec &> /dev/stdout
echo "Starting $(date -Is)"

if [[ "$(find ~/mbsync/queued-email/ -type f -not -name 'index' | wc -l)" = 0 ]]; then
  echo 'No queued emails found. noop'
else
  # https://github.com/syl20bnr/spacemacs/issues/4504#issuecomment-232973179
  emacs -batch                                   \
        -l ~/.emacs.d/init.el                    \
        -l ~/annex/bin/misc/mail/notmuch.el      \
        --eval "(run-hooks 'emacs-startup-hook)" \
        --eval '(smtpmail-send-queued-mail)'
fi

echo "Finishing: $(date -Is)"
