#! /bin/sh
#
# Portions of this file Copyright 1999-2005 University of Chicago
# Portions of this file Copyright 1999-2005 The University of Southern California.
#
# This file or a portion of this file is licensed under the
# terms of the Globus Toolkit Public License, found at
# http://www.globus.org/toolkit/download/license.html.
# If you redistribute this file, with or without
# modifications, you must include this notice in the file.
#

DELIM="#"
EXEC="org.globus.bootstrap.Bootstrap org.globus.tools.ChangePassPhrase"
DEF_OPTIONS=""
DEF_CMD_OPTIONS=""
EGD_DEVICE="/dev/urandom"

updateOptions() {
     
  if [ "X$2" != "X" ] ; then
    GLOBUS_OPTIONS="$GLOBUS_OPTIONS -D$1=$2"
  fi

}

####### MAIN BODY ##########

if [ ! -d "$GLOBUS_LOCATION" ] ; then
  echo "Error: GLOBUS_LOCATION invalid or not set: $GLOBUS_LOCATION" 1>&2
  exit 1
fi

LOCALCLASSPATH=$GLOBUS_LOCATION/lib/bootstrap.jar:$GLOBUS_LOCATION/lib/cog-url.jar:$GLOBUS_LOCATION/lib/axis-url.jar

### SETUP OTHER VARIABLES ####

updateOptions "GLOBUS_LOCATION" "$GLOBUS_LOCATION"
updateOptions "java.endorsed.dirs" "$GLOBUS_LOCATION/endorsed"
updateOptions "X509_USER_PROXY" "$X509_USER_PROXY"
updateOptions "X509_CERT_DIR" "$X509_CERT_DIR"
updateOptions "GLOBUS_HOSTNAME" "$GLOBUS_HOSTNAME"
updateOptions "GLOBUS_TCP_PORT_RANGE" "$GLOBUS_TCP_PORT_RANGE"
updateOptions "GLOBUS_TCP_SOURCE_PORT_RANGE" "$GLOBUS_TCP_SOURCE_PORT_RANGE"
updateOptions "GLOBUS_UDP_SOURCE_PORT_RANGE" "$GLOBUS_UDP_SOURCE_PORT_RANGE"

if [ -c "$EGD_DEVICE" -a  -r "$EGD_DEVICE" ]; then
    updateOptions "java.security.egd" "file://$EGD_DEVICE"
fi

if [ "X$IBM_JAVA_OPTIONS" = "X" ] ; then
  IBM_JAVA_OPTIONS=-Xquickstart
  export IBM_JAVA_OPTIONS
fi

if [ $# -gt 0 ]; then
  if [ "X${DEF_CMD_OPTIONS}" != "X" ]; then
    set - ${GLOBUS_OPTIONS} -classpath ${LOCALCLASSPATH} ${EXEC}  ${DEF_CMD_OPTIONS} "$@"
  else
    set - ${GLOBUS_OPTIONS} -classpath ${LOCALCLASSPATH} ${EXEC} "$@"
  fi
else
  if [ "X${DEF_CMD_OPTIONS}" != "X" ]; then
    set - ${GLOBUS_OPTIONS} -classpath ${LOCALCLASSPATH} ${EXEC}  ${DEF_CMD_OPTIONS}
  else
    set - ${GLOBUS_OPTIONS} -classpath ${LOCALCLASSPATH} ${EXEC}
  fi
fi

OLD_IFS=${IFS}
IFS=${DELIM}
for i in ${DEF_OPTIONS} ; do
  IFS=${OLD_IFS}
  DEFINE=`echo $i|cut -d'=' -f1`
  if [ "$DEFINE" != "$i" ]; then
    VALUE="`echo $i|cut -d'=' -f2-`"
    set - $DEFINE="$VALUE" "$@"
  else
    set - $DEFINE "$@"
  fi
  IFS=${DELIM}
done
IFS=${OLD_IFS}

### EXECUTE ############

if [ "X$JAVA_HOME" = "X" ] ; then
  _RUNJAVA=java
 else 
  _RUNJAVA="$JAVA_HOME"/bin/java
fi

exec $_RUNJAVA "$@"
