#!/bin/bash

unset DISPLAY;
unset LUI;

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

action=export;
export="";
increment=1000;
source="--source";
target=${LOGONROOT}/tmp;
active="t-active >= 1";
thinning="nil";
composite="nil";
index="nil";
treebank=erg;
tsdb=${LOGONROOT}/bin/tsdb;

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --binary)
      source="";
      shift 1;
    ;;
    --source)
      source="--source";
      shift 1;
    ;;
    --cat)
      cat=--cat;
      shift 1;
    ;;
    --export)
      action=export;
      tmp="$LOGONTMP/.redwoods.oe.io.$$";
      echo "${2}" | sed 's/,/\n/g' > ${tmp};
      while read i; do export="${export} :${i}"; done < ${tmp};
      shift 2;
    ;;
    --run)
      action=run;
      shift 1;
    ;;
    --thin)
      action=thin;
      shift 1;
    ;;
    --compress)
      action=compress;
      shift 1;
    ;;
    --update)
      action=update;
      shift 1;
    ;;
    --gold)
      gold="${2}";
      shift 2;
    ;;
    --erg)
      treebank=erg;
      script=${LOGONROOT}/lingo/erg/lkb/script;
      home=${LOGONROOT}/lingo/erg/tsdb/gold;
      skeletons=${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english;
      shift 1;
    ;;
    --terg)
      treebank=terg;
      script=${LOGONROOT}/lingo/terg/lkb/script;
      home=${LOGONROOT}/lingo/terg/tsdb/gold;
      skeletons=${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english;
      shift 1;
    ;;
    --redwoods)
      treebank=redwoods;
      base=${LOGONROOT}/lingo/redwoods;
      script=${base}/erg/lkb/script;
      home=${base}/tsdb/home;
      skeletons=${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english;
      shift 1;
    ;;
    --wikiwoods)
      treebank=wikiwoods;
      script=${LOGONROOT}/lingo/redwoods/erg/lkb/script;
      home=${LOGONROOT}/lingo/lkb/src/tsdb/wikiwoods;
      skeletons=${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english;
      shift 1;
    ;;
    --terg)
      treebank=terg;
      script=${LOGONROOT}/lingo/terg/lkb/script;
      home=${LOGONROOT}/lingo/terg/tsdb/gold;
      skeletons=${LOGONROOT}/lingo/lkb/src/tsdb/skeletons/english;
      shift 1;
    ;;
    --jacy)
      treebank=jacy;
      script=${LOGONROOT}/dfki/jacy/lkb/script;
      home=${LOGONROOT}/dfki/jacy/tsdb/gold;
      skeletons=${LOGONROOT}/dfki/jacy/tsdb/skeletons;
      shift 1;
    ;;
    --hinoki)
      treebank=hinoki;
      base=${LOGONROOT}/ntu/hinoki;
      script=${base}/jacy/lkb/script;
      home=${base}/tsdb/home;
      skeletons=${LOGONROOT}/dfki/jacy/tsdb/skeletons;
      shift 1;
    ;;
    --srg)
      treebank=srg;
      script=${LOGONROOT}/upf/srg/lkb/script;
      home=${LOGONROOT}/upf/srg/tsdb/gold;
      skeletons=${LOGONROOT}/upf/srg/tsdb/skeletons;
      shift 1;
    ;;
    --tibidabo)
      treebank=tibidabo;
      base=${LOGONROOT}/ub/tibidabo
      script=${LOGONROOT}/upf/srg/lkb/script;
      home=${base}/tsdb/home;
      skeletons=${LOGONROOT}/upf/srg/tsdb/skeletons;
      shift 1;
    ;;
    --default)
      home=${LOGONROOT}/lingo/lkb/src/tsdb/home;
      shift 1;
    ;;
    --increment)
      increment=${2};
      if [ "${increment}" = "nil" -o "${increment}" = "none" ]; then
        unset increment;
      fi
      shift 2;
    ;;
    --percentile)
      percentile=${2};
      shift 2;
    ;;
    --composite)
      composite="t";
      shift 1;
    ;;
    --index)
      index="t";
      shift 1;
    ;;
    --gc)
      gc=yes;
      shift 1;
    ;;
    --thinning)
      thinning="t";
      shift 1;
    ;;
    --condition)
      extra=${2};
      shift 2;
    ;;
    --active)
      case ${2} in
        nil|null|all) unset active; ;;
        resolved) active="t-active == 1"; ;;
        reduced) active="t-active >= 1"; ;;
      esac
      shift 2;
    ;;
    --target)
      target=${2};
      shift 2;
    ;;
    --prologue)
      proloque=${2};
      shift 2;
    ;;
    --epilogue)
      epilogue=${2};
      shift 2;
    ;;
    *)
      echo "usage: redwoods [ options ] profile; exit.";
      exit 1;
  esac
done

if [ -n "${export}" ]; then
  export="(${export# })";
elif [ "${action}" = "export" ]; then
  export="(:derivation :mrs :dependencies)";
fi

if [ -n "${1}" ]; then 
  profile=${1};
fi

if [ -z "${LOGONLOG}" ]; then
  LOGONLOG=${LOGONROOT}/log;
fi
log=${LOGONLOG}/${treebank}.$(basename ${profile}).${action}.${date}.log;

case "${action}" in
  export|thin|compress)
    export TSDB_HOME=${home}/${profile}
    if ${tsdb} -verify -quiet; then
      :
    else
      echo "redwoods: invalid \`${treebank}' profile \`${profile}'; exit."
      exit 1;
    fi
  ;;
esac

case "${action}" in
  export)
  {
    if [ ! -d ${target} ]; then
      { /bin/rm -f ${target}; mkdir ${target}; } > /dev/null 2>&1;
    fi

    low=$(${tsdb} -query 'select i-id from item' | sort -n | head -1);
    all=$(${tsdb} -query 'select i-id from item' | sort -n | tail -1);

    while [ ${low} -le ${all} ]; do
      if [ -n "${composite}" -o -z "${increment}" ]; then
        high=$[${all} + 1];
      else
        high=$[${low} + ${increment}];
      fi
      echo; echo "exporting \`${1}' [$low -- $high]"; echo;
      condition="i-id >= ${low} && i-id < ${high} && readings >= 1";
      if [ ! -z "${active}" ]; then
        condition="(${condition}) && (${active})";
      fi
      if [ ! -z "${extra}" ]; then
        condition="(${condition}) && (${extra})";
      fi
      low=${high};
      {
        loadup;
        if [ -n "${gc}" ]; then gc; fi
        echo "(lkb:read-script-file-aux \"${script}\")";
        echo "(tsdb:tsdb :home \"${home}\")";
        echo "(tsdb:tsdb :skeletons \"${skeletons}\")";
        echo "(setf tsdb::*redwoods-export-values* '${export})";
        echo "(setf tsdb::*redwoods-thinning-export-p* ${thinning})";
        echo "(setf mrs::*eds-include-quantifiers-p* t)";
        echo "(setf mrs::*eds-include-vacuous-relations-p* nil)";
        epilogue;
        echo "(tsdb::export-trees \"${profile}\" 
               :path \"${target}\" :condition \"${condition}\"
               :compositep ${composite} :index ${index})";
        echo "(excl:exit)";
      } | ${LOGONROOT}/bin/logon ${source} ${cat} \
            -I base -locale no_NO.UTF-8 -qq
    done
  } 2>&1 | tee ${log}
  if [ "${index}" != "nil" ]; then
    echo ${profile} >> ${target}/.index;
  fi
  ;;

  run)
  if [ -n "${base}" ]; then cd ${base}; fi
  {
    loadup;
    if [ -n "${gc}" ]; then gc; fi
    echo "(lkb:read-script-file-aux \"${script}\")";
    echo "(tsdb:tsdb :home \"${home}\")";
    echo "(tsdb:tsdb :skeletons \"${skeletons}\")";
    if [ -n "${increment}" ]; then
      echo "(setf tsdb::%redwoods-items-increment% ${increment})";
      echo "(setf tsdb::%redwoods-items-percentile% nil)";
    elif [ -n "${percentile}" ]; then
      echo "(setf tsdb::%redwoods-items-percentile% ${percentile})";
    fi
    epilogue;
    echo "(load \"${profile}\")"; \
    echo "(excl:exit 0 :no-unwind t)";
  } | ${LOGONROOT}/bin/logon ${source} ${cat} \
        -I base -locale no_NO.UTF-8 -qq 2>&1 | tee ${log}
  ;;

  thin)
  {
    #
    # erase the target directory, if need be, and invoke the normalizer
    #
    target="${profile}.1";
    if [ -a ${home}/${target} ]; then /bin/rm -rf ${home}/${target}; fi
    loadup;
    if [ -n "${gc}" ]; then gc; fi
    echo "(lkb:read-script-file-aux \"${script}\")";
    echo "(tsdb:tsdb :home \"${home}\")";
    echo "(tsdb:tsdb :skeletons \"${skeletons}\")";
    echo "(setf tsdb::*redwoods-trees-hook* nil)";
    echo "(setf tsdb::*redwoods-semantix-hook* \"mrs::get-mrs-string\")";
    echo "(setf tsdb::*redwoods-thinning-normalize-p* t)";
    epilogue;
    echo "(tsdb::browse-trees \"${profile}\" :strip \"${target}\")";
    echo "(excl:exit 0 :no-unwind t)";
  } | ${LOGONROOT}/bin/logon ${source} ${cat} \
        -I base -locale no_NO.UTF-8 -qq 2>&1 | tee ${log}
  ;;

  update)
  {
    if [ -z "${gold}" ]; then
      echo "redwoods: \`--gold' argument required for update.";
      exit 1;
    fi
    loadup;
    if [ -n "${gc}" ]; then gc; fi
    echo "(lkb:read-script-file-aux \"${script}\")";
    echo "(tsdb:tsdb :home \"${home}\")";
    echo "(tsdb:tsdb :skeletons \"${skeletons}\")";
    echo "(setf tsdb::*redwoods-trees-hook* nil)";
    echo "(setf tsdb::*redwoods-semantix-hook* nil)";
    echo "(setf tsdb::*redwoods-thinning-normalize-p* nil)";
    epilogue;
    echo "(tsdb::purge-test-run \"${profile}\" :action :tree)";
    echo "(tsdb::browse-trees 
           \"${profile}\" :interactive nil :gold \"${gold}\")";
    echo "(excl:exit 0 :no-unwind t)";
  } | ${LOGONROOT}/bin/logon ${source} ${cat} \
        -I base -locale no_NO.UTF-8 -qq 2>&1 | tee ${log}
  ;;

esac