#!/bin/sh
#
# run - run the server with LD_LIBRARY_PATH appropriately set
#
HERE=`pwd`
BIN=`dirname $0`

#
# allow for BIN being an absolute or a relative path
#
LD_LIBRARY_PATH="$HERE:/usr/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

# 
# ensure DLLs are in the PATH for MSWindows
#
U_OS=`uname -o 2>/dev/null`
if [ "$OS" = "Windows_NT" -o "$U_OS" = "Cygwin" ]; then
  PATH="$HERE:$HERE/$BIN:$BIN:$HERE/../lib:$HERE/$BIN/../lib:$BIN/../lib:$HERE/../examples:$HERE/$BIN/../examples:$BIN/../examples:$PATH"
fi

#
# if the platform contains a special OpenSSL build or a special ODBC
# driver build then ensure those areas are contained within the 
# LD_LIBRARY_PATH as the preferred location
#
if [ -d "/opt/cryptsoft/openssl/1.1.0dev" ]; then 
  LD_LIBRARY_PATH=/opt/cryptsoft/openssl/1.1.0dev:$LD_LIBRARY_PATH
fi
if [ -d "/opt/cryptsoft/openssl/3.0.0" ]; then 
  LD_LIBRARY_PATH=/opt/cryptsoft/openssl/3.0.0:$LD_LIBRARY_PATH
fi
if [ -d "/opt/cryptsoft/odbc/lib" ]; then 
  LD_LIBRARY_PATH=/opt/cryptsoft/odbc/lib:$LD_LIBRARY_PATH
fi

# allow for a DEV build with KMIPC shared library
if [ -d "../kmipc" ]; then 
  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../kmipc
fi

if [ $# = 0 ]; then
  echo "usage: run program [args]" >&2
  exit 1
fi

PROG=$1
shift

if [ ! -f server.cfg ]; then 
  cd $BIN
fi

if [ -d "db" ]; then 
  DBTOP=.
else 
  DBTOP=$BIN/..
fi

case "$PROG" in
  *k_db)
    EXTRA_ARGS="top=$DBTOP "
    ;;
esac

if [ -x "./$PROG" ]; then 
  PROG="./$PROG"
else 
  if [ -x "$BIN/$PROG" ]; then 
    PROG="$BIN/$PROG"
  fi
fi

exec $PROG $EXTRA_ARGS "$@"

