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

Dummy client component.
"""

class VMwareHorizonClient(Installer):
   def PreTransactionInstall(self, old, new, upgrade):
      iconImages = ['extras/artwork/linux_view_%dx.png' % size for size in [16, 32, 48, 82, 128]]
      gui.SetHeaderImage('extras/artwork/linux_view_82x.png')
      gui.SetIconImages(iconImages)
      gui.SetBannerImage('extras/artwork/linux-installer-card.png')

   def InitializeInstall(self, old, new, upgrade):
      self.AddTarget(
         'File',
         'bin/vmware-view-log-collector',
         BINDIR/'vmware-view-log-collector')
      self.AddTarget(
         'File',
         'lib/vmware/view/dct/*',
         '/usr/lib/vmware/view/dct/')
      self.AddTarget(
         'File',
         'lib/vmware/view/bin/vmware-view',
         '/usr/lib/vmware/view/bin/vmware-view')
      self.AddTarget(
         'File',
         'lib/vmware/view/bin/vmware-view-legacy',
         '/usr/lib/vmware/view/bin/vmware-view-legacy')
      self.AddTarget(
         'File',
         'lib/vmware/view/env/env_utils.sh',
         '/usr/lib/vmware/view/env/env_utils.sh')
      self.AddTarget(
         'File',
         'lib/vmware/view/env/vmware-view.info',
         '/usr/lib/vmware/view/env/vmware-view.info')
      self.AddTarget('File', 'bin/vmware-view', BINDIR/'vmware-view')
      self.AddTarget('File', 'bin/vmware-view-legacy', BINDIR/'vmware-view-legacy')
      self.AddTarget('File', 'share/*', '/usr/share/')
      self.AddTarget('File', 'doc/*', '/usr/share/doc/vmware-horizon-client/')
      self.AddTarget(
         'File', 'patches/*', '/usr/share/doc/vmware-horizon-client/patches/')
      self.AddTarget(
         'File', 'debug/*', '/usr/share/doc/vmware-horizon-client/debug/')
      self.SetPermission(BINDIR/'vmware-view', 0o755)
      self.SetPermission('/usr/lib/vmware/view/bin/vmware-view-legacy', BINARY)
      self.SetPermission('/usr/lib/vmware/view/bin/vmware-view', BINARY)
      self.SetPermission(BINDIR/'vmware-view-log-collector', BINARY)

      # Create link libudev.so.0 if system default libudev is libudev.so.1
      result = self.RunCommand('ldconfig', '-p', ignoreErrors=True)
      output = result[1];
      if output:
         output_list = output.split('\n')
         udev0 = ''
         for l in output_list:
            if str(l).find('libudev.so.0') >= 0:
               udev0 = l
               break;

         if not udev0:
            for l in output_list:
               if str(l).find('libudev.so.1') >= 0:
                  udevlist = l.split()
                  LIBUDEV = udevlist[-1]
                  self.AddTarget('Link', LIBUDEV, LIBDIR/'vmware/libudev.so.0')
                  break;

      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):
      self.SetConfig('viewComponent', 'test')
      self.AddScanFile(LIBDIR/'vmware/view/bin/vmware-view-legacy')
      self.AddScanFile(LIBDIR/'vmware/view/bin/vmware-view')
      self.RunCommand('ldconfig')
      # Update desktop database to enable URI launch
      self.RunCommand('update-desktop-database', ignoreErrors=True)

   def PostUninstall(self, old, new, upgrade):
      self.DelConfig('viewComponent')
      self.DelConfig('viewComponent')
      # Update desktop database to remove vmware-view desktop from cache
      self.RunCommand('update-desktop-database', ignoreErrors=True)
