#!python

import os
import sys
import argparse

try:
    import OuiLookup
except:
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    import OuiLookup


parser = argparse.ArgumentParser(
    description='{} v{}'.format(OuiLookup.NAME, OuiLookup.VERSION),
    add_help=True,
    epilog="""
        A CLI tool for interfacing with the OuiLookup module that provides CLI access the query(), update() and status() 
        functions.  Outputs at the CLI are JSON formatted thus allowing for easy chaining to other toolchains. The 
        update() function updates directly from "standards-oui.ieee.org" and the ouilookup package provides an 
        internal fallback oui.txt updated at time of packaging.
    """,
)

parser_group0 = parser.add_mutually_exclusive_group()
parser_group0.add_argument(
    '-q',
    '--query',
    required=False,
    default=False,
    type=str,
    metavar='<hwaddr>',
    help='Query to locate matching MAC hardware address(es) from the oui database.  Addresses may be expressed in '
         'formats with or without ":" or "-" separators.  Use a space between addresses for more than one lookup in a '
         'single query line.'
)
parser_group0.add_argument(
    '-u',
    '--update',
    required=False,
    default=False,
    action='store_true',
    help='Download the latest oui.txt from "standards-oui.ieee.org" and parse it to generate a local oui.json for use '
         'by OuiLookup. The following paths (in order) are examined for write-access to save the oui.json data-file: '
         '{}'.format(', '.join(OuiLookup.DATA_PATH_DEFAULTS))
)
parser_group0.add_argument(
    '-un',
    '--update-no-download',
    required=False,
    default=False,
    action='store_true',
    help='Parse the oui.txt file in the data-path and update the local oui.json data-file without downloading the '
         'latest oui.txt update from "standards-oui.ieee.org".'
)
parser_group0.add_argument(
    '-s',
    '--status',
    required=False,
    default=False,
    action='store_true',
    help='Return status information about the oui.json data-file available to OuiLookup.'
)

parser_group1 = parser.add_argument_group()
parser_group1.add_argument(
    '-d',
    '--debug',
    required=False,
    default=False,
    action='store_true',
    help='Provide debug logging output to stderr.'
)

args = parser.parse_args()

if args.debug: logger_level = 'debug'
else: logger_level = OuiLookup.LOGGER_LEVEL_DEFAULT

if __name__ == '__main__':
    ouilookup = OuiLookup.OuiLookup(logger_level=logger_level)
    if args.update is True:
        OuiLookup.out(ouilookup.update(), sort_keys=True)

    elif args.update_no_download is True:
        OuiLookup.out(ouilookup.update(skip_download=True), sort_keys=True)

    elif args.status is True:
        OuiLookup.out(ouilookup.status(), sort_keys=True)

    elif args.query is not False:
        OuiLookup.out(ouilookup.query(expression=args.query))

    else:
        parser.print_help()
        exit(1)
