#!/usr/bin/python
#-------------------------------------------------------------------------------
# # Copyright 2012 Cumulus Networks, LLC all rights reserved #
#-------------------------------------------------------------------------------
# 
# Determines the embedded platform and returns a string with the model number.
# Initially, this is pretty simple as we just look at the model number in the
# device-tree root; however, I expect that over time this expands to include
# looking at SEPROMs to get information regarding the model/version of a
# platform.
#

import os
import sys
import argparse
import subprocess

def get_model():
    model = None

    model = subprocess.check_output("/usr/lib/cumulus/cl-platform")

    return model

def get_revision(model):
    revision = None

    if model == 'dni,et-6448r':
        rev_file = '/sys/devices/e0005000.localbus/fa000000.cpld/board_revision'
        revision = open(rev_file, 'r').read().split('.')[0]

    return revision

def get_linux_cmdline():
    _cmdline_fp = open('/proc/cmdline', 'r')
    _cmdline = _cmdline_fp.readline()
    _cmdline_fp.close()

    return _cmdline

def get_switch_platform(model):
    if model == 'cumulus,cumulus_p2020':
        # has a PLX bridge chip -> line card
        if os.system('lspci -mm -n|grep -q \'"10b5" "8604"\'') == 0:
            return 'lc64x10'
        # has line card 2 eeprom
        # XXX - not implemented until we get hardware
#        if os.system('echo NOT IMPLEMENTED, consider using i2cdetect') == 0:
#            return 'lc16x40'
        # has any number of Trident+ chips -> fabric card
        elif os.system('lspci -mm -n|grep -q \'"14e4" "b846"\'') == 0:
            return 'fc1280'
        # Trident2
        elif os.system('lspci -mm -n|grep -q \'"14e4" "b850"\'') == 0:
            return 'trident2'

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=('Print platform-specific information.'))

    parser.add_argument('-a', '--all',
                        action='store_true',
                        help='print all information')
    parser.add_argument('-m', '--model',
                        action='store_true',
                        help='print board model (assumed when no options)')
    parser.add_argument('-r', '--revision',
                        action='store_true',
                        help='print board revision')
    parser.add_argument('-s', '--switch-platform',
                        action='store_true',
                        help='print switch platform')

    try:
        args = parser.parse_args()
    except Exception, e:
        parser.error(str(e))

    try:
        model = get_model()
    except:
        model = None

    try:
        revision = get_revision(model)
    except:
        revision = None

    try:
        switch_platform = get_switch_platform(model)
    except:
        switch_platform = None

    # Combine switch platform with CPU card model for platforms that have a
    # modular CPU card.
    if switch_platform is not None and model is not None:
        model = model + '_' + switch_platform

    if model is None:
        model = 'UNKNOWN'
    if revision is None:
        revision = 'UNKNOWN'
    if switch_platform is None:
        switch_platform = 'UNKNOWN'

    info = []
    if len(sys.argv) < 2 or args.all or args.model:
        info.append(model)
    if args.all or args.revision:
        info.append(revision)
    if args.all or args.switch_platform:
        info.append(switch_platform)

    sys.stdout.write(' '.join(info) + '\n')
