"""
Copyright 2014-2019 VMware, Inc.  All rights reserved. -- VMware Confidential

Dummy vmware-horizon-seamless-window component.
"""

class VMwareHorizonSeamlessWindow(Installer):

   def InitializeQuestions(self, old, new, upgrade):
      defaultSeamlessWindow = self.GetAnswer('seamlessEnable')
      if defaultSeamlessWindow:
         qlevel = 'CUSTOM'
      else:
         qlevel = 'REGULAR'

   def InitializeInstall(self, old, new, upgrade):
      self.AddTarget('File', 'lib/*', LIBDIR)
      self.SetPermission(LIBDIR/'vmware/libcrtbora.so', BINARY)
      self.SetPermission(LIBDIR/'vmware/libvmwarebase.so', BINARY)

      if self.IsOldGlibc():
         srcPath = "/usr/lib/vmware/view/crtbora/"
         desPath = "/usr/lib/vmware/"
         lib = ["libcairo.so.2", "libgthread-2.0.so.0"]
         if self.Is64Bit():
            # RedHat 6.9 is ugly, glib on it in 32bit and 64bit OS are different
            lib += ["libglib-2.0.so.0"]
         for f in lib:
            self.AddTarget('Link', srcPath + f, desPath + f)

      if (CONFDIR/'abortInstall').exists():
         raise InstallError('View abort install')

   def InitializeUninstall(self, old, new, upgrade):
      if (CONFDIR/'abortUninstall').exists():
         raise InstallError('View aborting uninstall')
      self.RunCommand('true', ignoreErrors=True)

   def PostInstall(self, old, new, upgrade):
      import shutil
      import os
      self.SetConfig('seamlessComponent', 'test')
      self.AddScanFile(LIBDIR/'vmware/libcrtbora.so')
      self.AddScanFile(LIBDIR/'vmware/libvmwarebase.so')

      # bug 2219490: Remove so files added in redhat patch in ubuntu
      if self.CheckPlatform() != "Red":
         lib4redhat = ["/usr/lib/vmware/libgio-2.0.so.0",
                       "/usr/lib/vmware/libgobject-2.0.so.0",
                       "/usr/lib/vmware/libglib-2.0.so.0",
                       "/usr/lib/vmware/libffi.so.5",
                       "/usr/lib/vmware/libgtk-x11-2.0.so.0",
                       "/usr/lib/vmware/libatk-1.0.so.0"]
         for f in lib4redhat:
            if os.path.exists(f):
               os.remove(f)
            else:
               continue
      if not self.IsOldGlibc():
         if os.path.exists("/usr/lib/vmware/view/crtbora"):
            shutil.rmtree("/usr/lib/vmware/view/crtbora")

   def PostUninstall(self, old, new, upgrade):
      self.DelConfig('seamlessComponent')
      self.DelConfig('seamlessComponent')

   def IsOldGlibc(self):
      import os
      result = ""
      if os.system("getconf GNU_LIBC_VERSION > /dev/null 2>&1"):
         return False
      else:
         result = os.popen("getconf GNU_LIBC_VERSION").read()
         result = (result.split(" ")[1].split("\n")[0])
         #glibc version in redhat 6.9 is 2.12
         return result <= '2.12'

   def Is64Bit(self):
      import platform
      return '64bit' == platform.architecture()[0]

   # Return the key word of platform
   def CheckPlatform(self):
      import os
      result = ""
      if os.path.exists('/etc/issue'):
         result = open('/etc/issue').read().split(' ')[0]
      return result

