#!/usr/bin/python

"""
    This module contains the deployment script to configure FW for an Intel system in WinPE and Linux
"""

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

def runState(restore=0):
    # call syscfg scripts here
    state = ATSTATE
    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':
        import amtcfg.amtcfg
        state = amtcfg.amtcfg.checkState(restore)
        if state < UPDATE:
            return state
        if state == REFRESH:
            for i in [1,2,3]:   # try 3 time to get to state
                state = amtcfg.amtcfg.amtConfig(restore)
                if state < UPDATE:
                    break
                os.system('prepareDeployment refresh')
            else:
                state = FAILURE
        if state < UPDATE:
            return state
    elif system in BMCfamilies:
        # call ipmiutil scripts here
        pass
    return state

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
    and flash data is located in:
        BootDevice/updates/S5000'
    """
    if len(sys.argv) == 1:
        restore = 0
    elif len(sys.argv) == 2 and sys.argv[1] == '0':
        restore = 0
    else:
        restore = 1
    sys.exit(runState(restore))
