#! /usr/bin/python
# Copyright (C) 2017 Cumulus Networks, inc.
#
# All Rights reserved.
#
# This software is subject to the Cumulus Networks End User License Agreement available
# at the following locations:.
#
# Internet: https://cumulusnetworks.com/downloads/eula/latest/view/
#
# Cumulus Linux systems: /usr/share/cumulus/EULA.txt
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program - see the file COPYING.

"""
WARNING: netshow IS DEPRECATED.  USE net show, FROM THE NCLU PACKAGE, INSTEAD.

Usage:
    netshow access [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow bondmems [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow bonds [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow bridges [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow counters [errors] [all] [--json | -j | json]
    netshow interface [all|<interface>] [--mac | -m | mac] [--oneline | oneline | -1 | --json | -j | json]
    netshow l2 [all] [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow l3 [all] [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow lldp [--json | -j | json]
    netshow mgmt [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow system [--json | -j | json]
    netshow trunks [--mac | -m | mac] [--oneline | oneline | -1  | --json | -j | json]
    netshow vlan [all|<number-range-list>] [--json | -j | json]
    netshow help [access | bondmems | bonds | bridges | counters | help |interface | l2 | l3 | lldp | mgmt | system | trunks | vlan]
    netshow [version | --version | -V]
    netshow [--help | -h]

Help:
    * default is to show intefaces only in the UP state.
    access     :  summary of physical ports with l2 or l3 config
    all        :  show all ports include those are down or admin down
    bondmems   :  summary of bond members
    bonds      :  summary of bonds
    bridges    :  summary of ports with bridge members
    counters   :  summary of physical port counters.
    help       :  print usage or help for specific command
    -h         :  print usage
    --help     :  print usage
    interface  :  summary info of all interfaces
    json       :  print output in json
    l2         :  summary of access, trunk and bridge interfaces
    l3         :  summary of ports with an IP.
    lldp       :  physical device neighbor information
    mac        :  show inteface MAC in output
    mgmt       :  summary of mgmt ports
    oneline    :  output each entry on one line
    system     :  system information
    trunks     :  summary of trunk interfaces
    version    :  show netshow package version
    -V         :  show netshow version
    --version  :  show netshow version
    vlan       :  summary of VLANs

Legend:
    UP      Carrier up
    UN      Carrier Up - bond member not in bond
    DN      Carrier Down.
    ADMDN   Admin Down. Use "ip link set <interface> up " to initialize
    DRMNT   Carrier Up - Link Dormant
    P       Active Bond Member
    D       Inactive Bond Member
"""

from netshowlib import interface_related, check, \
    ShowInterfaces, CumulusShowInterfaces, \
    ShowNeighbors, CumulusShowNeighbors, \
    ShowCounters, ShowVlan, \
    ShowSystem, CumulusShowSystem

from network_docopt import NetworkDocopt, get_network_docopt_info
import logging
import sys
import pkg_resources

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)7s %(filename)12s: %(message)s')
log = logging.getLogger(__name__)

# Color the errors and warnings in red
logging.addLevelName(logging.ERROR, "\033[91m  %s\033[0m" % logging.getLevelName(logging.ERROR))
logging.addLevelName(logging.WARNING, "\033[91m%s\033[0m" % logging.getLevelName(logging.WARNING))

# network_docopt DEBUG level is very chatty, hardcode it to INFO
logging.getLogger('network_docopt').setLevel(logging.INFO)


(print_options, ended_with_space, sys.argv) = get_network_docopt_info(sys.argv)
cli = NetworkDocopt(__doc__)

if print_options:
    cli.print_options(ended_with_space)
else:
    cli.run()
    vendor = check()
    show = None

    # 'access', 'bridges', 'bonds', 'bondmems', 'mgmt', 'l2', 'l3', 'phy',
    # 'trunks', 'interface'
    if interface_related(cli):
        if vendor == 'cumulus':
            show = CumulusShowInterfaces(cli)
        else:
            show = ShowInterfaces(cli)

    elif cli.get('system'):
        if vendor == 'cumulus':
            show = CumulusShowSystem(cli)
        else:
            show = ShowSystem(cli)

    elif cli.get('lldp'):
        if vendor == 'cumulus':
            show = CumulusShowNeighbors(cli)
        else:
            show = ShowNeighbors(cli)

    elif cli.get('counters'):
        show = ShowCounters(cli)

    elif cli.get('vlan'):
        show = ShowVlan(cli)

    elif cli.get('version') or cli.get('-V') or cli.get('--version'):
        print('netshow version %s' % pkg_resources.require('netshow')[0].version)
        sys.exit(0)

    elif cli.get('help'):
        # help is special in that it prints help for keywords or just usage
        sys.exit(0)

    log.debug("vendor %s" % vendor)

    if show:
        print show.run()
    else:
        print "ERROR: could not determine what data to show"
