#!/bin/sh
#
# Wrapper for the real binary. Ensure that the binary will find all
# the shared libraries it needs. If a shared library is not available from any
# of the standard system-wide locations, we provide it from the build
# directory. --hpreg
#

# Use readlink only when necessary: some boxes don't have it, some boxes have
# one that does not support the -f option. --hpreg
if [ -h "$0" ]; then
   this="`readlink -f "$0"`"
else
   this="$0"
fi

libdir=/usr/lib/vmware-vpx
binary="`basename "$this" .sh`"
defaultArgs=

# If /etc/vmware-vpx/vpxdDebug is present, use debug dir as the libdir.
# This will start debug version of vpxd and some of it's shared libraries.
vpxdDebugFile="/etc/vmware-vpx/vpxdDebug"
if [[ -e $vpxdDebugFile ]] ; then
   libdir="$libdir/debug"
fi

abslibdir="`cd \"$libdir\" 2>/dev/null && pwd || echo \"$libdir\"`"
# Generate cores even in release env
ulimit -c unlimited

# Raise the number of file descriptors that can be opened.
# No more than FD_SETSIZE_HUGE, see PR 689249, cl 7151.
ulimit -n 10000

#Oracle client settings to disable signal takeover
export ORACLE_HOME=/etc/vmware-vpx/oracle

if [ "$PID_FILE" ]; then
    echo $$ > "$PID_FILE"
fi

if [ -d $libdir/$binary-wrapper.d ]; then
   for script in $libdir/$binary-wrapper.d/*; do
         [ -f "$script" ] || continue
         . "$script"
   done
fi

VC_DB_TYPE=`/bin/awk '/^;DB_TYPE/ {print $3}' /etc/odbc.ini`
if [ "$VC_DB_TYPE" = "oracle" ]; then
   ld_library_path="$abslibdir:/usr/lib/oracle/11.2/client64/lib"
else
   ld_library_path="$abslibdir"
fi

export LD_LIBRARY_PATH="$ld_library_path:/usr/lib/vmware-vmca/lib64:/usr/lib/vmware-vmafd/lib64"

VMW_PRELOAD=${VMW_PRELOAD-$libdir/libMallocArenaFix.so}
LD_ASSUME_KERNEL=$LD_ASSUME_KERNEL \
LD_PRELOAD="$VMW_PRELOAD" \
   exec $DEBUG_CMD "$libdir"/$binary $defaultArgs "$@"

