#!/usr/bin/env python
# Copyright 2016-2017 VMware, Inc.
# All rights reserved. -- VMware Confidential

"""
    This script is a utility to be used by both day0 and b2b scripts
"""
import sys
import os
from os.path import join, abspath, dirname
import logging
import json

logger = logging.getLogger(__name__)

DEPLOYMENT_ROOT = abspath(join(dirname(__file__), os.pardir))
sys.path.append(join(DEPLOYMENT_ROOT, "scripts/patches/libs/feature-state"))
import featureStateTarget
# do not call featureStateTarget.init() !!!
# init() will override all the values with the ones from
# /etc/vmware/vsphereFeatures/vsphereFeatures.cfg in source build

_FSSoverride = {}

# Try to load override file
try:
    with open("/etc/vmware/vsphereFeatures/b2b_fss_override.json") as f:
        _FSSoverride = json.load(f)
except IOError:
    pass  # No such file: OK


def getTargetFSS(name):
    ''' Get FSS state for the target build. I.e. not the FSS state in this
        vCenter but FSS state as is set in the version being installed with this
        patch

    @param name: Name of the feature switch
    @type name: str

    @return: state as True, False or None (if set to None or there is no such
             switch)
    @rtype: bool

    '''
    if name in _FSSoverride:
        return _FSSoverride[name]
    # featureStateTarget.py defines all the feature switches as module-level
    # variables. So we use hasattr/getattr on module.
    if hasattr(featureStateTarget, name):
        return getattr(featureStateTarget, name)
    return None
