#!/usr/bin/env python3

import pybib

import argparse
import logging

parser = argparse.ArgumentParser(description='Retrieve BibTeX information for Digital Object Identifiers (DOIs)')

parser.add_argument('-o', '--out', type=str, help='The filename to write the entries to')
parser.add_argument('-v', '--verbose', action='store_true', help='Print information while retrieving entries')

g = parser.add_mutually_exclusive_group(required=True)
g.add_argument('DOI', nargs='?', type=str, help='Retrieve an entry for a DOI')
g.add_argument('-f', '--file', nargs='?', type=str, help='Retrieve entries from a file of DOIs')

args = parser.parse_args()
verbose = args.verbose or args.out

logging.basicConfig(
    format='%(message)s',
    level=logging.INFO if verbose else logging.CRITICAL
)

driver = pybib.drivers.DXDoi()

if args.DOI:
    entry = driver.get_entry(args.DOI)
    print(entry)
elif args.file:
    pybib.convert_file(args.file, driver, args.out)
