#!/bin/sh

#
# Confidential property of Sybase, Inc.
# Copyright 1987, 2003
# Sybase, Inc.  All rights reserved.
# Unpublished rights reserved under U.S. copyright laws.
#
# This software contains confidential and trade secret information of Sybase,
# Inc.   Use,  duplication or disclosure of the software and documentation by
# the  U.S.  Government  is  subject  to  restrictions set forth in a license
# agreement  between  the  Government  and  Sybase,  Inc.  or  other  written
# agreement  specifying  the  Government's rights to use the software and any
# applicable FAR provisions, for example, FAR 52.227-19.
# Sybase, Inc. One Sybase Drive, Dublin, CA 94568, USA
#

usage()
{       
    echo "Usage: "
    echo "    ${PROGNAME} [-v] [-U ASE username] [-P ASE password] [-S ASE Service] [-f properties file]"
    echo ""
    echo "    ASE username: Username to login to ASE with to verify start permissions."
    echo "                  This must be supplied or it will be prompted for. "
    echo "    ASE password: Password to login to ASE with to verify start permissions."
    echo "    ASE Service: Entry in interfaces, sql.ini, or LDAP identifying ASE to log into."
    echo "    properties file: Contains properties used for initialization."
    echo "          Default is: $SYBASE/WS-15_0/props/ws.properties."
    echo "    -v: Version information."
    echo ""
    echo "${PROGNAME} execution complete at `date`"
    echo ""

    exit 1
}

PROGNAME=`basename $0`

echo ""
echo "${PROGNAME} initiating execution at `date`."
echo ""

if [ "`echo -n`" = "-n" ]; then
    ECHOOPT=""
    ECHOSUFF="\c"
else
    ECHOOPT="-n"
    ECHOSUFF=""
fi

if [ "$SYBASE" = "" ]
then
    echo "The environment variable SYBASE must be set."
    echo "Please retry after setting the SYBASE environment variable."
    usage 
fi

if [ "$SYBASE_WS" = "" ]
then
    echo "The environment variable SYBASE_WS is not set."
    SYBASE_WS=WS-15_0
fi

#
#  Find out where Java is. Our order of searching is:
#
#  1. The environment variable SYBASE_JRE6. By default, InstallShield 
#     will populate this environment variable correctly.
#
#  2. The Java installation specified by JAVA_HOME.
#
#  If we cannot locate a java executeable, we exit with an error to the
#  end user.
#


if [ "$SYBASE_JRE6" = "" ]
then
    echo "The environment variable SYBASE_JRE6 is not set."
    if [ "$JAVA_HOME" = "" ]
    then
        echo "The environment variable JAVA_HOME is also not set."
        echo "ERROR: Could not find a valid Java installation."
        echo "Please verify ASE installation or use the environment variable \$SYBASE_JRE6  "
        echo "or \$JAVA_HOME to specify a JRE location. "

        exit 1
    else
        echo "Using \$JAVA_HOME to locate Java."
        SYBASE_JRE6=$JAVA_HOME
    fi
fi

JRE=$SYBASE_JRE6
JAVA=${JRE}/bin/java

#
#  We ensure that the $JAVA exists and is executable.
#

if [ ! -x "$JAVA" ]
then
    echo "ERROR: Could not find java executable at $JAVA. "
    echo "Please verify that a valid JRE installation exists. "
    echo ""
    exit 1
fi

#
# We need to set LIBPATH before accessing JRE
# on AIX
#
if [ "`uname -s`" = "AIX" ]
then
    export LIBPATH=$JRE/bin/classic:$JRE/bin
fi

PROP_FILE=${SYBASE}/${SYBASE_WS}/props/ws.properties

USERNAME=
PASSWORD=
SERVICE=

# Is the password specified on the command line?
# If not, we must prompt for it.

PASSWORD_ARG=0

# For all of the command line arguments, we must be able to
# process the arg with or without a space between the argument
# and the value.
#
# If there is a space between the arugument and the value, the
# value is in $2, which we retrieve and shift the command line
# args. If there is not a space, we use 'expr' to extract the
# value.

while [ $# -ge 1 ]
do
    case $1 in
    -f*) if [ "$1" = "-f" ]
         then
             PROP_FILE=$2 ; shift ;
         else
	     PROP_FILE=`echo "${1}" | sed -n -e 's/^-f\(.*\)$/\1/p'` ;
         fi;;

    -U*) if [ "$1" = "-U" ]
         then
             USERNAME=$2 ; shift ;
         else
	     USERNAME=`echo "${1}" | sed -n -e 's/^-U\(.*\)$/\1/p'` ;
         fi ;;
    -P*) if [ "$1" = "-P" ]
         then
             if [ "$2" = "" ]
             then

#    For -P "" or -P '', we have to do a shift, but if -P is at the
#    end of the argument list, we cannot do a shift, so verify we
#    are not at the end of the argument chain. This issue does not
#    arise for other arugments since they do not accept "" as a
#    valid value.

                  if [ $# -ge 1 ]
                  then
                      shift
                 fi
                 PASSWORD_ARG=1;
              else
                  case "$2" in 

#    If -P is specified with no argument, we have to assume a null
#    password.
#
                  -*) PASSWORD_ARG=1;;

#    In this case there is a password specified on the command
#    line.
#
                   *) PASSWORD=$2; shift; PASSWORD_ARG=1;;
               esac
             fi
         else
	     PASSWORD=`echo "${1}" | sed -n -e 's/^-P\(.*\)$/\1/p'` ;
         fi;;
 
    -S*) if [ "$1" = "-S" ] 
         then
             SERVICE=$2 ; shift ;
         else
	     SERVICE=`echo "${1}" | sed -n -e 's/^-S\(.*\)$/\1/p'` ;
         fi ;;
    -v) VERSION=1 ;;
    *) usage ;;

    esac

    if [ $# -ge 1 ]
    then
        shift
    fi
done

SYBASE_WS_LIB=${SYBASE}/${SYBASE_WS}/lib

PRCLASSPATH=${SYBASE_WS_LIB}/axis.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jaxrpc.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/saaj.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/commons-logging-1.0.4.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/commons-discovery-0.2.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/log4j-1.2.4.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/wsdl4j-1.5.1.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE}/jConnect-6_0/classes/jconn3.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE}/jConnect-6_0/classes/jTDS3.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/xercesImpl.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/xmlParserAPIs.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/javax.servlet.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/server.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/sqlx.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/flexlm.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/org.mortbay.jetty.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/tools.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jcert.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jnet.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jsse.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jce1_2_2.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/sunjce_provider.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/ws.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE}/shared/lib/dsparser.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE}/${SYBASE_WS}/props

echo "Starting ASE Web Services ..."

ADDDEFINES="-Dsybase.home=$SYBASE -Docs.home=$SYBASE_OCS -Dcom.sybase.ase.ws.libtcl=$SYBASE_TCL_CFG"

# CR 539537
# For HP-UX, sysproperty java.net.preferIPv4Stack should be set to 
# enable ipv6
if [ "`uname -s`" = "HP-UX" ]
then
    ADDDEFINES="$ADDDEFINES -Djava.net.preferIPv4Stack=false"
fi

cd ${SYBASE}/${SYBASE_WS}/producer

if [ "$VERSION" = "" ]
then
    echo "Using SYBASE_JRE6 as $SYBASE_JRE6"
    echo "Using SYBASE as $SYBASE" 
    echo "Using SYBASE_WS as $SYBASE_WS" 
    
    if [ "$USERNAME" = "" ]
    then
        echo "A valid ASE username with sa_role priveleges must be specified to start ASE Web Services."
        echo "Please enter ASE Username: \c "
        read USERNAME
    fi

    if [ "$SERVICE" = "" ]
    then
        echo "A valid ASE service must be specified to start ASE Web Services."
        echo "Please enter ASE Servicename: \c "
        read SERVICE
    fi

#   If the password is not specified on the command line,
#   we must prompt for it.

    if [ "$PASSWORD_ARG" = "0" ]
    then
        echo "Please enter ASE Password : \c "
        stty -echo
        read PASSWORD
        stty echo
    fi

    EPASSWORD=`$JAVA -classpath ${PRCLASSPATH} com.sybase.ase.ws.security.Encrypt ${PASSWORD}`

    echo "${JAVA} ${ADDDEFINES} -classpath ${PRCLASSPATH} com.sybase.ase.ws.producer.MainServer -properties ${PROP_FILE} -username ${USERNAME} -password ******** -service ${SERVICE} > /dev/null 2>&1 "
    ${JAVA} ${ADDDEFINES} -classpath ${PRCLASSPATH} com.sybase.ase.ws.producer.MainServer -properties ${PROP_FILE} -username ${USERNAME} -password "${EPASSWORD}" -service ${SERVICE} > /dev/null 2>&1 &
else
    ${JAVA} -classpath ${PRCLASSPATH} com.sybase.ase.ws.producer.MainServer -version 
fi

echo "Please check the webservice.log file for any errors or informational messages."

echo ""
echo "${PROGNAME} execution complete at `date`"
echo ""
