#!/usr/bin/env bash
#
# Copyright 2010-2011 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.
#

#
# Script to reconfigure vmware tools startup order, scripts, and tools.conf
# For VUM we need vami-sfcb to start and then tools starts and then
# we advertise guestinfo.
# Since we moved to using open-vm tools we have no control over tools scripts
# This re configuration script replaces vmtoolsd with a wrapper to delay the
# running of vmtoolsd until vami-sfcb is running. This script should be called
# after package installation and after update.
#

#
# Configure the start and resume scripts, these scripts advertise the
# guestinfo during poweron and resume of the vm.
#

export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=vami_reconfigure_tools

PATH=$PATH:/usr/sbin:/usr/lib/vmware-tools/sbin
VAMI_DIR=/opt/vmware/share/vami

configure_start_resume_scripts()
{
    TOOLS_CONF=/etc/vmware-tools/tools.conf
    RESUME_SCRIPT_TAG=resume-script
    POWERON_SCRIPT_TAG=poweron-script

    #
    # new vmtools uses "vmware-toolbox-cmd" to configure power/resume scripts,
    # old vmtools uses tools.conf file to configure
    # Doing both here is because user may install old one by himself/herself
    #
    if type 'vmware-toolbox-cmd'
    then
        vmware-toolbox-cmd script power enable
        vmware-toolbox-cmd script power set "$VAMI_DIR/vami_poweron_vm_script"
        vmware-toolbox-cmd script resume enable
        vmware-toolbox-cmd script resume set "$VAMI_DIR/vami_resume_vm_script"
    elif [ -f $TOOLS_CONF ]
    then
        echo `gettext "Configuring"` $TOOLS_CONF `gettext \
            "to add power-on and resume scripts."`
        # If tools.conf exists, append our resume script instead of the default
        cp $TOOLS_CONF ${TOOLS_CONF}.orig
        grep -v $RESUME_SCRIPT_TAG < ${TOOLS_CONF}.orig | \
            grep -v $POWERON_SCRIPT_TAG > $TOOLS_CONF
        echo "$POWERON_SCRIPT_TAG = \"$VAMI_DIR/vami_poweron_vm_script\"" \
            >> $TOOLS_CONF
        echo "$RESUME_SCRIPT_TAG = \"$VAMI_DIR/vami_resume_vm_script\"" \
            >> $TOOLS_CONF
    fi
}

#
# Configure vami vmtoolsd wrapper. The wrapper script is used to postpone
# the running of vmtoolsd until vami-sfcb is running.
#
configure_vmtoolsd_wrapper()
{
    echo `gettext "Configuring VAMI VMware tools service wrapper."`

    VMTOOLSD_FILE=`which vmtoolsd 2>/dev/null` || VMTOOLSD_NOT_FOUND=$?
    VMGUESTD_FILE=`which vmware-guestd 2>/dev/null` || VMGUESTD_NOT_FOUND=$?

    if [ ! $VMTOOLSD_NOT_FOUND ]; then
        VMTOOLSD=vmtoolsd
    elif [ ! $VMGUESTD_NOT_FOUND ]; then
        VMTOOLSD=vmware-guestd
        VMTOOLSD_FILE=$VMGUESTD_FILE
    else
        return 1
    fi

    # check if the file is vami wrapper file
    grep VAMI_VMTOOLSD_WRAPPER $VMTOOLSD_FILE > /dev/null
    if [ $? != 0 ]; then
        # original file, the 'VAMI_VMTOOLSD_WRAPPER' text was not found
        VMTOOLSD_ORIGDIR=$VMTOOLSD_FILE.vami
        VMTOOLSD_ORIGFILE=$VMTOOLSD_ORIGDIR/$VMTOOLSD

        # create a backup
        mkdir -p $VMTOOLSD_ORIGDIR
        mv -f $VMTOOLSD_FILE $VMTOOLSD_ORIGFILE
    fi

    cp -f $VAMI_DIR/vami_vmtoolsd_wrapper $VMTOOLSD_FILE

    return 0
}


# check if tools is installed if it isn't
# then we don't have to do anything
if [ -x /etc/init.d/vmware-tools -o \
     -x /etc/init.d/vmware-tools-services -o \
     -e /usr/lib/systemd/system/vmtoolsd.service ]
then
    configure_start_resume_scripts
    # Check and skip vmtoolsd wrapper on systemd.
    command -v systemctl > /dev/null
    if [ $? == "0" ]
    then
       echo `gettext "vmtoolsd wrapper not required on this VM with systemd."`
    # If vami-sfcb service is enabled, configure vmtoolsd_wrapper.
    elif [ -x /etc/init.d/vami-sfcb ]
    then
       configure_vmtoolsd_wrapper
    else
       echo `gettext "Service vami-sfcb is not installed on this VM."`
    fi
else
    echo `gettext "VMware tools is not installed on this VM."`
fi

exit 0
