#! /bin/sh

#
# $Id: srm,v 1.16.2.1 2007/02/22 07:50:33 tigran Exp $
# $Author: tigran $
#
# changes by Neha - to make sure java exists.

#DEBUG=true
#SECURITY_DEBUG=true 
#DEBUG=false

javapath=""
big=0
correctjavapath=""
version=0
getVersion() {
                javapath=$1/bin/java
                version=`$javapath -version  2>&1 >/dev/null | tr -d "\"" | awk '/java version/{print
$3 }'`
                #echo "$version found in $javapath"
		#echo ""
                #echo "big before is $big"
                compare $big $version
                if [ $? != 1 ] ; then
                        big=$version
                        correctjavapath=$javapath
                fi
}


compare() {
        temp=$(for i in "$1" "$2" ; do echo $i ; done)
        temp1=$(for i in "$1" "$2" ; do echo $i ; done | sort)
        #echo "temp $temp"
        #echo "temp1 $temp1"
        if [ "$temp" == "$temp1" ] ; then
                #echo "equal"
                return 0
        else
                #echo "notequal"
                return 1
        fi
}


setjavalocation() {
    
  for i in /usr /usr/local /usr/java /usr/local/java /opt  /opt/java /opt ; do 
      for j in $i/*sdk* $i/*jdk* $i/*java* $i/*jre* $i; do    
         if [ -x $j/bin/java ] ; then
            getVersion $j
         fi
      done
  done

  #echo "The most recent java found is $correctjavapath"
  java_location=$correctjavapath
}

args=$* 

#Check if java already exists in PATH
which java >/dev/null 2>&1
is_java_in_path=$?
if [ $is_java_in_path = 0 ]
then
	java_location=java
else
	#If not, then check if JAVA_HOME is defined
	if [ "$JAVA_HOME" != "" ]
	then
		java_location=$JAVA_HOME/bin/java
		ls $JAVA_HOME/bin/java
		if [ $? = 1 ] 
		then
			#echo "Either JAVA_HOME defined incorrectly or java doesnot exist under $JAVA_HOME/bin"
			setjavalocation
		fi
	else
		#If not, then check for java in usual places
		#echo "java not in $PATH and JAVA_HOME not defined."
		#echo "will look in other places..."
		setjavalocation

	fi
fi
#If we find any of the dir above
if [ "$java_location" == "" ]; then
	echo "JAVA_HOME not defined. java doesnot exist in PATH " >&2
	echo "Also java not found in usual places" >&2
	echo "Make sure java is installed. Exiting ..."  >&2
	return 1
fi

#echo "java_location is: $java_location"
if [ "$SRM_PATH" = "" ]
    then
    echo SRM_PATH is not set >&2
    exit 1
fi

if [ ! -f $SRM_PATH/lib/srm_client.jar ]
    then
    echo cannot find $SRM_PATH/lib/srm_client.jar >&2
    exit 1
fi

if [ ! -f $SRM_PATH/lib/glue/GLUE-STD.jar ]
    then
    echo cannot find $SRM_PATH/lib/glue/GLUE-STD.jar >&2
    exit 1
fi

SRM_CP=$SRM_PATH/lib/srm_client.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/srm.jar

    # glue
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/GLUE-STD.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/dom.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/servlet.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/jnet.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/jsse.jar
SRM_CP=$SRM_CP:$SRM_PATH/lib/glue/jcert.jar

    # globus cog
for i in $SRM_PATH/lib/globus/*.jar
  do
  SRM_CP=$SRM_CP:$i
done

# security
SRM_CP=$SRM_CP:$SRM_PATH/lib/security/bcprov-jdk14-122.jar

for i in $SRM_PATH/lib/axis/*.jar
  do
  SRM_CP=$SRM_CP:$i
done


OPTIONS="-Djava.protocol.handler.pkgs=org.globus.net.protocol"
SRMCP_OPTIONS=""
if [ "$DEBUG" = "true" ]
    then
    SRM_CP=${SRM_PATH}/conf:$SRM_CP
    OPTIONS=" ${OPTIONS} -Dlog4j.configuration=log4j.properties.axis"
    OPTIONS=" ${OPTIONS} -Delectric.logging=SOAP,HTTP"
fi

if [ "$SECURITY_DEBUG" = "true" ]
    then
    SRM_CP=${SRM_PATH}/conf:$SRM_CP
    OPTIONS=" ${OPTIONS} -Dlog4j.configuration=log4j.properties.security -Dlog4j.debug=true "
fi

if [ "$SRM_CONFIG" = "" ]
then 
    SRM_CONFIG=$HOME/.srmconfig/config.xml
fi

if [ "$SRM_CONFIG" != "" -a  "$SRM_CONFIG" != "NONE" -a  ! -f "$SRM_CONFIG" ]
then
    SRM_CONFIG_PARENT=`dirname $SRM_CONFIG`
    mkdir -p $SRM_CONFIG_PARENT
fi
   

    # if env variable for user proxy  is defined, use it
if [ "$X509_USER_PROXY" != "" ]
    then
    x509_user_proxy=$X509_USER_PROXY
else
    if [ -r /tmp/x509up_u`id -u` ]
	then
	x509_user_proxy=/tmp/x509up_u`id -u`
    fi
fi	

if [ "$x509_user_proxy" != "" ]
    then
    SRMCP_OPTIONS="$SRMCP_OPTIONS -x509_user_proxy=$x509_user_proxy"
fi

if [ "$SRM_CONFIG" != "NONE" -a ! -f $SRM_CONFIG ]
    then
    echo configuration file not found, configuring srmcp >&2
    if [ "$x509_user_proxy" = "" ]
	then
	x509_user_proxy=$HOME/k5-ca-proxy.pem
    fi
    if [ -d $HOME/.globus/certificates ]
	then
	x509_user_trusted_certs=$HOME/.globus/certificates
    else
	if [ -d /etc/grid-security/certificates ]
	    then
	    x509_user_trusted_certs=/etc/grid-security/certificates
	else
                #use /etc/grid-security/certificates anyway
	    x509_user_trusted_certs=/etc/grid-security/certificates
	fi
    fi
    use_proxy=true
    url_copy=$SRM_PATH/sbin/url-copy.sh
    mapfile=$SRM_PATH/conf/SRMServerV1.map
    
    cmd="$java_location -cp $SRM_CP $OPTIONS gov.fnal.srm.util.SRMDispatcher \
	    -mapfile=$mapfile \
	    -urlcopy=$url_copy \
	    -x509_user_proxy=$x509_user_proxy \
	    -x509_user_key=$HOME/.globus/userkey.pem \
	    -x509_user_cert=$HOME/.globus/usercert.pem \
	    -x509_user_trusted_certificates=$x509_user_trusted_certs \
	    -use_proxy=$use_proxy \
	    -srmcphome=$SRM_PATH \
	    -save_conf=$SRM_CONFIG \
	    $args"
    
    if [ "$DEBUG" = "true" ]
	then
	echo $cmd
    fi
    $cmd
    return_code=$?
    if [ $return_code = 0 ]
	then
	if /bin/false ; then
	    if tty -s
		then
		    echo would you like to view/edit configuration file?[y/n] >&2
		    read -t 60 ANSWER
		    if [ "$ANSWER" = "y" ]
			then
			${VISUAL:-${EDITOR:-vi}} $SRM_CONFIG
		    fi
	    fi
	else
	    echo "created configuration file in $SRM_CONFIG" >&2
	fi
    fi
fi

URL_COPY=$SRM_PATH/sbin/url-copy.sh
if [ "$SRM_CONFIG" != "NONE" -a -f $SRM_CONFIG ]
    then 
    SRMCP_OPTIONS="-conf=$SRM_CONFIG $SRMCP_OPTIONS"
fi
#cd $SRM_PATH/bin
cmd="$java_location -cp $SRM_CP $OPTIONS gov.fnal.srm.util.SRMDispatcher  $SRMCP_OPTIONS $args"
if [ "$DEBUG" = "true" ]
    then
    echo $cmd
fi
$cmd

