#!/usr/bin/python
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:tw=0

  #############################################################################
  #
  # Copyright (c) 2005 Dell Computer Corporation
  # Dual Licenced under GNU GPL and OSL
  #
  #############################################################################
"""inventory_firmware:

usage:
    -h | --help         print this message
    -c | --config       Location of an additional config file to import
    -b | --bootstrap    run inventory in bootstrap mode
"""

from __future__ import generators

# import arranged alphabetically
import getopt
import os
import sys
import ConfigParser

# our modules
import firmwaretools.clifuncs
import firmwaretools.trace_decorator as trace_decorator

class CmdlineError(Exception):pass

def main():
    global firmwaretools # so we can import more submodules below...
    mode = "inventory"

    ini = ConfigParser.ConfigParser()
    altConfig = 0
    overrides = []
    commaSeparated=0
    verbose=0

    try:
        opts, args = getopt.getopt(sys.argv[1:], "bc:fho:uv", ["help", "config=", "bootstrap", "overrides=", "up2date_mode", "verbose", "fake-mode"])
        for option, argument in opts:
            if option in ("-h", "--help"):
                print __doc__
                sys.exit(0)
            if option in ("-c", "--config"):
                firmwaretools.clifuncs.getConfig(ini, [argument,])
                altConfig = 1
            if option in ("-b", "--bootstrap"):
                mode = "bootstrap"
            if option in ("-u", "--up2date_mode"):
                commaSeparated=1
            if option in ("-o", "--override"):
                overrides.append(argument)
            if option in ("-v", "--verbose",):
                verbose = verbose + 1
                trace_decorator.debug["__main__"] = verbose
            if option in ("-f", "--fake-mode"):
                os.environ['DEBUG_REPOSITORY'] = "1"
                os.environ['DEBUG_INVENTORY'] = "1"
                import firmwaretools.mockrepository
                import firmwaretools.mockpackage

        # load standard configuration
        if not altConfig:
            firmwaretools.clifuncs.getConfig(ini, firmwaretools.clifuncs.configLocations)

        for over in overrides:
            section, key, value = over.split(",", 2)
            if not ini.has_section(section):
                ini.add_section(section)
            ini.set(section, key, value)

        if mode == "bootstrap":
            os.execvp("bootstrap_firmware", sys.argv)
        else:
            for pkg in firmwaretools.clifuncs.runInventory(ini):
                print "%s = %s" % (str(pkg), pkg.version)

    except CmdlineError, e:
        print
        print e
        print
        print __doc__
        sys.exit(2)

    except (getopt.GetoptError):
        # print help information and exit:
        print __doc__
        sys.exit(2)

    return 0 #shell logic


if __name__ == "__main__":
    sys.exit( main() )

