#!/usr/bin/env python3

import xmlrpc.client
import ssl
import n4d.client
import syslog
import time
import sys

#import imp # Domentado por raul
from importlib.machinery import SourceFileLoader
#

DEBUG=True

def dprint(data):
	
	if DEBUG:
		syslog.syslog("[LliureXRemoteInstallerClientUpdate] %s"%data)
		
#def dprint

def check_root():
	
	try:
		f=open("/run/lliurex-remote-installer-client-update","w")
		f.close()
		return True
	except:
		return False
		
#def check_root		


class LliurexRemoteSystemd:
	
	def __init__(self):
	
		#RIC=imp.load_source("LliureXRemoteInstallerClient","/usr/share/n4d/python-plugins/LliurexRemoteInstallerClient.py")
		class_ = SourceFileLoader("LliureXRemoteInstallerClient_n4d", "/usr/share/n4d/python-plugins/LliurexRemoteInstallerClient.py").load_module()
		self.lri=getattr(class_,"LliureXRemoteInstallerClient")()
		
	#def init
	
	def run(self):
		
		#c=xmlrpclib.ServerProxy("https://localhost:9779")
		# wait until n4d local is active
		c=n4d.client.Client("https://localhost:9779")
		ok=False
		
		for i in range(0,10):
			try:
				c.get_methods()
				ok=True
				break
			except:
				time.sleep(2)


		# wait until n4d server connection is active
		c=n4d.client.Client("https://server:9779")
		ok=False
		
		for i in range(0,10):
			try:
				c.get_methods()
				ok=True
				break
			except:
				time.sleep(2)
	
		if not ok:
			return
	
		try:
			dprint("Init LRIC")
			self.lri.test_system()
		except Exception as e:
			dprint("Error: [%s]"%e)

	#def run
	
#class LRS


if __name__=="__main__":

	if check_root:
		try:
			dprint("Init LRIC")
			lrs=LliurexRemoteSystemd()
			lrs.run()
		except Exception as e:
			dprint("Something went wrong:\n\t%s"%e)
			
	else:
		print("[!] You need root privileges to run this program [!]")
