#!/usr/bin/env bash
#
# Copyright 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 postpone the running of vmtoolsd until vami-sfcb is running
#
# DO NOT REMOVE LINE: VAMI_VMTOOLSD_WRAPPER

sfcbrunning=0
wait_iteration_limit=1440
sleep_interval=5
vlog=/var/log/vami_vmtoolsd.log
> $vlog


Logging()
{
   echo -e `date` "$*" >> $vlog
}


Logging Starting vmtoolsd wrapper...

# full path to original vmtoolsd
vmtoolsd=`dirname $0`/`basename $0`.vami/`basename $0`

if [ ! -f $vmtoolsd ]; then
   Logging ERROR: Missing $vmtoolsd file!
   exit 1
fi

# When vmtoolsd starts as a daemon, "vmtoolsd --background <pidfile>",
# put it on hold until vami-sfcbd is ready.
if [ -x /etc/init.d/vami-sfcb ] &&
   [ "$1" == "--background" ] && [ "$2" != "" ]; then

   pid_file=$2

   # Remove the pidfile associated to this wrapper script until the original
   # vmtoolsd is ready to launch; the pidfile has an effect for VMware Tools.
   rm -f $pid_file

   # set traps
   trap 'rm -f $pid_file; exit' EXIT HUP INT QUIT ALRM TERM

   # Place the for-loop background process in a separate process group to 
   # avoid TERM signal witnessed in the Upstart program running on Ubuntu 10.x.
   set -m

   # Check for vami-sfcbd status every 5 seconds; 2 hours limit.
   # This for-loop needs to run in the background or else it would hold up
   # the other deamons during boot.
   for (( i=0; i<=$wait_iteration_limit; i++ ))
   do
       Logging Waiting for vami-sfcbd to be active...

       # Run vami_sfcb_test if available, otherwise check for vami-sfcbd pid
       if [ -x /opt/vmware/share/vami/vami_sfcb_test ]; then
          nohup /opt/vmware/share/vami/vami_sfcb_test 1>/dev/null 2>&1
          if [ $? -eq 0 ]; then
             sfcbrunning=1
             Logging vami-sfcbd is ready
          fi
       elif [ "$(pidof vami-sfcbd)" != "" ]; then
          sfcbrunning=1
          Logging vami-sfcbd is ready
       fi

       if [ $sfcbrunning -eq 1 -o $i -eq $wait_iteration_limit ]; then

          # Write the required vmtoolsd pidfile
          echo $$ > $pid_file
          # launching the original vmtoolsd binary
          Logging exec "$vmtoolsd $@"
          exec $vmtoolsd "$@"

          if [ $? -ne 0 ]; then
             Logging ERROR: Failed to execute $vmtoolsd "$@"
             rm -f $pid_file
             exit 1
          fi

       else
          Logging vami-sfcbd is not ready
          Logging sleep $sleep_interval seconds before recheck...
          sleep $sleep_interval
      fi

   done &

   exit 0

else

   # Launch the original vmtoolsd binary
   Logging exec "$vmtoolsd $@"
   exec $vmtoolsd "$@"
   if [ $? -ne 0 ]; then
      Logging ERROR: Failed to execute $vmtoolsd "$@"
      exit 1
   fi

fi

