"""
Copyright 2018,2020 VMware, Inc.  All rights reserved. -- VMware Confidential

Dummy vmware-horizon-scanner-component.
"""

import os
import os.path

DEST = LIBDIR/'vmware/view/scannerclient'
PLUGIN_DIR=LIBDIR/'vmware/rdpvcbridge'

class VMwareHorizonScanner(Installer):
   def InitializeQuestions(self, old, new, upgrade):
      defaultScanner = self.GetAnswer('scannerEnable')
      if defaultScanner:
         qlevel = 'CUSTOM'
      else:
         qlevel = 'REGULAR'

      self.AddMultiChoiceQuestion(
         'YesNo',
         key='scannerEnable',
         text='Scanner Redirection',
         required=True,
         default='yes',
         footer='The Scanner Redirection component allows you to use local '
                'scanner devices from a remote desktop.',
         level=qlevel)

   def InitializeInstall(self, old, new, upgrade):
      scannerEnable = self.GetAnswer('scannerEnable')
      if scannerEnable == 'yes':
         self.AddTarget('File', 'vmware/ftplugins.conf', DEST/'ftplugins.conf')
         self.AddTarget('File', 'init.d/*', DEST)
         self.AddTarget(
            'File', 'lib/vmware/view/bin/ftscanhvd', DEST/'ftscanhvd')
         self.AddTarget(
            'File',
            'lib/vmware/view/bin/ftscanhvd',
            '/usr/lib/vmware/view/bin/ftscanhvd')

         if not os.path.exists('/tmp/installFlag'):
            self.AddTarget(
               'File',
               'lib/vmware/rdpvcbridge/*',
               '/usr/lib/vmware/rdpvcbridge/')
            self.SetPermission('/usr/lib/vmware/rdpvcbridge/*', BINARY)
            self.RunCommand('touch', '/tmp/installFlag')
            self.AddTarget(
               'File',
               'vmware/ftplugins.conf',
               '/etc/vmware/ftplugins.conf')
         else:
            log.Warn(u'serialport has installed ftnlses3hv.so and '
                      'ftplugins.conf.')

         if os.path.isfile(
            '/usr/sbin/update-rc.d') or os.path.isfile('/usr/bin/update-rc.d'):
            self.AddTarget(
               'File',
               'init.d/ftscanhv.debian',
               '/etc/init.d/ftscanhv')
         else:
            self.AddTarget(
               'File',
               'init.d/ftscanhv.redhat',
               '/etc/init.d/ftscanhv')
         self.SetPermission('/etc/init.d/ftscanhv', BINARY)
         self.SetPermission('/usr/lib/vmware/view/bin/ftscanhvd*', BINARY)
         self.SetPermission(DEST/'*', BINARY)

         if not os.path.isfile('/etc/init.d/ftscanhv'):
            log.Error(u'/etc/init.d/ftscanhv not exist')
         self.RegisterService(name='ftscanhv',
                              src='init.d/ftscanhv',
                              start=50, stop=8)
      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 PreUninstall(self, old, new, upgrade):
      scannerEnable = self.GetAnswer('scannerEnable')
      if scannerEnable == 'yes':
         script = '/etc/init.d/ftscanhv'
         if os.path.isfile(script) and self.RunCommand(
            script, 'stop', ignoreErrors=True).retCode != 0:
            log.Warn(u'Unable to stop ScannerRedirection service.')
         inits = self.LoadInclude('initscript')
         inits.DeconfigureService('ftscanhv')

   def PostInstall(self, old, new, upgrade):
      self.SetConfig('scannerclientComponent', 'test')
      scannerEnable = self.GetAnswer('scannerEnable')
      if scannerEnable == 'yes':

         self.AddScanFile(LIBDIR/'vmware/view/bin/ftscanhvd')

         if os.path.isfile(
            '/usr/sbin/update-rc.d') or os.path.isfile('/usr/bin/update-rc.d'):
            self.RunCommand(
               'update-rc.d', 'ftscanhv', 'defaults',  ignoreErrors=True)
         else:
            self.RunCommand('chkconfig', 'ftscanhv', 'on', ignoreErrors=True)

         # Stop the ScannerRedirection init script
         script = INITSCRIPTDIR/'ftscanhv'
         if script.exists() and self.RunCommand(
            script, 'stop', ignoreErrors=True).retCode != 0:
            log.Warn(u'Unable to stop current ScannerRedirection service. '
                      'Not fatal.')

         # Start the ScannerRedirection init script
         script = INITSCRIPTDIR/'ftscanhv'
         if script.exists() and self.RunCommand(
            script, 'start', ignoreErrors=True).retCode != 0:
            log.Error(u'Unable to start ScannerRedirection service.')
      if os.path.exists('/tmp/installFlag'):
         self.RunCommand('rm', '-rf', '/tmp/installFlag', ignoreErrors=True)


   def PostUninstall(self, old, new, upgrade):
      self.DelConfig('scannerclientComponent')
      if os.path.exists('/tmp/installFlag'):
         self.RunCommand('rm', '-rf', '/tmp/installFlag', ignoreErrors=True)

   def PreExit(self, old, new, upgrade):
      scannerEnable = self.GetAnswer('scannerEnable')
      if scannerEnable == 'yes':
         script = '/etc/init.d/ftscanhv'
         if os.path.isfile(script) and self.RunCommand(
            script, 'stop', ignoreErrors=True).retCode != 0:
            log.Warn(u'Unable to stop ScannerRedirection service.')

         inits = self.LoadInclude('initscript')
         # Deconfigure the service twice to make sure that all the autostart
         # scripts are successfully removed
         inits.DeconfigureService('ftscanhv')
         inits.DeconfigureService('ftscanhv')
