#!/bin/csh
#
# purpose:  assigns an operating environment for the 
#           JGOFS system OtherOptions server
#
# NB: this file belongs in the directory configured as ScriptAlias /jg/
#     on this HTTPD server.  (typically htmlbin or optbin)
#     Generated from a template by a makefile in the source tree
#
#    March 23, 2004 rcg/wjs
#         Add Matlab run time library to library path.  
#    October 25, 1999 clh
#        attempt to provide a bit more error handling
#        list of environment variables established:
#
#	OPTHOME == top of tree
#	OPTRUNDIR == to be appended to OPTHOME to find PWD
#	MYADDR == how to refer to host running these scripts
#	DEFAULT_PORT == which port on this host to use
#	JGTEMP == top level temporary space
#	USETEMPDIR == temporary space to use as tree for current object
#	USETEMPADDR == url for USETEMPDIR
# *** 
#   and then, a set of commonly used strings referring to the object
# ***
#	OBJECT == a jgofs object string (no jg/serv/, no extension)
#	OBJEXT == same as above, with extension (not used often)
#	URLOBJX == object spec as usable from a browser (jg/serv and ext.)
#	URLOBJ == object spec as above with no extension (jg/serv)
#	PROTOLEV == just the extension
#	SUBSELS == subselections list as used on WWW
#	DISPSS == un-trigram'ed subselections
#
#    December 14, 1999 clh
#	add another variable DISPOBJ, the display-style object,
#	complete with subselections if present
#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# NVB: until a better way is found, when changing this file:
#    Please edit the .tmp (template) file in source tree, and
#    modify build-opt-env-pl.tmp with same changes you make here
#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#   20 Jul 05.  WJS
#	USETEMPADDR
# version October 25, 1999, clh
#   note explaining 'firsttimeseen' var, chmod stmt added
#
setenv OPTHOME /data302/wsass/port80/production-Apr17
setenv OPTRUNDIR optbin
# 
setenv MYADDR optserv1.whoi.edu:80
setenv DEFAULT_PORT 80
setenv JGTEMP /data302/wsass/port80/production-Apr17/htdocs/jgofsopt/80
#
# check that the temporary directory exists and that it has all setup
#    info/data in it that any of the menu items needs
#  mod November 2, 1998 - clh - move here so all items need not dup code
#	note that REMOTE_HOST is environment of HTTPD
#  mod December 15, 1998 - clh - add conditional use of REMOTE_HOST
#	hosts w/o DNS name cannot serve - REMOTE_ADDR always served
if (${?REMOTE_HOST}) then
  set remotehost_dir=$REMOTE_HOST
else
  set remotehost_dir=$REMOTE_ADDR
endif

set realtempdir=${JGTEMP}/$remotehost_dir
setenv USETEMPDIR $realtempdir
setenv USETEMPADDR http://optserv1.whoi.edu:80/jgofsopt/80/$remotehost_dir

# check that the temporary directory exists and that it has all setup
#    info/data in it that any of the menu items needs
#
if ( ! -e $realtempdir ) then
 mkdir -p $realtempdir
 chmod 755 $realtempdir
endif

foreach i (${OPTHOME}/setup/*)
  if ( ! -e $realtempdir/$i:t) then
   cp $i $realtempdir
  endif
end

#
# since all menu items have same need to deal with PATH_INFO and 
#  QUERY_STRING to display and use object(s) and subselections,
#  if any, then below are provided for consistency and efficiency
#
#	OBJ = complete object spec, machine, path, objname
#	DISPOBJ = same as OBJ if no subselections
#		   else, OBJ(subselections)
#	OBJEXT = OBJ, with extention (protocol, for ex: .html0)
#       SUBSELS = any subselections, with trigrams
#	DISPSS = un-quoted subselections, used for display
#
if (${?PATH_INFO}) then
  set tmp=$PATH_INFO
  set ext=$tmp:e
  if ("$ext" == "") then
     set ext=`cat $realtempdir/proto-lev`
     setenv PROTOLEV $ext
     setenv OBJEXT "$tmp.$ext"
  else
     setenv OBJEXT $tmp
     echo $ext > $realtempdir/proto-lev
  endif
  setenv PROTOLEV $ext
  setenv OBJECT $tmp:r
          echo "$OBJECT" > $realtempdir/obj
  setenv URLOBJ `${OPTHOME}/bin/fixurl "${OBJECT}"`
  setenv URLOBJX "${URLOBJ}.$ext"
  setenv SUBSELS ""
  setenv SUBSELS_QS ""
  setenv DISPSS ""
  setenv DISPOBJ "${OBJECT}"
endif

if ( ${?QUERY_STRING} && "$QUERY_STRING" != "" ) then
  set subsels_qs="$QUERY_STRING"
  setenv SUBSELS_QS "$subsels_qs"
#
# Check QS for htmlescaped operator - if found, unescape for subsels
#      ex: press&lt;200 --->  press<200
#
  set found=`${OPTHOME}/bin/strpbrk "$subsels_qs" "&;"`

  if ($found == "yes") then
    set subsels=`${OPTHOME}/bin/htmlunesc "$subsels_qs"`
  else
    set subsels=$subsels_qs 
  endif
  set subsels=`${OPTHOME}/bin/trigram_util "$subsels"`
  setenv SUBSELS "$subsels"
          echo "$SUBSELS" > $realtempdir/sel
#
# Check QS for operator that should be htmlescaped for displaying
#      ex: press<200 --->  press&lt;200
#
  set found=`${OPTHOME}/bin/strpbrk $subsels "<>"`

  if ($found == "yes") then
    set dispss=`${OPTHOME}/bin/htmlesc "$subsels"`
  else
    set dispss="$subsels_qs"
  endif
  set dispss=`${OPTHOME}/bin/trigram_util -un "$dispss"`
  setenv DISPSS "$dispss"
  setenv DISPOBJ "${OBJECT}($dispss)"
endif
#printenv
