#!   /usr/bin/env   python
#-*-python-*-

# Copyright CERN, 2011, 2012
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Modified and broken by Alessandro Rubini, still learning python

# Import system modules
import sys
import getopt
import time
import datetime
import os

sys.path.insert(0, '/usr/libexec/fmc/')

from libipmi.fmc_eeprom import *


"""
Creates a FRU binary file to be written into FMC EEPROM

"""

def main (argv0, argv):

    # Defaults
    FRU_VENDOR = "fmc-example"
    FRU_NAME = "mezzanine"
    FRU_SERIAL = "0001"
    FRU_PART = "sample-part"
    FRU_OUTPUT = "/dev/stdout"
    verbose = 0

    # Override defaults with environment variables
    try:
        FRU_VENDOR = os.environ['FRU_VENDOR']
    except:
        pass
    try:
        FRU_NAME = os.environ['FRU_NAME']
    except:
        pass
    try:
        FRU_SERIAL = os.environ['FRU_SERIAL']
    except:
        pass
    try:
        FRU_PART = os.environ['FRU_PART']
    except:
        pass
    try:
        FRU_OUTPUT = os.environ['FRU_OUTPUT']
    except:
        pass
    if os.getenv("FRU_VERBOSE") is not None:
        verbose = 1

    # Override defaults with command line arguments
    try:
        opts, args = getopt.getopt(argv,"v:n:s:p:o:",["--help"])
    except getopt.GetoptError:
        print "fru-generator: wrong arguments"
        sys.exit(2)
    for opt, arg in opts:
        if opt == "--help":
            print "fru-generator: no help yet"
            sys.exit(1)
        if opt == '-v':
            FRU_VENDOR = arg
        if opt == '-n':
            FRU_NAME = arg
        if opt == '-s':
            FRU_SERIAL = arg
        if opt == '-p':
            FRU_PART = arg
        if opt == '-o':
            FRU_OUTPUT = arg

    if verbose:
        print "VENDOR = " + FRU_VENDOR
        print "NAME = " + FRU_NAME
        print "SERIAL = " + FRU_SERIAL
        print "PART = " + FRU_PART
        print "OUTPUT = " + FRU_OUTPUT

    #==================================================
    # Calculate number of minutes since 0:00 1/1/96
    now_date = datetime.datetime.now()
    ref_date = datetime.datetime(1996, 1, 1)
    diff_date = now_date - ref_date
    total_seconds = diff_date.days * 86400 + diff_date.seconds
    current_date = int(total_seconds//60)
    mfg_date = current_date

    #==================================================
    # Create Board Info Area
    # FRU field is used to store the date of generation of the eeprom content
    # This could be used later to determine if the content has to be udated (bug fix, ...)
    fru = "%s" % now_date
    bia = BoardInfoArea(mfg_date, FRU_VENDOR, FRU_NAME, FRU_SERIAL, FRU_PART, fru)

    #==================================================
    # Multirecords Area

    # output number, vnom, vmin, vmax, ripple, imin, imax
    dcload0 = DCLoadRecord(0, 2.5, 2.375, 2.625, 0.0, 0, 4000) # VADJ
    dcload1 = DCLoadRecord(1, 3.3, 3.135, 3.465, 0.0, 0, 3000) # P3V3
    dcload2 = DCLoadRecord(2, 12.0, 11.4, 12.6, 0.0, 0, 1000)  # P12V
    dcload = [ dcload0, dcload1, dcload2 ]

    # output number, vnom, vmin, vmax, ripple, imin, imax
    dcout0 = DCOutputRecord(3, 0.0, 0.0, 0.0, 0.0, 0, 0) # VIO_B_M2C
    dcout1 = DCOutputRecord(4, 0.0, 0.0, 0.0, 0.0, 0, 0) # VREF_A_M2C
    dcout2 = DCOutputRecord(5, 0.0, 0.0, 0.0, 0.0, 0, 0) # VREF_B_M2C
    dcout = [ dcout0, dcout1, dcout2 ]

    # module size  : 0=single width, 1=double width
    # P1 size      : 0=LPC, 1=HPC
    # P2 size      : 0=LPC, 1=HPC, 3=not fitted
    # clock dir    : 0=M2C, 1=C2M
    # nb sig P1 A  : number
    # nb sig P1 B  : number
    # nb sig P2 A  : number
    # nb sig P2 B  : number
    # nb GBT P1    : number
    # nb GBT P2    : number
    # max TCK freq : frequency in MHz
    oem = OEMRecord(0, 1, 3, 1, 68, 0, 0, 0, 0, 0, 0)

    #==================================================
    # Write eeprom content to a binary file
    ipmi_open_file(FRU_OUTPUT)
    #ipmi_set(bia, dcload, dcout, oem, iua)
    ipmi_set(bia, dcload, dcout, oem)
    ipmi_write()
    ipmi_close_file()



if __name__ == '__main__' :
    main(sys.argv[0], sys.argv[1:])
