#!/usr/bin/python

"""
    This module contains the deployment script to update the FRU/SDR in WinPE
"""

import string, os, os.path, shutil, sys, popen2, systemMgr
from logMgr import addLogRecord,CRITICAL,ERROR,WARNING,INFO,DEBUG
from config import DeploymentStore, DeployPath
from deploymentData import ATSTATE,FAILURE,UPDATE,REFRESH,REBOOT,UNSUPPORTED
import time
try:
    __file__
except:
    __file__ = __name__

def checkState(restore=0):
    """
        checkState() returns FAILURE if state can't be achieved
        checkState() returns REFRESH if system info refresh is needed once configured
    """
    if not sys.platform == 'win32':
        print 'frusdr is intended for WinPE systems only'
        addLogRecord(0,__file__, INFO,'frusdr is intended for WinPE systems only')
        return FAILURE
    ret, system = systemMgr.queryProperty(0,'IntelBIOSID','BoardFamilyID','',{})
    if not system or ret:
        print 'Intel board family unavailable: ret = %d'%ret
        addLogRecord(0,__file__, ERROR,'Intel board family unavailable: ret = %d'%ret)
        return FAILURE
    if system == 'S3000':
        print 'no BMC on S3000 systems'
        addLogRecord(0,__file__, INFO,'no BMC on S3000 systems')
        return FAILURE
    ret, BMCver = systemMgr.queryProperty(0,'SDR','BMC version','',{})
    if not BMCver or ret:
        addLogRecord(0,__file__, ERROR,'BMC version unavailable: ret = %d'%ret)
        return FAILURE
    from flash import getChassisSpec
    chassis, chassisSpec = getChassisSpec(restore)
    if not chassisSpec:
        return FAILURE
    # No ATSTATE return, always flash once
    return REFRESH


def frusdrUpdate(restore=0):
    state = checkState(restore)
    if state < UPDATE:
        return state
    from flash import getChassisSpec,extractUpdate
    chassis, chassisSpec = getChassisSpec(restore)
    if not chassisSpec:
        return FAILURE
    filePath = extractUpdate(restore)
    if not filePath:
        return FAILURE
    cfgfile = os.path.join(filePath,'MASTER.CFG')
    
    print "running an example of updating the FRU/SDR using frusdr ..."
    
    from config import getstatusoutput
    flashdir = os.path.join(DeployPath,'win32','frusdr')
    cwd = os.getcwd()
    os.chdir(flashdir)
    #os.system("win2kinstall %s *IntelUtilDeV > nul"%os.path.join(flashdir,'ibsmutil.inf'))
    status, output = getstatusoutput("frusdr /cfg %s"%cfgfile)
    #os.system("win2kuninstall *IntelUtilDeV > nul")
    os.chdir(cwd)
    if status:
        print 'frusdr failed: status = 0x%x, %s'%(status,output)
        addLogRecord(0,__file__, ERROR,'frusdr failed: status = 0x%x, %s'%(status,output))
        return status
    return REFRESH

if __name__ == '__main__':
    """
    this script assumed these enviroment variables have been set:
    (see deploy.sh or deploy.cmd)
        DeployPath
        DeploymentStore
        PATH
        LD_LIBRARY_PATH (Linux)
        PYTHONPATH
    and this script has been run:
        prepareDeployment
    """
    import time
    if len(sys.argv) == 1:
        restore = 0
    elif len(sys.argv) == 2 and sys.argv[1] == '0':
        restore = 0
    else:
        restore = 1
    state = frusdrUpdate(restore)
    if state == REFRESH:
        print 'FRU/SDR update succeeded'
        addLogRecord(0,__file__, INFO,'FRU/SDR update succeeded')
        state = ATSTATE
    sys.exit(state)
