#!/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-vcha
binary="`basename "$this" .sh`"
defaultArgs=

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

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

ld_library_path="$abslibdir"

export LD_LIBRARY_PATH="$ld_library_path:/opt/vmware/vpostgres/current/lib"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/lib/vmware-vmafd/lib64"

LD_PRELOAD="$VMW_PRELOAD" \
   exec $DEBUG_CMD "$libdir"/$binary $defaultArgs "$@"
