#!/usr/bin/env bash

set -eu
set -o pipefail

help_main()
{
    echo "\
Usage: mail COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   inbox
   neomutt
   pull

Report bugs to: go.wigust@gmail.com."
}

help_neomutt()
{
    echo "\
Usage: mail neomutt [OPTION] MAIL_ADDRESS...
Run neomutt with MAIL_ADDRESS configuration.

  -A                     list available mail addresses
  -h, --help             display this help and exit

Report bugs to: go.wigust@gmail.com."
}

help_inbox()
{
    echo "\
Usage: mail inbox [OPTION] MAIL_ADDRESS...
Run curl with MAIL_ADDRESS configuration.

  -A                     list available mail addresses
  -h, --help             display this help and exit

Report bugs to: go.wigust@gmail.com."
}

mail_addresses=(pyhalov@majordomo.ru
                asfjsdf@mail.ru
		rxaetbed@mail.ru
                houdinihar@yandex.ru
                jenkins@majordomo.ru
                i18n.wugi@yandex.ru
                zabbix.guix@yandex.ru
                oleg@wugi.info
                oleg-windows@wugi.info
                localhost)

case "$1" in
    pull)
        mbsync -a
        notmuch new
        ;;
    --help)
        help_main
        ;;
    inbox)
        case "$2" in
            pyhalov@majordomo.ru)
                exec -a "$0" curl                                                                               \
                    --silent                                                                                    \
                    --request "EXAMINE ${3:-INBOX}"                                                             \
                    --user "pyhalov@majordomo.ru:$(pass show majordomo/private/newmail.majordomo.ru/pyhalov@majordomo.ru)"        \
                    imaps://imap.majordomo.ru:993
                ;;
            asfjsdf@mail.ru)
                exec -a "$0" curl                                               \
                    --silent                                                    \
                    --request "EXAMINE ${3:-INBOX}"                             \
                    --user "asfjsdf@mail.ru:$(pass show mail.ru/asfjsdf)"       \
                    imaps://imap.mail.ru:993
                ;;
            rxaetbed@mail.ru)
                exec -a "$0" curl							\
                    --silent								\
                    --request "EXAMINE ${3:-INBOX}"					\
                    --user "rxaetbed@mail.ru:$(pass show mail.ru/rxaetbed@mail.ru)"	\
                    imaps://imap.mail.ru:993
                ;;
            houdinihar@yandex.ru)
                exec -a "$0" curl                                               \
                    --silent                                                    \
                    --request "EXAMINE ${3:-INBOX}"                             \
                    --user "houdinihar:$(pass show email/yandex.ru/houdinihar)" \
                    imaps://imap.yandex.ru:993
                ;;
            jenkins@majordomo.ru)
                exec -a "$0" curl                                                               \
                    --silent                                                                    \
                    --request "EXAMINE ${3:-INBOX}"                                             \
                    --user "jenkins@majordomo.ru:$(pass show majordomo/public/jenkins.intr/admin)"     \
                    imaps://imap.majordomo.ru:993
                ;;
            i18n.wugi@yandex.ru)
                exec -a "$0" curl                                                       \
                    --silent                                                            \
                    --request "EXAMINE ${3:-INBOX}"                                     \
                    --user "i18n.wugi:$(pass show email/yandex.ru/i18n.wugi)"           \
                    imaps://imap.yandex.ru:993
                ;;
            zabbix.guix@yandex.ru)
                exec -a "$0" curl                                                       \
                    --silent                                                            \
                    --request "EXAMINE ${3:-INBOX}"                                     \
                    --user "zabbix.guix:$(pass show email/yandex.ru/zabbix.guix)"       \
                    imaps://imap.yandex.ru:993
                ;;
            oleg@wugi.info)
                exec -a "$0" curl                                       \
                    --silent                                            \
                    --request "EXAMINE ${3:-INBOX}"                     \
                    --user "oleg:$(pass show vm1.wugi.info/oleg)"       \
                    imaps://imap.wugi.info:993
                ;;
            oleg-windows@wugi.info)
                exec -a "$0" curl                                               \
                    --silent                                                    \
                    --request "EXAMINE ${3:-INBOX}"                             \
                    --user "oleg:$(pass show vm1.wugi.info/oleg-windows)"       \
                    imaps://imap.wugi.info:993
                ;;
            localhost)
                exec -a "$0" curl                               \
                    --silent                                    \
                    --request "EXAMINE ${3:-INBOX}"             \
                    --user "oleg:$(pass localhost/imap/oleg)"   \
                    imap://localhost
                ;;
            -A)
                printf "%s\n" "${mail_addresses[@]}"
                ;;
            ll)
                mail inbox -A | while IFS= read -r address
                do
                    echo "$address"
                    mail inbox "$address"
                done
                ;;
            *)
                help_inbox
                ;;
        esac
        ;;
    neomutt)
        case "$2" in
            pyhalov@majordomo.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_pyhalov_majordomo"
                ;;
            asfjsdf@mail.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_asfjsdf_mail"
                ;;
            rxaetbed@mail.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_rxaetbed_mail"
                ;;
            houdinihar@yandex.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_houdinihar_yandex"
                ;;
            jenkins@majordomo.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_jenkins_majordomo"
                ;;
            i18n.wugi@yandex.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_i18n.wugi_yandex"
                ;;
            zabbix.guix@yandex.ru)
                exec -a "$0" neomutt -F "$HOME/.muttrc_zabbix.guix_yandex"
                ;;
            localhost)
                exec -a "$0" neomutt -F "$HOME/.muttrc_localhost"
                ;;
            -A)
                printf "%s\n" "${mail_addresses[@]}"
                ;;
            *)
                help_neomutt
                ;;
        esac
        ;;
    *)
        help_main
        ;;
esac
