#!/bin/sh

#+
#  Name:
#     voclientd
#
#  Purpose:
#
#  Description:
#     This shell script invokes the VOClient daemon.
#     It's not very complicated, but performs some argument manipulation
#     prior to invoking java with the right classpath and classname.
#
#     1. If a class path is specified using either the CLASSPATH
#        environment variable or the -classpath flag to this script,
#        it will be added to the application classpath.
#
#     2. Any initial command-line arguments which look like they are destined
#        for java itself (starting with -D or -X) will be sent to java,
#        and the others will be sent to the application.

#  Requisites:
#     - java should be on the path.
#
#     - the application jar file should exist in the same directory as this
#       command.
#
#  Credits:
#     Based on the STILTS startup script by Mark Taylor (Starlink)
#     Original version - Michael Fitzpatrick, NOAO, June 2006
#     VOClient integration - DCT 2006-July-10
#     Modified for NVOSS bin,java/lib install - DCT August 2006
#     Modified for VO-CLI distro - Michael Fitzpatrick, NOAO, January 2008
#     Generalized for VO-CLI distro - Michael Fitzpatrick, NOAO, January 2009
#-


# The following jar files come from the NVOSS Software distribution but are
# included with the VO-CLI distribution as well.  They are needed to support
# the voclientd interface and are assumed to be in a directory called
# 'voclient.jars' in the same directory as the 'voclientd' command (this
# script).



# Determine the BIN and LIB directories.
if test -n "$VOCLI_HOME" -a -f "$VOCLI_HOME/voclientd"; then
    appjar="$VOCLI_HOME/voclient.jar"

else

    #  Find where this script is located.
    bindir="`dirname $0`"
    appjar="$bindir/voclient.jar"


    # Make sure it looks reasonable.
    if test ! -f "${appjar}"
    then
       echo 1>&2 "Can't find VOClient daemon classes in ${jardir}"
       exit 1
    fi


    #  Locate the application jar file.
    for j in $voc_jars; do
       if test -z "$appjar" -a -f "$bindir/$j"; then
          appjar="$bindir/$j"
	  jardir="$bindir/voclient.jars/"
       fi
    done


    # Add the VOClient jar to the beginning of the CLASSPATH.
    if test -f "${appjar}"; then
       CLASSPATH="${appjar}:${CLASSPATH}"
    fi
fi


#  Pull out any arguments which look to be destined for the java binary.
javaArgs="-Xmx1024m"
while test "$1"
do
   if echo $1 | grep -- '^-[XD]' >/dev/null; then
      javaArgs="$javaArgs $1"
      shift
   elif [ "$1" = "-classpath" -a -n "$2" ]; then
      shift
      export CLASSPATH="$1"
      shift
   else
      break
   fi
done

#  Check for Cygwin and transform paths.
case "`uname`" in
  CYGWIN*)
    if test -n "$CLASSPATH"; then
       CLASSPATH=`cygpath --path --windows "${appjar}:$CLASSPATH"`
    else
       CLASSPATH=`cygpath --windows "${appjar}"`
    fi
  ;;
  *)
    CLASSPATH="${appjar}:${CLASSPATH}"
  ;;
esac

# Execute the command.
java \
   $javaArgs \
   -classpath $CLASSPATH \
   voclient.VOClientd \
   "$@"

