#! /usr/bin/python3

# Module: vyatta-platform
# **** License ****
# Copyright (c) 2019-2020, AT&T Intellectual Property.  All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
# **** End License ****
#
# Detect platform and answer queries on platform-specific behaviour
#

import logging
import argparse
import sys
from vyatta.platform.detect import PlatformError, detect

LOG = logging.getLogger()
LOG.setLevel(logging.INFO)

def main(args):
    if args.debug:
        LOG.setLevel(logging.DEBUG)

    if args.query_is_switch:
        is_switch = False
        try:
            platform = detect()
            is_switch = platform.is_switch()
        except PlatformError as e:
            LOG.debug('determining switch gave ' + repr(e))
            pass

        sys.exit(0 if is_switch else 1)

    if args.query_is_hw_router_intf_supported:
        is_hw_router_intf_supported = False
        try:
            platform = detect()
            is_hw_router_intf_supported = platform.is_hw_router_interface_capable()
        except PlatformError as e:
            LOG.debug('determining HW router intf support gave ' + repr(e))
            pass

        sys.exit(0 if is_hw_router_intf_supported else 1)

    if args.configure_dataplane:
        try:
            platform = detect()
            platform.configure_dataplane(args.configure_dataplane)
        except PlatformError as e:
            LOG.debug('configuring dataplane gave ' + repr(e))
            pass

    if args.what_am_i:
        try:
            platform = detect()
            print(platform.get_platform_string())
        except PlatformError as e:
            LOG.debug('determining switch gave ' + repr(e))
            print('unknown')
            pass

    if args.am_i:
        try:
            platform = detect().get_platform_string()
        except PlatformError as e:
            LOG.debug('determining switch gave ' + repr(e))
            platform = "unknown"

        sys.exit(0 if platform == args.am_i else 1)

    if args.format_platform_state:
        try:
            platform = detect()
            platform.format_platform_state(args.format_platform_state)
        except PlatformError as e:
            LOG.debug('determining platform gave ' + repr(e))
            pass

    sys.exit(0)

if __name__ == '__main__':
    logging.basicConfig()
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument("--query-is-switch", action='store_true',
                        help="Query whether the platform defaults to a switch. If not, then it's a router")
    group.add_argument("--query-is-hw-router-intf-supported", action='store_true',
                        help="Query whether the platform supports router interfaces in the hardware")
    group.add_argument("--configure-dataplane", metavar='CONFFILE',
                        help="Configure the dataplane to make use of the platform")
    group.add_argument("--what-am-i", action='store_true',
                        help="Return a string that identifies the platform")
    group.add_argument("--am-i",
                       help="Return 0 if I identify as the given platform, else return 1")
    group.add_argument("--format-platform-state", metavar='COMMAND',
                        help="Format state specific for a platform for display to a user. State is passed via stdin")
    parser.add_argument("--debug", action='store_true',
                        help="Turn on debugs")
    args = parser.parse_args()
    main(args)
