#!/bin/bash

#
# Hey, emacs(1), this is -*- mode: shell; coding: utf-8; -*- got it?
#

unset DISPLAY;
unset LUI;

if [ -z "${LOGONROOT}" ]; then
  echo "www: 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;

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

source="--source";
setup="logon";
limit=5

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --binary)
      source="";
      shift 1;
    ;;
    --source)
      source="--source";
      shift 1;
    ;;
    --setup)
      setup=${2};
      shift 2;
    ;;
    --noen|logon)
      setup=logon;
      shift 1;
    ;;
    --erg)
      setup=erg;
      shift 1;
    ;;
    --gg)
      setup=gg;
      shift 1;
    ;;
    --deen)
      setup=deen;
      shift 1;
    ;;
    --jaen)
      setup=jaen;
      shift 1;
    ;;
    --jaen+chasen)
      setup=jaen+chasen;
      shift 1;
    ;;
    --jacy)
      setup=jacy;
      shift 1;
    ;;
    --srg)
      setup=srg;
      shift 1;
    ;;
    --cst)
      setup=cst;
      shift 1;
    ;;
    --limit)
      limit=${2};
      shift 2;
    ;;
    --port)
      port=${2};
      shift 2;
    ;;
    --cat)
      cat="--cat";
      shift 1;
    ;;
    -d|--debug)
      debug=true;
      shift 1;
    ;;
  esac
done

if [ -z "${port}" ]; then
  case ${setup} in
    logon|noen) port=8012; ;;
    jaen|jaen+chasen) port=8050; ;;
    enja) port=8051; ;;
    deen) port=8052; ;;
    ende) port=8053; ;;
    erg) port=8100; ;;
    gg) port=8101; ;;
    jacy) port=8102; ;;
    srg) port=8103; ;;
    cst) port=8104; ;;
    *) port=8888; ;;
  esac
fi

LOG=${LOGONROOT}/www.${date}.${USER}.${port}.log


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

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

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

  #
  # set [incr tsdb()] options as needed for this particular task
  #
  echo "(setf tsdb::*process-suppress-duplicates* nil)";
  echo "(setf tsdb::*process-raw-print-trace-p* t)";

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

  case ${setup} in
    logon|noen)
      echo "(tsdb:tsdb :cpu :norgram :task :parse :file t 
             :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :noen :task :transfer :file t 
             :reset nil :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate
             :file t :reset nil :count 1 ${options})";
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/lingo/erg/lkb/script\")";
      echo "(tsdb:tsdb :cpu :norgram :task :parse :file t 
             :reset nil :count 2 ${options})";
      echo "(tsdb:tsdb :cpu :noen :task :transfer :file t 
             :reset nil :count 2 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate
             :file t :reset nil :count 2 ${options})";
      echo "(mt::read-smt)";
    ;;
    jaen)
      echo "(tsdb:tsdb :cpu :jacy+chasen :task :parse :file t 
             :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :jaen :task :transfer :file t 
             :reset nil :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate
             :file t :reset nil :count 1 ${options})";
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/lingo/erg/lkb/script\")";
      echo "(setf tsdb::*www-title* :jaen)";
      echo "(setf tsdb::*www-custom-js* \"jacy\")";
      echo "(setf tsdb::*www-introduction* \"jaen\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"日本語が面白いぞ.\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/jacy\")
                (:generate \"http://www.delph-in.net/erg\")))";
    ;;
    deen)
      echo "(tsdb:tsdb :cpu :gg@pet :task :parse :file t 
             :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :deen :task :transfer :file t 
             :reset nil :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate
             :file t :reset nil :count 1 ${options})";
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/dfki/gg/lkb/script\")";
      echo "(setf tsdb::*www-title* :deen)";
      echo "(setf tsdb::*www-custom-js* \"gg\")";
      echo "(setf tsdb::*www-introduction* \"deen\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"Karl lacht.\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/gg\")
                (:generate \"http://www.delph-in.net/erg\")))";
    ;;
    erg)
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/lingo/erg/lkb/script\")";
      echo "(tsdb:tsdb :cpu :cheap :task :parse :file t 
             :count 4 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate :reset nil
             :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :erg :task :generate :reset nil
             :count 1 ${options})";
      echo "(setf tsdb::*www-title* :erg)";
      echo "(setf tsdb::*www-custom-js* \"erg\")";
      echo "(setf tsdb::*www-introduction* \"erg\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"Try pressing return in this window!\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/erg\")
                (:generate \"http://www.delph-in.net/erg\")))";
    ;;
    jacy)
      echo "(lkb::read-script-file-aux 
             \"${LOGONROOT}/dfki/jacy/lkb/script.chasen\")";
      echo "(tsdb:tsdb :cpu :jacy+chasen :task :parse :file t 
             :count 1 ${options})";
      echo "(tsdb:tsdb :cpu :jacy :task :generate :reset nil
             :count 1 ${options})";
      echo "(setf tsdb::*www-title* :jacy)";
      echo "(setf tsdb::*www-custom-js* \"jacy\")";
      echo "(setf tsdb::*www-introduction* \"jacy\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"日本語が面白いぞ.\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/jacy\")
                (:generate \"http://www.delph-in.net/jacy\")))";
    ;;
    srg)
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/upf/srg/lkb/script\")";
      echo "(tsdb:tsdb :cpu :srg :task :parse :file t 
             :count 4 ${options})";
      echo "(setf tsdb::*www-title* :srg)";
      echo "(setf tsdb::*www-custom-js* \"srg\")";
      echo "(setf tsdb::*www-introduction* \"srg\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"El muchacho sucedió a la muchacha en el trono.\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/srg\")
                (:generate \"http://www.delph-in.net/srg\")))";
    ;;
    cst)
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/cst/danish/lkb/script\")";
      echo "(tsdb:tsdb :cpu :cst :task :parse :file t 
             :count 4 ${options})";
      echo "(setf tsdb::*www-title* :cst)";
      echo "(setf tsdb::*www-custom-js* \"cst\")";
      echo "(setf tsdb::*www-introduction* \"cst\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"kvinden underviser i filmhistorie\")";
      echo "(setf tsdb::*www-urls* nil)";
    ;;
    gg)
      echo "(lkb::read-script-file-aux \"${LOGONROOT}/dfki/gg/lkb/script\")";
      echo "(tsdb:tsdb :cpu :gg@pet :task :parse :file t 
             :count 2 ${options})";
      echo "(tsdb:tsdb :cpu :gg :task :generate :reset nil
             :count 1 ${options})";
      echo "(setf tsdb::*www-title* :gg)";
      echo "(setf tsdb::*www-custom-js* \"gg\")";
      echo "(setf tsdb::*www-introduction* \"gg\")";
      echo "(setf tsdb::*www-comparisons* nil)";
      echo "(setf tsdb::*www-sample* 
              \"Karl lacht.\")";
      echo "(setf tsdb::*www-urls*
              '((:parse \"http://www.delph-in.net/gg\")
                (:generate \"http://www.delph-in.net/gg\")))";
    ;;
    *)
      echo "www: configuration \`${setup}' not supported yet; exit.";
      exit 1;
    ;;
  esac

  echo "(tsdb::www-initialize :port ${port})";
  cat -;

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