#!/bin/bash

unset DISPLAY;
unset LUI;

if [ -z "${LOGONROOT}" ]; then
  echo "batch: unable to determine \$LOGONROOT directory; exit.";
  exit 1;
fi

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

if [ -z "${LOGONLOG}" ]; then
  LOGONLOG=${LOGONROOT}/log;
fi

date=`date "+%y-%m-%d"`

count=1
limit=5
source="--source"
mode=noen

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --smt)
      mode=smt;
      shift 1;
    ;;
    --oa)
      mode=oa;
      shift 1;
    ;;
    --visl)
      mode=visl;
      shift 1;
    ;;
    --google)
      mode=google;
      shift 1;
    ;;
    --jaen)
      mode=jaen;
      shift 1;
    ;;
    --jaen+chasen)
      mode=jaen+chasen;
      shift 1;
    ;;
    --enja)
      mode=enja;
      shift 1;
    ;;
    --deen)
      mode=deen;
      shift 1;
    ;;
    --ende)
      mode=ende;
      shift 1;
    ;;
    --enen)
      mode=enen;
      shift 1;
    ;;
    --noja)
      mode=noja;
      shift 1;
    ;;
    --from)
      from=${2};
      mode=mmt;
      shift 2;
    ;;
    --to)
      to=${2};
      mode=mmt;
      shift 2;
    ;;
    --binary)
      source="";
      shift 1;
    ;;
    --source)
      source="--source";
      shift 1;
    ;;
    -a|--text|--bitext|--ascii)
      ascii=true;
      shift 1;
    ;;
    -c|--count)
      count=${2};
      echo "batch: ignoring --count argument (PR itsdb/323)."
      count=1
      shift 2;
    ;;
    -l|--limit)
      limit=${2};
      if echo "${limit}" | grep : > /dev/null; then
        limit="\"${limit}\"";
      fi
      shift 2;
    ;;
    -s|--suffix)
      suffix="${2}";
      shift 2;
    ;;
    --cat)
      cat="--cat";
      shift 1;
    ;;
    *)
      echo -n "usage: batch [ --count n ] [ --limit n ] [ --ascii ] ";
      echo " { skeleton | file }";
      exit 1;
  esac
done

case ${mode} in
  noen)
    cpu=":logon";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/uib/data/base.txt";
    else
      skeleton="base"
    fi
    ;;
  smt)
    cpu=":smt";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/uib/data/base.txt";
    else
      skeleton="base"
    fi
    ;;
  oa)
    cpu=":oa";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/uib/data/base.txt";
    else
      skeleton="base"
    fi
    ;;
  visl)
    cpu=":visl";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/uib/data/base.txt";
    else
      skeleton="base"
    fi
    ;;
  google)
    cpu=":google";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/uib/data/base.txt";
    else
      skeleton="base"
    fi
    ;;
  jaen)
    cpu=":ja2en";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/dfki/jacy/data/mrs.je.txt";
    else
      skeleton="shiken"
    fi
    ;;
  jaen+chasen)
    cpu=":ja2en+chasen";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/dfki/jacy/data/mrs.je.txt";
    else
      skeleton="shiken"
    fi
    ;;
  enja)
    cpu=":en2ja";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/dfki/jacy/data/mrs.ej.txt";
    else
      skeleton="shiken"
    fi
    ;;
  deen)
    cpu=":de2en";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/dfki/gg/data/mrs.deen.txt";
    else
      skeleton="mrs"
    fi
    ;;
  ende)
    cpu=":en2de";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/dfki/gg/data/mrs.ende.txt";
    else
      skeleton="mrs"
    fi
    ;;
  enen)
    cpu=":en2en";
    skeleton="mrs"
    ;;
  
  noja)
    cpu=":no2ja";
    if [ -n "${ascii}" ]; then
      skeleton="${LOGONROOT}/ntnu/data/mrs.noja.txt";
    else
      skeleton="mrs"
    fi
    ;;
  mmt)
    cpu=":${from}2${to}"
    ;;
esac

if [ ! -z "${1}" ]; then 
  skeleton=${1};
fi
if [ -n "${ascii}" ]; then
  if [ "${skeleton#/}" = "${skeleton}" ]; then 
      skeleton=${PWD}/${skeleton}; 
  fi
  if [ ! -f "${skeleton}" ]; then
    echo "batch: unable to open \`${skeleton}'; exit."
    exit 1;
  fi
fi

name=$(basename ${skeleton} ".txt")


LOG=${LOGONLOG}/${name}.fan.${date}.log

{
  options=":error :exit :wait 1200";

  echo "(setf (system:getenv \"DISPLAY\") nil)";

  loadup;
  if [ -n "${gc}" ]; then gc; fi

  case ${mode} in
    jaen|jaen+chasen)
      echo "(tsdb:tsdb :skeleton 
             \"${LOGONROOT}/dfki/jacy/tsdb/skeletons\")";
    ;;
    deen)
      echo "(tsdb:tsdb :skeleton 
             \"${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/deutsch\")";
    ;;
    ende|enen)
      echo "(tsdb:tsdb :skeleton 
             \"${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english\")";
    ;;
    mmt)
      echo "(tsdb:tsdb :skeleton 
             \"${LOGONROOT}/uw/mmt/tsdb/skeletons\")";
      echo "(tsdb::mmt-create-clients)";
    ;;
  esac

  if [ "${limit}" != "nil" -a "${limit}" != "0" ]; then
    echo "(setf tsdb::*tsdb-maximal-number-of-analyses* ${limit})";
    echo "(setf tsdb::*tsdb-exhaustive-p* nil)";
  else
    echo "(setf tsdb::*tsdb-exhaustive-p* nil)";
  fi

  #
  # launch one or more processing clients
  #
  echo "(tsdb:tsdb :cpu ${cpu} :file t :count ${count} ${options})";

  #
  # name and create the target profile, either from a skeleton or by means of
  # importing an ASCII bi-text file
  #
  echo "(setf target
         (format
          nil \"~a~a\"
          (tsdb::suggest-test-run-directory \"${name}\" :absolute nil) 
          \"${suffix}\"))";
  if [ -z "${ascii}" ]; then
    echo "(tsdb:tsdb :create target :skeleton \"${name}\")";
  else \
    echo "(tsdb::do-import-items \"$skeleton\" target :format :bitext)";
  fi

  #
  # finally, start batch translation, eventually terminate gracefully
  #
  echo "(tsdb::tsdb-do-process target :type :translate :overwrite t)";

  echo "(pvm::pvm_quit)";
  echo "(sleep 1)";
  echo "(excl:exit)";

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