#!/usr/bin/python3

import argparse

import run
from erimp import Erimp
from krikt import Krikt

desc = 'updates the krikts'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-v', action='store_true', help='raise verbosity')
parser.add_argument('-r', action='store_true', help='random order')
parser.add_argument('-n', action='store_true', help='newest first')
parser.add_argument('-c', action='store_true', help='check only, needs date')
parser.add_argument('-f', type=str, help='do this xml --> .json')
parser.add_argument('bapis', metavar='bnp', type=str, nargs='*',
                    help='bits and pieces arguments')
args = parser.parse_args()
do_verbose = args.v
e = Erimp(args.bapis, do_verbose=do_verbose)
k = Krikt(e)

if args.f is not None:
    k.run_file(args.f)
    quit()

# # for checks we need an issuedate
if args.c and e.given_issuedate is None:
    print("I need an issuedate for check.")
    quit()

if e.given_issuedate is None:
    if not args.n and not args.r:
        print("No issuedate ... I need either -r or -n.")
        quit()

if args.n and args.r:
    print("You can't give both -r and -n.")
    quit()

do_newest_first = False
if args.n:
    do_newest_first = True

# # make sure we don't run two processes if sorted
if do_newest_first:
    run.check()
k.renew(do_newest_first=do_newest_first, do_verbose=do_verbose,
        do_only_issuedate=e.given_issuedate, do_check=args.c)
