#!/bin/sh
#
# Collection of common functions used by JMS client scripts
#

#
# $1 - the name of the script
#
display_help() {
program_name=`basename $1`
    cat <<EOF

Usage: $program_name <sun [-appclient] | jboss | jg> [-debug] [app_specific_args]

EOF
}


#
# $@ - command line arguments
# 
# Exits with 0 and writes the command line at stdout or exits with a non-zero value and
# writes the error message at stderr
#
generate_command_line() {

reldir=`dirname $0`

if [ "$1" = "" -o "$1" = "-help" ]; then
    exit 1;
fi

# parse command line args
while [ "$1" != "" ]; do
        if [ "$1" = "sun" -o "$1" = "jboss" -o "$1" = "jg" ]; then
            server=$1
	elif [ "$1" = "-appclient" ]; then
            appclient=true
            # send it down, java runtime needs it
            args="$args $1"
	elif [ "$1" = "-debug" ]; then
            debug=true
	else
            #echo "ERROR: unknown argument: $1" 1>&2
            # pass it to the java runtime
            args="$args $1"
	fi
	shift
done

if [ "$server" = "" ]; then
    echo "ERROR: unknown target JMS provider. Use jboss, sun or jg." 1>&2
    exit 1
fi

if [ "$appclient" = "true" -a "$server" = "jboss" -o "$appclient" = "true" -a "$server" = "jg" ]; then
    echo "WARNING: cannot run a JBoss/JG client using Sun's appclient; -appclient ignored." 1>&2
    appclient=""
fi

reldir=`dirname $0`
(cd $reldir/../etc; rm jndi.properties; ln -s jndi.properties.$server jndi.properties) 

if [ "$debug" = "true" ]; then
    JVM_DEBUG="-Xint -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=12345,suspend=y"
fi

#echo "RELDIR: $reldir, file $reldir/../local/config:"`file $reldir/../local/config` 1>&2
if [ -f $reldir/../local/config ]; then
    source $reldir/../local/config
fi

COMMON_CLASSPATH=\
$reldir/../etc:\
$LOCAL_CLASSPATH:\
$reldir/../output:\
$reldir/../lib/log4j.jar:\
$reldir/../lib/jgroups-core-2.2.1.jar:\
$reldir/../lib/jms-1.1.jar:\
$reldir/../lib/clester.jar

if [ "$server" = "jboss" ]; then
    #
    # JBoss
    #
    if [ -z $JBOSS_HOME ]; then 
	echo "ERROR: JBOSS_HOME is not set. Please set it and try again." 1>&2
	exit 1
    fi
    #
    # run the class directly, generate the command line
    # 
echo \
$JAVA_HOME/bin/java $JVM_DEBUG -cp $COMMON_CLASSPATH:\
$JBOSS_HOME/client/jnp-client.jar:\$JBOSS_HOME/client/jboss-common-client.jar:\
$JBOSS_HOME/client/jbossall-client.jar: \
$main_class "$args"

elif [ "$server" = "sun" ]; then
    #
    # Sun appserver
    #
    if [ -z $SUNWAPPSERV_HOME ]; then 
	echo "ERROR: SUNWAPPSERV_HOME is not set. Please set it and try again." 1>&2
	exit 1
    fi
    if [ "$appclient" = "true" ]; then
        if [ "$debug" = "true" ]; then
            echo "WARNING: cannot control debugging flags when running from appclient!" 1>&2
        fi
        #
        # Run the code using the Sun's appclient, generate the command line
        #
echo \
"export APPCPATH=$LOCAL_CLASSPATH:$reldir/../lib/log4j.jar:$reldir/../etc:$reldir/../lib/jgroups-core-2.2.1.jar:$reldir/../lib/jms-1.1.jar; 
$SUNWAPPSERV_HOME/bin/appclient -client $reldir/../$app_jar $args"
    else
        #
        # run directly the class, set the JNDI by myself, generate the command line
        #
echo \
$JAVA_HOME/bin/java $JVM_DEBUG -cp $COMMON_CLASSPATH:\
$SUNWAPPSERV_HOME/lib/j2ee.jar:$SUNWAPPSERV_HOME/lib/install/applications/jmsra/imqjmsra.jar:\
$SUNWAPPSERV_HOME/lib/appserv-rt.jar:$SUNWAPPSERV_HOME/lib/appserv-admin.jar \
$main_class "$args"
    fi

else 
    #
    # JG JMS server, generate the command line
    #
echo \
$JAVA_HOME/bin/java $JVM_DEBUG -cp $COMMON_CLASSPATH $main_class "$args"
fi


}





