#!/usr/bin/env python

import getopt
import os
import sys
import isi.net.flexnet as flx

try:
    True
except NameError:
    True = 1
    False = 0

def usage(exitCode):
    sys.stderr.write(r'''
Usage: ''' + os.path.basename(sys.argv[0]) + r''' [OPTION]

Options:
  -h, --help                    Print this message and exit.
  -i, --interface               Run on this interface
''')
    sys.exit(exitCode)

def main():
    # parse options
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:',
                ['interface='])
        for o, a in opts:
            if o in ['-i', '--interface']:
                iface = a 
    except getopt.GetoptError:
        usage(1)

    if not len(opts):
        usage(1)

    try:
        # figure out the array.xml file to use
        print nic_load(iface)

    except EnvironmentError, e:
        error(str(e))

def nic_load(iface):
    try:
        nic = flx.nic_load(iface)
        return nic.name
    except Exception, e:
        return 'None' 

def error(str):
    print >> sys.stderr, os.path.basename(sys.argv[0]) + ':', str
    sys.exit(1)

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        error('%s' % e)
