#!/bin/bash

#
# some observations on memory usage (27-jul-14; oe):
#
# + we used to exclusively run in 32-bit mode; using the 1214 release candidate
#   of the ERG, once cheap is initialized (but has yet to parse anything), its
#   process size is about 480 mbytes;
# + my trial 64-binary, on the other hand, is close to 790 mbyte at that point;
# + parsing cb/1100 in one-best mode (repeatedly), process sizes peak at around
#   530 mbyte in 32-bit mode and 890 mbyte in 64-bit mode (two thirds more);
# + on my 2014 X1C, parsing takes about 0.6 seconds in either mode, i.e. there
#   is no relevant space–time trade-off (no speed to gain from 64-bit mode);
# + the memory limit option used to only control the maximum allocations for
#   temporary dags, i.e. ones created during unifications; this has led some
#   users (including dan) to assume that the memory limit actually is off by
#   a factor of two.
#

#
# determine the LOGON root directory, assuming this script resides in a 
# ‘bin’ sub-directory that is shared across platforms.
#
if [ -z "${LOGONROOT}" ]; then
  path=$(dirname $0)
  if [ "${path#./}" != "${path}" ]; then
    path="$(pwd)/${path#./}"
  fi
  if [ "${path#/}" = "${path}" ]; then
    if [ "${path}" = "." ]; then
      path="$(pwd)";
    else 
      path="$(pwd)/${path}"
    fi
  fi
  LOGONROOT="${path%/bin}";
  export LOGONROOT;
fi

#
# include a shared set of shell functions and global parameters, including the
# architecture identifier .LOGONOS.  also, remember the native kernel mode.
#
. ${LOGONROOT}/etc/library.bash;
native=${LOGONOS};

#
# allow developers to test (easily) using a binary external to the LOGON tree,
# avoid adding our own PET-specific shard libraries to LD_LIBRARY_PATH.
#
if [ -n "${LOGONCHEAP}" ]; then
  #
  # if need be, drop an initial command-line option specific to this script
  #
  if [ "${1}" == "--32" -o "${1}" == "--tty" \
       -o "${1}" == "-s" -o "${1}" == "--stable" \
       -o "${1}" == "-t" -o "${1}" == "--test" \
       -o "${1}" == "--ut" \
       -o "${1}" == "-l" -o "${1}" == "--local" \
       -o "${1}" == "--size" -o "${1}" == "--duration" ]; then
    shift 1;
  fi
  exec ${LOGONCHEAP} "$@"
fi

#
# as of mid-2011, the standard (i.e. more or less supported) LOGON binaries 
# are always 32-bit; why duplicate the build effort, for no obvious benefit in
# running 64-bit PET binaries?                                   (5-aug-11; oe)
#
# starting with the 1214 release of the ERG, we want the extra leg room during
# treebank creation, i.e. use more generous resource limits.    (21-nov-14; oe)
#

#
# default to the ‘standard’ binary, compiled off the PET trunk
#
binary=${LOGONROOT}/uio/bin/${LOGONOS}/cheap;

#
# at this point, parse the options that modify our own behaviour
#
while [ "${1}" == "--32" -o "${1}" == "--tty" \
        -o "${1}" == "-s" -o "${1}" == "--stable" \
        -o "${1}" == "-t" -o "${1}" == "--test" \
        -o "${1}" == "--ut" \
        -o "${1}" == "-l" -o "${1}" == "--local" \
        -o "${1}" == "--size" -o "${1}" == "--duration" ]; do
  case "${1}" in
    --32)
      LOGONOS=${LOGONOS%%.??}.32;
      native=${LOGONOS};
      shift 1;
    ;;
    --tty)
      unset DISPLAY
      shift 1;
    ;;
    -s|--stable)
      #
      # for legacy users, a ‘stable’ binary, compiled off PET release 1.0
      #
      LOGONOS=linux.x86.32;
      binary=${LOGONROOT}/uio/bin/${LOGONOS}/scheap;
      shift 1;
    ;;
    -t|--test)
      #
      # for developers, a ‘testing’ binary, possibly compiled off some branch;
      # revert to native kernel mode, i.e. 32- or 64-bit.
      #
      LOGONOS=${native};
      binary=${LOGONROOT}/uio/bin/${LOGONOS}/tcheap;
      shift 1;
    ;;
    --ut)
      #
      # as we introduce new functionality, more bleeding-edge than ‘--test’
      #
      LOGONOS=${native};
      binary=${LOGONROOT}/uio/bin/${LOGONOS}/ucheap;
      shift 1;
    ;;
    -l|--local)
      #
      # use a locally compiled binary, external to the LOGON tree
      #
      LOGONOS=${native};
      binary=${HOME}/src/pet/trunk/debug/cheap/cheap;
      shift 1;
    ;;
    --size)
      ulimit -d $[$2 * 1024];
      ulimit -v $[$2 * 1024];
      shift 2;
    ;;
    --duration)
      ulimit -t $2;
      shift 2;
    ;;
  esac
done

#
# now set up dynamic linker search path, so as to find our shared libraries
#
if [ "${LOGONOS%%.*}" == "linux" ]; then
  if [ -z "${LD_LIBRARY_PATH}" ]; then
    LD_LIBRARY_PATH=${LOGONROOT}/uio/lib/${LOGONOS};
  else
    LD_LIBRARY_PATH=${LOGONROOT}/uio/lib/${LOGONOS}:${LD_LIBRARY_PATH};
  fi
  export LD_LIBRARY_PATH;
fi

[ -x "${binary}" ] || binary=${LOGONROOT}/uio/bin/${LOGONOS}/cheap;
exec ${binary} "$@"