#! /usr/bin/python3
import os
import sys
import argparse

parser = argparse.ArgumentParser(
        description='''
            Display desktop notifications for unread mail in notmuch database.
            ''')
parser.set_defaults(print_summary=False)
parser.add_argument('--print-summary', dest='print_summary',
                    action='store_true',
                    help="""
                    Just print the query summary to stdout, without recency
                    tracking.
                    """)
args = parser.parse_args()

if args.print_summary:
    from notifymuch.messages import Messages
    from notifymuch import config
    config.load()
    try:
        summary = Messages().summary()
    except Messages.NoConfigError as e:
        print('Unable to fetch notmuch messages: %s' % e, file=sys.stderr)
        print('Is notmuch configured correctly?', file=sys.stderr)
        sys.exit(1)
    if summary:
        print(summary)
else:
    # Background ourselves for consistent behaviour, because show_notification
    # may or may not block.
    pid = os.fork()
    if pid > 0:
        os._exit(0)

    from notifymuch.notification import show_notification
    show_notification()
