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

Dummy vmware-horizon-serialport component.
"""

import os
import os.path

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

class VMwareHorizonSerialPort(Installer):
   def InitializeQuestions(self, old, new, upgrade):
      defaultSerialPort = self.GetAnswer('serialportEnable')
      if defaultSerialPort:
         qlevel = 'CUSTOM'
      else:
         qlevel = 'REGULAR'

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

   def InitializeInstall(self, old, new, upgrade):
      serialportEnable = self.GetAnswer('serialportEnable')
      if serialportEnable == 'yes':
         self.AddTarget('File', 'init.d/*', DEST)
         self.AddTarget('File', 'lib/vmware/view/bin/ftsprhvd', DEST/'ftsprhvd')
         self.AddTarget('File', 'vmware/ftplugins.conf', DEST/'ftplugins.conf')
         self.AddTarget(
            'File',
            'lib/vmware/view/bin/ftsprhvd',
            '/usr/lib/vmware/view/bin/ftsprhvd')
         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'scanner 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/ftsprhv.debian',
               '/etc/init.d/ftsprhv')
         else:
            self.AddTarget(
               'File',
               'init.d/ftsprhv.redhat',
               '/etc/init.d/ftsprhv')
         self.SetPermission('/etc/init.d/ftsprhv', BINARY)
         self.SetPermission('/usr/lib/vmware/view/bin/ftsprhvd*', BINARY)

         self.SetPermission(DEST/'*', BINARY)

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

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

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

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

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

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

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

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

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