#!/usr/bin/python3

import argparse
import random
import time

import filing

from emailer import Emailer
from kapro import Kapro
from profile import Profile

desc = 'broacast to readers'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-b', action='store_true', help='build library only')
parser.add_argument('-v', action='store_true', help='do verbose')
parser.add_argument('-n', action='store_true', help='no send')
parser.add_argument('-m', type=int, help='max mails')
parser.add_argument('-s', type=int, help='sleep between mails')
parser.add_argument('empro', type=str, help='empro')
args = parser.parse_args()

kapro = Kapro(do_verbose=args.v)
profile = Profile(do_verbose=args.v)
emailer = Emailer(do_verbose=args.v)
empro = args.empro

# # better to this first
if(args.b):
    # # update the file library
    kapro.build_lib()
    quit()

# # use profile file library, rather than kapro
lib = kapro.load_lib()
random.shuffle(lib)
count_mails = 0
max_mails = args.m
count_sleep = args.s
for fufi in lib:
    if count_mails is not None:
        time.sleep(count_sleep)
    if max_mails is not None and count_mails >= max_mails:
        quit()
    count_mails += 1
    base = empro + '/' + profile.base_from_fufi(fufi)
    maix = filing.parse_lax(fufi)
    emailer.prepare(maix, base=base, empro='start_nitpo', dont_send=args.n)