#!/bin/bash

X11HOME="/project/cl/X11R6"
awk="/opt/gnu/bin/gawk"
tsct_home="/export/apache/virtual/cl-www/docs/tsnlp/tsct"
tsct="${tsct_home}/bin/tsct"
date="$(/bin/date)"
remote="${REMOTE_HOST}"

echo Content-type: text/html
echo

expand() {
  echo "$*" | \
  ${awk} 'BEGIN { RS="&"; FS="=" }
          /^[a-zA-Z0-9_]*=/ { printf("%s=\047%s\047\n", $1, $2); }
          /english\.[xy]=/ { printf("language=english\n", $2); }
          /french\.[xy]=/ { printf("language=french\n", $2); }
          /german\.[xy]=/ { printf("language=german\n", $2); }'
} # expand()
 
normalize() {
  echo "$*"| /bin/sed -e 's/%0A//g' \
                      -e 's/++*/ /g' \
                      -e 's/%21/!/g' \
                      -e 's/%22/"/g' \
                      -e 's/%24/$/g' \
                      -e 's/%26/\&/g' -e 's/&&/\&/g' \
                      -e 's/%27/"/g' \
                      -e 's/%28/(/g' \
                      -e 's/%29/)/g' \
                      -e 's/%2B/\+/g' \
                      -e 's/%3A/:/g' \
                      -e 's/%3C/</g' \
                      -e 's/%3D/=/g' -e 's/==/=/g' \
                      -e 's/%3E/>/g' \
                      -e 's/%3F/?/g' \
                      -e 's/%5B/[/g' \
                      -e 's/%5D/]/g' \
                      -e 's/%5E/^/g' \
                      -e 's/%60/"/g' \
                      -e 's/%7C/|/g' -e 's/||/|/g' \
                      -e 's/%7E/~/g' -e 's/match(\(..*\),\(..*\))/\1 ~ \2/g' \
                      -e 's/ *\. *$//' -e 's/ *$//'
} # normalize

quote() {
  echo "$*" | \
#
# the removal of double quotes is required to work around a bug in Mosaic
# preventing the proper interpretation of `&quot;C&quot;' (31-jul-95 -- oe)
#
  ${awk} '{ for(i = 1; i <= NF; i++) {
              if($i ~ /^".+"$/) {
                printf("\140%s\047 ", substr($i, 2, length($i) - 2));
              } # if
              else {
                printf("%s ", $i);
              } # else
            } # for
            printf("\n");
          }' | \
  /bin/sed -e 's/&/\&amp;/g' \
           -e 's/>/\&gt;/g' \
           -e 's/</\&lt;/g' \
           -e 's/"/\&quot;/g' 
} # quote()

title() {

cat << EOF

  <title>Interactive tsct(1) Browser Interface</title>

  <h1>
  Interactive tsct(1) Browser Interface</h1>
  <hr>

EOF

} # title()

firsttime() {

cat << EOF

  <p>
  From this page you are given on-line (though read-only) access to the
  TSNLP test suite construction tool (tsct(1)) and an instructive
  subset (a few hundred test items) of the test data available (in
  order to take advantage of the 
  <i>full set of test data</i>, the TSNLP <a href="/tsnlp/tsdb/tsdb.cgi">test
  suite database</a> tsdb(1) is better suited).
  <p>
  To run the <i>interactive live demonstration</i> of the
  TSNLP graphical test suite construction tool, you will have to grant
  access to your local X11 server to allow tsct(1) to produce
  graphical output on your workstation display as a remote client.
  <p>
  Some of the people out there who are highly concerned with network
  security issues may want to read the X(1) and xhost(1) man pages
  first before giving remote access to their X11 display; probably,
  however, a vast majority of people do not even want to care. 
  We promise the demonstration will be worth the effort.
  <p>

EOF

} # firsttime()

anytime() {

cat << EOF

  To run tsct(1) on our server host and redirect its output
  to your local workstation display you have to:
  <ul>
  <li> make sure that (i) either the machine
       \`<tt>tsnlp.dfki.uni-sb.de</tt>' is on your X11 server access
       control list (in a Unix environment run \`<tt>xhost
       tsnlp.dfki.uni-sb.de</tt>'); or that (ii) access control for  
       your display server is completely disabled (e.g. many people may
       consider the very open-minded \`<tt>xhost +</tt>' the default); and
  <li> supply the complete address (either in numeric or fully
       domain-qualified form) of your local X server to the text input
       box below;
       the DFKI worldwide web server attempts to guess your server data
       from the information provided by your www client (if available)
       as a default in the input box: if the information is missing,
       incomplete or simply incorrect please give  the valid data.
  </ul>
  <p>

EOF

} # anytime()

error() {

  echo "<hr>"
  echo "<b>Error: client</b> "
#  echo " \`<tt>tsnlp.dfki.uni-sb.de'</tt> "
  echo "<b>failed to connect to</b> "
  echo "<tt> </tt>\`<tt>${guess}</tt>'"
  echo "<hr>"
  echo "<p>"

} # error()

input() {

  echo "<form action=\"/tsnlp/tsct/tsct.cgi\">"
  echo "<b>X11 display server to connect tsct(1) to: "
  echo "<input type=\"text\" name=\"display\" value=\"${guess}\" size=\"35\">"
  echo "</b></form>"

} # input()

windows() {

cat << EOF

  The TSNLP test suite construction tool organizes test data and
  annotations at four basic levels of representation that correspond to
  four independent tsct(1) windows:
  <form action="/tsnlp/tsct/tsct.cgi">
  <dl>
    <dt> <input type="radio" name="window" value="items" checked>
         <b>Core Data</b><br>
         The core of the data collection are the
         individual <i>test items</i> (sentences, phrases et al.)
         together with all general, categorial, and structural
         information that is independent of phenomenon, application,
         and user parameters.
         Besides the actual string of words, annotations at this level
         include (i) bookkeeping records (date, author, origin, and item
         id), (ii) the item format, length, category, and wellformedness
         code, (iii) the (morpho-)syntactic categories and string
         positions of lexical and, where uncontroversial, phrasal
         constituents, and (iv) an abstract dependency structure.
    <dt> <input type="radio" name="window" value="phenomena">
         <b>Phenomenon Data</b><br>
         Based on a hierarchical classification of <i>phenomena</i>
         (e.g. verb valency being a subtype to general complementation)
         each phenomenon description is identified by its phenomenon
         identifier, supertype(s), interaction with other phenomena, and
         presupposition of such.
         Moreover, the set of (syntactic) parameters that is relevant in
         the analysis of a token phenomenon (e.g. the number and type of
         complements in the case of verb valency) is determined.
         Individual test items can be assigned to one or several
         phenomena and annotated according to the respective parameters.
    <dt> <input type="radio" name="window" value="sets">
         <b>Test Sets</b><br>
         As an optional descriptive level in-between the test item and
         phenomenon levels, pairs or sets of grammatical and
         ill-formed items can be grouped into <i>test sets</i>.
         Thus, it can be encoded how, for example, test items that
         originate from the systematic variation of a single
         parameter relate to each other.
    <dt> <input type="radio" name="window" value="profiles">
         <b>User &amp; Application Profiles</b><br>
         Information that typically correlates with different
         evaluation and application types in the use of a test suite is
         factored from the remainder of the data into <i>user &amp;
         application profiles</i>.
         For the core TSNLP test suite (aiming to be neutral
         with respect to different user and application types) the
         tsct(1) profile window merely serves as a template that
         is to be extended when customizing the test suite.
  </dl>

EOF

#  echo "<b>TSNLP test data currently available in"
#  if [ "${language}" = "english" ]; then
#    echo "<input type="radio" name="language" value="english" checked>"
#    echo "<i>English</i><tt> </tt>"
#    echo "<input type="radio" name="language" value="french">"
#    echo "<i>French</i><tt> </tt>or<tt> </tt>"
#    echo "<input type="radio" name="language" value="german">"
#    echo "<i>German</i>;"
#  else \
#    if [ "${language}" = "french" ]; then
#      echo "<input type="radio" name="language" value="english">"
#      echo "<i>English</i><tt> </tt>"
#      echo "<input type="radio" name="language" value="french" checked>"
#      echo "<i>French</i><tt> </tt>or<tt> </tt>"
#      echo "<input type="radio" name="language" value="german">"
#      echo "<i>German</i>;"
#    else \
#      echo "<input type="radio" name="language" value="english">"
#      echo "<i>English</i><tt> </tt>"
#      echo "<input type="radio" name="language" value="french">"
#      echo "<i>French</i><tt> </tt>or<tt> </tt>"
#      echo "<input type="radio" name="language" value="german" checked>"
#      echo "<i>German</i>;"
#    fi
#  fi
#  echo "</b>"
  echo "<input type=hidden name=display value=\"${guess}\">"
  echo "<input type=image src=\"/tsnlp/tsct/english.gif\" name=english>"
  echo "<input type=image src=\"/tsnlp/tsct/french.gif\" name=french>"
  echo "<input type=image src=\"/tsnlp/tsct/german.gif\" name=german>"
  echo "</form>"

} # windows()

options() {

  cat << EOF

  <hr>
  <b>[<a href="/tsnlp/objective.html">objective</a>]</b>
  <b>[<a href="/tsnlp/consortium.html">consortium</a>]</b>
  <b>[<a href="/tsnlp/publications.html">publications</a>]</b>
  <b>[<a href="/tsnlp/staff.html">staff</a>]</b>
  <b>[<a href="/tsnlp/tsdb/tsdb.cgi">test suite database</a>]</b>
  <b>[<a href="/tsnlp/">TSNLP home</a>]</b>
  <hr><i>last modified: 5-oct-95 
  (<a href="http://cl-www.dfki.uni-sb.de/~oe">oe@cl.dfki.uni-sb.de</a>)</i>

EOF

} # options()


# -----------------------------------------------------------------------------
# body of `tsct.cgi'
# -----------------------------------------------------------------------------

#echo "${QUERY_STRING}" >> /tmp/xxx

title

if [ "${QUERY_STRING}" ]; then
  eval "$(expand "$QUERY_STRING")";
  if [ "${display}" ]; then
    oquery="${display}"
    guess="$(normalize "${display}")"
    if [ "${guess%%:[0-9]}" = "${guess}" -a \
         "${guess%%:[0-9].[0-9]}" = "${guess}" ]; then
      guess="${guess}:0.0"
    fi

  fi
  if ! ${X11HOME}/bin/xset -display "${guess}" -q > /dev/null 2>&1; then
    error
    guess=""
    anytime
    input
  else \
    if [ -z "${window}" ]; then
      windows
    else \
      windows
      DISPLAY=${guess}
      export DISPLAY
      if [ "${window}" = "items" ]; then
        items="/tmp/.tsct.$$.items"
        /bin/rm -f ${items}
        cp ${tsct_home}/${language}/sample.items ${items}
        (
          ${tsct} -rf ${tsct_home}/bin/item.ari -file ${items} && \
          /bin/rm -f ${items} ${items}~
        ) < /dev/null > /dev/null 2>&1 &
      fi
      if [ "${window}" = "phenomena" ]; then
        phenomena="/tmp/.tsct.$$.phenomena"
        /bin/rm -f ${phenomena}
        cp ${tsct_home}/${language}/sample.phenomena ${phenomena}
        (
          ${tsct} -rf ${tsct_home}/bin/phenomenon.ari -file ${phenomena} && \
          /bin/rm -f ${phenomena} ${phenomena}~
        ) < /dev/null > /dev/null 2>&1 &
      fi
      if [ "${window}" = "sets" ]; then
        sets="/tmp/.tsct.$$.sets"
        /bin/rm -f ${sets}
        cp ${tsct_home}/${language}/sample.sets ${sets}
        (
          ${tsct} -rf ${tsct_home}/bin/set.ari -file ${sets} && \
          /bin/rm -f ${sets} ${sets}~
        ) < /dev/null > /dev/null 2>&1 &
      fi
      if [ "${window}" = "profiles" ]; then
        profiles="/tmp/.tsct.$$.profiles"
        /bin/rm -f ${profiles}
        cp ${tsct_home}/${language}/sample.profiles ${profiles}
        (
          ${tsct} -rf ${tsct_home}/bin/profile.ari -file ${profiles} && \
          /bin/rm -f ${profiles} ${profiles}~
        ) < /dev/null > /dev/null 2>&1 &
      fi
    fi
  fi
else \
  if [ "${REMOTE_HOST}" ]; then
    guess="${REMOTE_HOST}:0.0"
  else \
    if [ "${REMOTE_ADDRESS}" ]; then
      guess="${REMOTE_ADDRESS}:0.0"
    else \
      guess=""
    fi
  fi
  firsttime
  anytime
  input
fi

options

