#!/bin/bash

if [ ! -f ${LOGONROOT}/etc/library.bash ]; then
  echo "logon: unable to determine \$LOGONROOT source directory; exit"
  exit 1;
fi

. ${LOGONROOT}/etc/library.bash

source="--source";
all=yes;

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --32)
      LOGONOS=${LOGONOS%%.??}.32;
      shift 1;
    ;;
    --naked)
      unset source;
      shift 1;
    ;;
    --noun*)
      nouns=yes;
      unset all;
      shift 1;
    ;;
    --adjective*)
      adjectives=yes;
      unset all;
      shift 1;
    ;;
    --kf)
      nouns=yes;
      adjectives=yes;
      unset all;
      shift 1;
    ;;
    --compound*)
      compounds=yes;
      unset all;
      shift 1;
    ;;
    --lars*)
      lars=yes;
      unset all;
      shift 1;
    ;;
    --andre*)
      andre=yes;
      unset all;
      shift 1;
    ;;
    --unknown*)
      unknown=yes;
      unset all;
      shift 1;
    ;;
    --debug)
      debug=true;
      shift 1;
    ;;
    --cat)
      cat="--cat";
      shift 1;
    ;;
  esac
done

log=${HOME}/trag.$(date "+%Y-%m-%d").log

unset DISPLAY

{
  if [ -n "${source}" ]; then
    echo "(pushnew :lkb *features*)";
    echo "(pushnew :logon *features*)";
    echo "(load \"${LOGONROOT}/lingo/lkb/src/general/loadup.lisp\")";
    echo "(compile-system \"tsdb\" :force t)";
  fi

  echo "(tsdb::tsdb :cpu :norgram :task :parse :file t)";
  echo "(tsdb::tsdb :cpu :erg :task :parse :file t :reset nil :wait 300)";
  echo "(lkb::read-script-file-aux \"${LOGONROOT}/uio/trag/lkb/script\")";
  if [ -n "${all}" -o -n "${nouns}" -o -n "${adjectives}" ]; then
    echo "(tsdb::initialize-kf)";
  fi
  if [ -n "${debug}" ]; then
    echo "(setf tsdb::*pvm-debug-p* t)";
  fi

  echo "(setf semi 
         (mt:read-semi 
          \"${LOGONROOT}/parc/pargram/norwegian/bokmal/norgram.smi\"))";

  if [ -n "${all}" -o -n "${nouns}" ]; then
    echo "(tsdb::process-kf
           (tsdb::kf-list :category :noun)
           :simplex \"${LOGONROOT}/uio/noen/kf.n.mtr\"
           :mwe \"${LOGONROOT}/uio/noen/kf.nn.mtr\"
           :gaps \"${LOGONROOT}/uio/noen/kf.n.gaps\"
           :log \"${LOGONROOT}/uio/noen/kf.n.log\"
           :active (list (mrs:vsym \"v*n_no\") (mrs:vsym \"n*n_no\")
                         (mrs:vsym \"noun_no\")))";
  fi
  if [ -n "${all}" -o -n "${adjectives}" ]; then
    echo "(tsdb::process-kf
           (tsdb::kf-list :category :adjective)
           :simplex \"${LOGONROOT}/uio/noen/kf.a.mtr\"
           :gaps \"${LOGONROOT}/uio/noen/kf.a.gaps\"
           :log \"${LOGONROOT}/uio/noen/kf.a.log\"
           :active (list (mrs:vsym \"adjective_no\")))";
  fi
  if [ -n "${all}" -o -n "${compounds}" ]; then
    echo "(tsdb::process-kf
           \"${LOGONROOT}/uio/nw/compounds/compounds.logon\"
           :simplex \"${LOGONROOT}/uio/noen/nn.mtr\"
           :log \"${LOGONROOT}/uio/noen/nn.log\")";
  fi
  if [ -n "${all}" -o -n "${lars}" ]; then
    echo "(tsdb::process-kf
           \"${LOGONROOT}/uio/nw/compounds/lars.dat\"
           :simplex \"${LOGONROOT}/uio/noen/lars.nn.mtr\"
           :log \"${LOGONROOT}/uio/noen/lars.nn.log\"
           :beam -1.0)";
  fi
  if [ -n "${all}" -o -n "${andre}" ]; then
    echo "(tsdb::process-kf
           (tsdb::read-andre \"${LOGONROOT}/uio/noen/andre.n.dat\")
           :simplex \"${LOGONROOT}/uio/noen/andre.n.mtr\"
           :mwe \"${LOGONROOT}/uio/noen/andre.nn.mtr\"
           :log \"${LOGONROOT}/uio/noen/andre.n.log\"
           :active (list (mrs:vsym \"v*n_no\") (mrs:vsym \"n*n_no\")
                         (mrs:vsym \"noun_no\")))";
  fi
  if [ -n "${all}" -o -n "${unknown}" ]; then
    echo "(tsdb::process-kf
           (tsdb::read-unknown 
            \"${LOGONROOT}/uio/noen/unknown.n.dat\" :semi semi)
           :simplex \"${LOGONROOT}/uio/noen/unknown.n.mtr\"
           :log \"${LOGONROOT}/uio/noen/unknown.n.log\"
           :source (mrs:vsym \"noun_no\")
           :active (list (mrs:vsym \"noun_no\")))";
  fi

  echo "(excl:exit 0 :no-unwind t)";

} | $LOGONROOT/bin/logon ${source} ${cat} \
    -I base -locale en_US.UTF-8 -qq 1>&1 | tee ${log}