#!/usr/bin/env python
#
# Copyright (c) 2008-2010 VMware, Inc.  All rights reserved.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import sys
import os
import subprocess
import libxml2

VMTOOLS_PATH = ['/usr/bin', '/usr/sbin', '/usr/lib/vmware-tools/sbin']
VMTOOLS_NAME = ['vmware-guestd', 'vmware-rpctool']
UPDATE_VAMI_DEPLOY="/opt/vmware/var/lib/vami/update/provider/provider-deploy.xml"

def getGuestdTool():
    """
      Return vmware-guestd or vmware-rpctool found on system.
    """
    for p in VMTOOLS_PATH:
        for n in VMTOOLS_NAME:
            guestdtool = os.path.join(p, n)
            if os.path.exists(guestdtool) and os.access(guestdtool, os.X_OK):
                if (n == 'vmware-guestd'):
                   guestdtool = guestdtool + ' --cmd'
                return guestdtool
    return None

def setVamiGuestInfo(guestd_cmd):
    """
      Call setGuestInfo for all vami variables
    """
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.version 2010100\'",
        shell=True)
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.features SUP\'",
        shell=True)
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.http-port 5488\'",
        shell=True)
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.https-port 5489\'",
        shell=True)
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.https-clientkeyfile " +
        "/opt/vmware/etc/sfcb/file.pem\'",
        shell=True)
    subprocess.call(guestd_cmd +
        " \'info-set guestinfo.vmware.vami.https-clienttruststorefile " +
        "/opt/vmware/etc/sfcb/client.pem\'",
        shell=True)

def repositoryEnabled():
    """
      Check if repository is enabled by default for this appliance by checking
      the provider-deploy file.
    """
    try:
        doc = libxml2.parseFile(UPDATE_VAMI_DEPLOY)
        xp = doc.xpathNewContext()
        nodeList = xp.xpathEval(
            "//service/properties/property[@name='repositoryAddress']")

        repo = ""
        for node in nodeList:
            repo = node.prop("value")

        if len(repo) > 0:
            return 1
        return 0
    except:
        return 0


guestdTool = getGuestdTool()
if guestdTool and repositoryEnabled():
    setVamiGuestInfo(guestdTool)
