#!/usr/bin/python

import sys

PLUGIN_PATH="/var/lib/llx-hw-support/"



def stage_boot():
	import os
	import xmlrpclib
	
	if(os.exists(PLUGIN_PATH+"/boot.d")==False):
		return

	login=""
	proxy = xmlrpclib.ServerProxy("https://localhost:9779")
	kernel_version=proxy.get_kernel_version(login,"Hardware")
	product_name=proxy.get_product_name(login,"Hardware")
	serial_number=proxy.get_serial_number(login,"Hardware")
	architecture=proxy.get_architecture(login,"Hardware")
	
	version=kernel_version.split(".")
	print(product_name)
	print(version)
	major_version=int(version[0])
	minor_version=int(version[1])
	
	files=os.listdir(PLUGIN_PATH+"/boot.d")
	files.sort()
	
	for f in files:
		cmd="{0} {1} {2} {3} {4} {5}".format(PLUGIN_PATH+"boot.d/"+f,product_name,serial_number,architecture,major_version,minor_version)
		os.system(cmd)
	
	#do something!
	
def stage_shutdown():
	import os
	import xmlrpclib
	
	if(os.exists(PLUGIN_PATH+"/shutdown.d")==False):
		return

	login=""
	proxy = xmlrpclib.ServerProxy("https://localhost:9779")
	kernel_version=proxy.get_kernel_version(login,"Hardware")
	product_name=proxy.get_product_name(login,"Hardware")
	serial_number=proxy.get_serial_number(login,"Hardware")
	architecture=proxy.get_architecture(login,"Hardware")
	
	version=kernel_version.split(".")
	print(product_name)
	print(version)
	major_version=int(version[0])
	minor_version=int(version[1])
	
	files=os.listdir(PLUGIN_PATH+"/shutdown.d")
	files.sort()
	
	for f in files:
		cmd="{0} {1} {2} {3} {4} {5}".format(PLUGIN_PATH+"shutdown.d/"+f,product_name,serial_number,architecture,major_version,minor_version)
		os.system(cmd)

	#do something!

def stage_xdg():
	import os
	import xmlrpclib
	
	if(os.exists(PLUGIN_PATH+"/xdg.d")==False):
		return

	
	#are we running on a X11 environment
	display=os.environ["DISPLAY"]
	
	if(display==None):
		print("No DISPLAY found")
		return 
	
	#guessing wherever are running on a light client
	x11address=display.split(":")

	if(x11address[0]==""):
		n4daddress="localhost"
	else:
		n4daddress=x11address[0]
		
	login=""
	proxy = xmlrpclib.ServerProxy("https://"+n4daddress+":9779")
	kernel_version=proxy.get_kernel_version(login,"Hardware")
	product_name=proxy.get_product_name(login,"Hardware")
	serial_number=proxy.get_serial_number(login,"Hardware")
	architecture=proxy.get_architecture(login,"Hardware")
	
	version=kernel_version.split(".")
	major_version=int(version[0])
	minor_version=int(version[1])
	
	files=os.listdir(PLUGIN_PATH+"/xdg.d")
	files.sort()
	
	#    SCRIPT_NAME PRODUCT SERIAL ARCH KERNEL_H KERNEL_L
	#Ex: 10-test Edge330 DF43SYG42 64bit 3 16
	for f in files:
		cmd="{0} {1} {2} {3} {4} {5}".format(PLUGIN_PATH+"xdg.d/"+f,product_name,serial_number,architecture,major_version,minor_version)
		os.system(cmd)



def print_usage():
	print("llx-hw-support STAGE")
	print("")
	print("Available commands:")
	print("boot\texecute the boot stage")
	print("xdg\texecute the xdg autostart stage")
	print("shutdown\texecute the shutdown stage")


def main():

	if(len(sys.argv)>1):
		if (sys.argv[1]=="--help"):
			print_usage()
		
		elif (sys.argv[1]=="boot"):
			stage_boot()
		elif (sys.argv[1]=="xdg"):
			stage_xdg()
		elif (sys.argv[1]=="shutdown"):
			stage_shutdown()
			
		else:
			print_usage()
	else:
		print_usage()
	


if __name__=="__main__":
	main()