import urllib, os, sys, socket, _winreg, string, psyco

psyco.full()

# Change the user agent to SpySweeper to avoid detection
class AppURLopener(urllib.FancyURLopener):
   def __init__(self, *args):
       self.version = "SpySweeper"
       urllib.FancyURLopener.__init__(self, *args)

urllib._urlopener = AppURLopener()


# Set a few defaults
default_path = "C:\\Program Files\\Webroot\\Spy Sweeper"
host = "www.webroot.com"
force = False


# Process command line arguments
if len(sys.argv) > 0:
   for x in sys.argv:
       x = string.lower(x)
       if x == "-f" or x == "-force":
           force = True
       elif x == string.lower(sys.argv[0]):
           pass
       else:
           print "\nInvalid command line argument,", x
           os._exit(1)


# Set up screen
   os.system('cls')
   print "Updating Webroot SpySweeper\n"


# Get SpySweeper location
try:
   hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Software\Webroot\SpySweeper')
   ssID = _winreg.QueryValueEx(hkey, 'id')
   ssPath = ssID[0]
except WindowsError, (errno, strerror):
   if errno == 2:
       if not os.path.exists(default_path + "\\SpySweeper.exe"):
           print "Error: SpySweeper not found in registry"
           sys.exit(1)
       else:
           ssPath = default_path
   else:
       print "Error: ", (errno, strerror)
       sys.exit(1)


# Verify that masters.mst is where it's supposed to be.
mstPath = ssPath + '\\masters\\'
if not os.path.exists(mstPath + 'masters.mst') and not force:
   print "Error: SpySweeper definitions not found!"
   os._exit(1)

# Get installed mst version number
try:
   if not os.path.exists(ssPath + '\\Version\\version.ver'):
       print "Unable to determine verion! Please reinstall SpySweeper."
       sys.exit(1)
   else:
       f = file(ssPath + '\\Version\\version.ver', 'r')
       current_version = string.strip(f.read())
       print "Installed mst version ......", current_version
       f.close()
except WindowsError, (errno, strerror):
   if errno == 2:
       print "Error: SpySweeper not found in registry"
       sys.exit(1)
   else:
       print "Error: ", (errno, strerror)
       exit(1)

try:
# Connect to webroot server
   print "Server's mst version .......",
   response = urllib.urlopen("http://www.webroot.com/php/disp0201.php?oc=49&pc=64000")
   data = response.read()
   server_version = data
   print data

# Download if newer or if forced
   if data > current_version or force:
       print "Downloading update ......... ",
       response = urllib.urlopen("http://www.webroot.com/php/disp0201.php?pc=64000&oc=50")
       length = int(response.info()["Content-Length"])
       amt = (length/10) + 10
       data = ""
       while True:
           print '%s' % (chr(8) + '#'),
           dl = response.read(amt)
           if dl == "":
               break
           data += dl
       print chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+chr(8)+"OK           "
       f = file(ssPath + '\\version\\version.ver', 'w')
       f.write(server_version)
       f.close()
   else:
       print "\nLatest update already installed"
       os._exit(0)

   if os.path.exists(mstPath + 'masters.mst'):
       print "Backing up old file ........",
       if os.path.exists(mstPath + 'masters.bak'):
           os.unlink(mstPath + 'masters.bak')
       os.rename(mstPath + 'masters.mst', mstPath + 'masters.bak')
       print "OK"
   print "Updating masters file ......",
   f = file(mstPath + 'masters.mst', 'wb')
   f.write(data)
   f.close()
   print "OK"
   print "\nSpySweeper successfuly updated"

except KeyboardInterrupt:
   print "\n\nCanceled by user"
   sys.exit(1)
except socket.gaierror:
   print "Unable to resolve host"
   sys.exit(1)
except socket.error:
   print "Unable to connect:", sys.exc_info()[1]
   sys.exit(1)
except:
   print "Unexpected error:", sys.exc_info()
   raise