#!/usr/bin/python

"""This module is intended to be enhanced to acheive the deployment goals
For a more comprehesive example of a deployment scenario,
 see doc/customDeployment/customDeployment.py
"""
__author__ = 'Todd C Davis'
__Product__ = 'Intel_PDK'

import string, os, os.path, sys, popen2, systemMgr
from logMgr import addLogRecord,CRITICAL,ERROR,WARNING,INFO,DEBUG
from config import DeploymentStore, DeployPath
try:
    __file__
except:
    __file__ = __name__

print "run customized deployment scripts here ..."
print "execute a series of scripts of your own making ..."

"""
print "running an example of capturing BIOS settings ..."

epcsutilFilePath = os.path.join(DeploymentStore,'epcs.txt')

status = 0
if sys.platform == 'win32':
    filePath = os.path.join(DeployPath,'bin','epcsutil')
    cmd = "python %s read %s"%(filePath,epcsutilFilePath)
    r, w, e = popen2.popen3('%s'%cmd,-1,'b')
    elines = e.read()
    e.close()
    lines = r.read()
    r.close()
    w.close()
    if elines:
        print "%s failed " % cmd + elines
        status = 1
    elif lines.find('FAILURE') >= 0 or lines.find('failed') >= 0:
        status = 1

elif sys.platform == 'linux2':
    cmd = "epcsutil read %s" % epcsutilFilePath
    cmdObj = popen2.Popen4('%s'%cmd)
    status = cmdObj.wait()
    lines = cmdObj.fromchild.read()

if status:
    addLogRecord(0,__file__, ERROR,'epcsutil failed: status = 0x%x, %s'%(status,lines))
else:
    addLogRecord(0,__file__, INFO,'BIOS settings saved to %s'%epcsutilFilePath)
"""
ret, value = systemMgr.queryProperty(0,'IntelBIOSID','BoardFamilyID','',{})
if value and ret == 0:
    if not value == 'S3000':
        
        print "running an example of capturing BIOS & BMC configuration ..."
        
        syscfgFilePath = os.path.join(DeploymentStore,'syscfg.scf')
        if os.path.exists(syscfgFilePath):
            os.remove(syscfgFilePath)
        
        if sys.platform == 'win32':
            filePath = os.path.join(DeployPath,'win32','syscfg')
        elif sys.platform == 'linux2':
            filePath = os.path.join(DeployPath,'Linux','syscfg')
        cwd = os.getcwd()
        os.chdir(filePath)
        status = 0
        cmd = "syscfg -s %s"%syscfgFilePath
        if sys.platform == 'win32':
            r, w, e = popen2.popen3('%s'%cmd,-1,'b')
            elines = e.read()
            e.close()
            lines = r.read()
            r.close()
            w.close()
            if elines:
                print elines
                status = 1
            elif lines.find('FAILURE') >= 0 or lines.find('failed') >= 0:
                status = 1
        
        elif sys.platform == 'linux2':
            cmdObj = popen2.Popen4('./%s'%cmd)
            status = cmdObj.wait()
            lines = cmdObj.fromchild.read()
    
        if status:
            addLogRecord(0,__file__, ERROR,'\"%s\" failed: status = 0x%x, %s'%(cmd,status,lines))
        else:
            addLogRecord(0,__file__, INFO,'BIOS and BMC settings saved to %s'%syscfgFilePath)
    
        print "running an example of setting the Power Restore Policy to 'OFF' ..."
    
        cmd = "syscfg /powerrestorepolicy OFF"
        if sys.platform == 'win32':
            r, w, e = popen2.popen3('%s'%cmd,-1,'b')
            elines = e.read()
            e.close()
            lines = r.read()
            r.close()
            w.close()
            if elines:
                print elines
                status = 1
            elif lines.find('FAILURE') >= 0 or lines.find('failed') >= 0:
                status = 1
        
        elif sys.platform == 'linux2':
            cmdObj = popen2.Popen4('./%s'%cmd)
            status = cmdObj.wait()
            lines = cmdObj.fromchild.read()
    
        if status:
            addLogRecord(0,__file__, ERROR,'\"%s\" failed: status = 0x%x, %s'%(cmd,status,lines))
        else:
            addLogRecord(0,__file__, INFO,"Power Restore Policy set to 'OFF'")


sys.exit(0)
