#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
import os
from subprocess import PIPE, Popen
import shutil
import ctypes
import re
import datetime
import platform
import subprocess
import socket

try:
    import winreg as _winreg
except ImportError:
    try:
        import _winreg
    except ImportError:
        pass
import ssl
import time

try:
    import urllib.request as urllib2
except ImportError:
    try:
        import urllib2
    except ImportError:
        pass

def GetWindowsEdition(Key_name):
	val = ""
	try:
		reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
		ok = _winreg.OpenKey(reg, Key_name, 0, _winreg.KEY_WOW64_32KEY | _winreg.KEY_READ)
		val = _winreg.QueryValueEx(ok, "ProductName")[0]
		_winreg.CloseKey(ok)
		return val
	except Exception as exception:
		val = "Windows Registry Exception: " + str(exception)
		return val

WindowsVersion = GetWindowsEdition('SOFTWARE\Microsoft\Windows NT\CurrentVersion')

class disable_file_system_redirection:
		try:
			_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
			_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
			def __enter__(self):
				self.old_value = ctypes.c_long()
				self.success = self._disable(ctypes.byref(self.old_value))
			def __exit__(self, type, value, traceback):
				if self.success:
					self._revert(self.old_value)
		except:
			pass

def ecmd(command, output=False):
	if not ("XP" in WindowsVersion or "2008" in WindowsVersion):
		with disable_file_system_redirection():
			objt = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
			out, err = objt.communicate()
			print out,err
			ret = objt.returncode
	else:
		objt = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
		out, err = objt.communicate()
		ret = objt.returncode
		print out,err
	if not out:
		return ret
	else:
		return '%s\n%s' % (out, err)

def WebrootUninstall():
	OsPath = r"C:\Program Files (x86)"
	Webrootpath1 = r"{0}\Program Files (x86)\Webroot\WRSA.exe".format(os.environ['systemdrive'])
	Webrootpath2 = r"{0}\Program Files\Webroot\WRSA.exe".format(os.environ['systemdrive'])
	Webrootcommand=""
	WebrootResultReboot = ""
	# If system is 64 bit
	if os.path.exists(Webrootpath1):
		if os.path.exists(Webrootpath1):
			Webrootcommand='"C:\Program Files (x86)\Webroot\WRSA.exe" -uninstall'
			WebrootUninstResult = ecmd(Webrootcommand, True)
			if int(WebrootUninstResult)==0:
				print("Webroot uninstall command was send successful")
				if WebrootReboot is "yes":
					WebrootResultReboot = "yes"
			else:
				print(str(WebrootUninstResult))
		else:
			print("Path was not found - " + Webrootpath1 )
	else:
		if os.path.exists(Webrootpath2):
			Webrootcommand='"C:\Program Files\Webroot\WRSA.exe" -uninstall'
			WebrootUninstResult = ecmd(Webrootcommand, True)
			if int(WebrootUninstResult)==0:
				print("Webroot uninstall command was send successful")
				if WebrootReboot is "yes":
					WebrootResultReboot = "yes"
			else:
				print(str(WebrootUninstResult))
		else:
			print("Path was not found - " + Webrootpath2 )
	if WebrootResultReboot is "yes":
		print(ecmd(r'shutdown.exe -r -t ' + reboottime + ' /f /c "' + rebootcomment + '"', True))
		print('System will restart in ' + str(t1) + ' minutes')
		
WebrootUninstall()
