#!/bin/sh

#
# LKB wrapper to load the ERG and go into generation server mode
#

source=""
grammar=erg;

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --erg)
      grammar=erg;
      shift 1;
    ;;
    --jacy)
      grammar=jacy;
      shift 1;
    ;;
    --gg)
      grammar=gg;
      shift 1;
    ;;
    --binary)
      source="";
      shift 1;
    ;;
    --source)
      source="--source";
      shift 1;
    ;;
    *)
      echo -n "usage: server [ --jacy ] [ --gg ]";
      echo " { skeleton | file }";
      exit 1;
  esac
done

(
  #
  # when running from source, configure and compile the LOGON system
  #
  if [ -n "${source}" ]; then
    echo "(load \"${LOGONROOT}/lingo/lkb/src/general/loadup.lisp\")";
    echo "(pushnew :lkb *features*)";
    echo "(pushnew :mrs *features*)";
    echo "(pushnew :tsdb *features*)";
    echo "(pushnew :mt *features*)";
    echo "(pushnew :logon *features*)";
    echo "(compile-system \"tsdb\")";
  fi

  echo "(sleep 1)";
  echo "(pushnew :slave *features*)";
  case ${grammar} in
    erg)
      echo "(lkb::read-script-file-aux \"$LOGONROOT/lingo/erg/lkb/script\")";
      ;;  
    jacy)
      echo "(lkb::read-script-file-aux \"$LOGONROOT/dfki/jacy/lkb/script\")";
      ;;  
    gg)
      echo "(lkb::read-script-file-aux \"$LOGONROOT/dfki/gg/lkb/script\")";
      ;;  
  esac;
  echo "(lkb::index-for-generator)";
  echo "(setf *maximum-number-of-edges* 10000)";
  echo "(progn (excl:gc) (excl:gc t) (excl:gc))";
  echo "(lkb::start-generator-server)";
  echo "(format t \"~%#~%# generation server: all systems ready.~%#~%\")";
  cat -
) | ${LOGONROOT}/bin/logon ${source} \
      -I base -locale no_NO.UTF-8 "$@" -qq