#!/usr/bin/python

from phionlibs import CStdConfig
from phionlibs import SendEvent
import sys

_dodebug=0

def debugout(s):
	if _dodebug:
		print s
	
def parseopts():
	global _dodebug
	
	if len(sys.argv)>1 and ( sys.argv[1]=='-d' or sys.argv[1]=='--debug' ):
		_dodebug=1
	else:
		_dodebug=0
	
def main():
	parseopts()
	
	instconf=CStdConfig()
	st=instconf.LoadPath('/opt/phion/run/', 'appliance.state')
	if not st.ok():
		sys.__stderr__.write('Could not load appliance configuration - file appliance.state not found.\n')
		sys.exit(0)
	
	instmodel=instconf.Get('/', 'model')
	debugout('Appliance model auto detection resulted "%s"' % instmodel)
	
	boxconf=CStdConfig()
	st=boxconf.Load('box.conf')
	if not st.ok():
		sys.__stderr__.write('Could not load box configuration - file box.conf not found.\n')
		sys.exit(0)
	
	confmodel=boxconf.Get('/', 'appliance')
	createevent=boxconf.Get('/', 'detectapplmm')
	debugout('Configured appliance model is "%s"' % confmodel)
	
	if confmodel != instmodel:
		debugout('Found appliance model mismatch! Configured is "%s" whereas auto detection resulted "%s"' % (confmodel, instmodel))
		if createevent=='y':
			SendEvent(2, 'Box', 2, 'Appliance detection', 34, 'Appliance model configuration mismatch', 'Configured is "%s" whereas auto detection resulted "%s"' % (confmodel, instmodel))
			debugout('Create event 34!')
		else:
			debugout('Eventing appliace model mismatch is configuratively disabled.')



if __name__ == '__main__':
	main()

