#!/usr/bin/env python
# Copyright 2017-2018 VMware, Inc. All rights reserved. -- VMware Confidential
"""
Analytics component module following the B2B patching principles.
"""

import logging

from .utils import COMPONENT_NAME, preserve_version_file,\
   get_current_version, run_firstboot

from fss_utils import getTargetFSS
from extensions import extend, Hook
from l10n import msgMetadata as _T, localizedString as _
from vcsa_utils import getComponentDiscoveryResult
from reporting import getProgressReporter

logger = logging.getLogger(__name__)


@extend(Hook.Discovery)
def discover(ctx):
   logger.info('Executing Discovery Hook for Analytics Service')

   return getComponentDiscoveryResult(COMPONENT_NAME)


@extend(Hook.Prepatch)
def pre_patch(ctx):
   # This hook is invoked before the rpms are installed.
   logger.info('Executing Pre Patch Hook for Analytics Service')
   preserve_version_file(ctx.stageDirectory)


@extend(Hook.Patch)
def patch(ctx):
   # This hook is invoked after the rpms are installed.
   logger.info('Executing Patch Hook for Analytics Service')

   progress_reporter = getProgressReporter()
   progress_reporter.updateProgress(0, _(_T('analytics.patch.begin',
                                            'Start Analytics patching')))

   current_version = get_current_version(ctx.stageDirectory)
   if (current_version is None) or (int(current_version) < 4):
      run_firstboot(ctx)

   progress_reporter.updateProgress(100, _(_T('analytics.patch.complete',
                                              'Patching Analytics completed')))
