#!/bin/sh
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.

###############################################
# Sourcing macros
. ${BASE_DIR}/${PRODUCT_DIR}/lib/psmacros
###############################################

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
BASE=${BASE_DIR}/$PS_PRODUCT_DIR
PS_HOME=$BASE
IS_HOME=${IDSAME_BASE_DIR}/$IDSAME_PRODUCT_DIR

FILE_GW_INSTANCE_SETUP=$PS_CONFIG_DIR/platform.conf
FILE_NLP_PID=$PS_VAR_DIR/netletd.pid
FILE_VERSION=${BASE_DIR}/$PS_PRODUCT_DIR/lib/SRAversion.properties

IS_CHROOT=0           # Running from chroot env? 1=yes 0=no
CHROOT_DIR=CHROOTDIR  # Chroot directory

GETTEXT=/usr/bin/gettext
TEXTDOMAIN=gwscripts
TEXTDOMAINDIR=${BASE_DIR}/$PS_PRODUCT_DIR/locale
export TEXTDOMAIN
export TEXTDOMAINDIR
export LANG
NEWLINE='\n'

# Common components, utilities
NOHUP=/usr/bin/nohup
JSS_NSS_NSPR_LIBPATH=$JSS_NSS_NSPR_LIB_DIR
JSS_JAR=$JSS_JAR_LIB_DIR/jss3.jar

#################################################################

###############################################
#
###############################################
GrabConfig() {
    ANSWER=`grep "^$2$3" $1 | uniq | sed -e "s/$2$3//"`
}
GrabConfigSingle() {
    ANSWER=`grep "^$2$3" $1 | uniq | sed -e "s/$2$3//" | tr -d "\t " `
}

###############################################
#
###############################################
GetJDKVersion() {
    AWK=/usr/bin/awk

    if [ -x ${JAVA_HOME}/bin/java ]; then
        ANSWER_VERSION=`${JAVA_HOME}/bin/java -fullversion 2>&1 | ${AWK} '{print $4}' | sed -e "s/\"//g"`
    else
        ANSWER_VERSION=""
    fi

    INDEX1_4=`echo ${ANSWER_VERSION} | ${AWK} '{print index($0,"1.4")}'`
    INDEX1_3=`echo ${ANSWER_VERSION} | ${AWK} '{print index($0,"1.3")}'`

    if [ "${INDEX1_4}" = "1" ]; then
        JDK_VERSION="1.4"
    else
        if [ "${INDEX1_3}" = "1" ]; then
            JDK_VERSION="1.3"
        fi
    fi
}

###############################################
#
###############################################
GearUp() {
    # cd Out of CDROM dir, otherwise CDROM can't eject
    cd $PS_VAR_DIR/debug

    GrabConfig $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.jdk.dir" "="
    JAVA_HOME=$ANSWER
    GetJDKVersion

    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.host" "="
    GATEWAY_HOST=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.port" "="
    GATEWAY_PORT=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.protocol" "="
    GATEWAY_PROTOCOL=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.notification.url" "="
    GATEWAY_NOTIFICATION_URL=$ANSWER
    IDENTITY_NOTIFICATION_URL=$GATEWAY_PROTOCOL://$GATEWAY_HOST:$GATEWAY_PORT/$GATEWAY_NOTIFICATION_URL

    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "portal.server.instance" "="
    IS_CONFIG_NAME=$ANSWER

    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "portal.server.host" "="
    PORTAL_HOST=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "portal.server.port" "="
    PORTAL_PORT=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.retries" "="
    TRY_COUNT=$ANSWER

    GrabConfig $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "gateway.certdir" "="
    DIRECTORY_CERT_DB=$ANSWER
    FILE_CERT_DB_PASSWORD=$DIRECTORY_CERT_DB/.jsspass
    FILE_CERT_NICKNAME=$DIRECTORY_CERT_DB/.nickname

    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "http.proxySet" "="
    HTTP_PROXY_SET=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "http.proxyHost" "="
    HTTP_PROXY_HOST=$ANSWER
    GrabConfigSingle $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE "http.proxyPort" "="
    HTTP_PROXY_PORT=$ANSWER

    #To workaround JDK JNI bug on Linux
    OSTYPE=`/bin/uname -s`
    if [ "$OSTYPE" = "Linux" ]; then
        EXTRA_LIBS="$JAVA_HOME/jre/lib/i386/native_threads"
        LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$EXTRA_LIBS"
        export LD_LIBRARY_PATH
    fi

}

###############################################
#
###############################################
StopNetletProxy()
{
    echo "`$GETTEXT 'Stopping netlet proxy...'`"
    if [ -s $FILE_NLP_PID.$GW_INSTANCE ] ; then
        GrabConfig $FILE_NLP_PID.$GW_INSTANCE "pid" "="
        if [ "$ANSWER" != "" ]; then
            kill -9 $ANSWER
        fi
    fi
    rm -f $FILE_NLP_PID.$GW_INSTANCE
}

###############################################
# Set CLASSPATH and LD_LIBRARY_PATH
###############################################

SetPath() {

    # Classpath
    IS_CLASSPATH="$IDSAME_CONFIG_DIR/config:$IS_HOME/lib:$IS_HOME/locale:$IS_HOME/lib/am_sdk.jar:$IS_HOME/lib/am_services.jar:$IS_HOME/lib/am_logging.jar:$IS_HOME/lib/servlet.jar"
    JCE_CLASSPATH="$IS_HOME/lib/jce1_2_1.jar:$IS_HOME/lib/local_policy.jar:$IS_HOME/lib/US_export_policy.jar:$IS_HOME/lib/sunjce_provider.jar"
    # Classpath is w.r.t. working directory that is $PS_HOME
    NLP_CLASSPATH="lib:locale:lib/netletproxy.jar:lib/certadmin.jar:lib/ssl.jar:lib/x509v1.jar"
    NLP_CLASSPATH="$IS_CLASSPATH:$JCE_CLASSPATH:$JSS_JAR:$NLP_CLASSPATH"

    if [ -z "$CLASSPATH" ] ; then
	    CLASSPATH="$NLP_CLASSPATH"
    else
	    CLASSPATH="$NLP_CLASSPATH:$CLASSPATH"
    fi
    export CLASSPATH

    # LD_LIBRARY_PATH
    if [ -z "$LD_LIBRARY_PATH" ] ; then
	    LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$PS_HOME/lib/solaris/sparc"
    else
	    LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$PS_HOME/lib/solaris/sparc:$LD_LIBRARY_PATH"
    fi
    export LD_LIBRARY_PATH

}

###############################################
#
###############################################
Start() {

    cd $PS_HOME

    echo "`$GETTEXT 'Starting netlet proxy...'`"
    CMD="$JAVA_HOME/bin/java $NATIVE_THREAD -ms64m -mx128m -classpath $CLASSPATH -DSRAP_CONFIG_DIR=$PS_CONFIG_DIR $DEFINES1 $DEFINES2 $DEFINES3 $DEFINES4 $DEFINES5 $BOOT_CLASSPATH com.sun.portal.netlet.proxy.NetletProxy $FILE_NLP_PID.$GW_INSTANCE"

    if [ "$IS_CHROOT" -eq 1 ] ; then
        ($NOHUP chroot $CHROOT_DIR ${CMD})  > /dev/null 2>&1 &
    else
        ($NOHUP $CMD) > /dev/null 2>&1 &
    fi

    if [ "$!" != "" ]; then
        echo pid=$! >> $FILE_NLP_PID.$GW_INSTANCE
    fi
}

###############################################
#
###############################################
StartNetletProxy() {
    if [ -s $FILE_NLP_PID.$GW_INSTANCE ] ; then
        StopNetletProxy
    fi

    #
    GearUp

    # Use native threads if JDK supports '-native' option
    native_test1=`${JAVA_HOME}/bin/java -native 2>&1 | grep -i "illegal\ argument"`
    native_test2=`${JAVA_HOME}/bin/java -native 2>&1 | grep -i "not\ supported"`
    if [ -z "$native_test1" ] && [ -z "$native_test2" ]; then
        NATIVE_THREAD=-native
    else
        NATIVE_THREAD=""
    fi

    # Defines or Java system properties
    DEFINES1="-Dsun.net.inetaddr.ttl=0 -Djava.protocol.handler.pkgs=com.iplanet.services.comm"
    DEFINES2="-Dcom.iplanet.am.notification.url=$IDENTITY_NOTIFICATION_URL -Dgateway.notification.url=$GATEWAY_NOTIFICATION_URL"
    DEFINES3="-Dgateway.keybase=$DIRECTORY_CERT_DB -Dgateway.pass=$FILE_CERT_DB_PASSWORD -Dgateway.nickname=$FILE_CERT_NICKNAME"
    DEFINES4="-Djava.util.logging.manager=com.sun.identity.log.LogManager -Djava.util.logging.config.file=$IS_HOME/lib/LogConfig.properties"

    DEFINES5="-Dgateway.profilename=$GW_INSTANCE -Dconf.suffix=$GW_INSTANCE"
    if [ "$IS_CONFIG_NAME" -ne "" ]; then
        DEFINES5="$DEFINES5 -Dserver.name=$IS_CONFIG_NAME"
    fi

    if [ "${JDK_VERSION}" = "1.3" ]; then
        BOOT_CLASSPATH="-Xbootclasspath:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/jre/lib/i18n.jar:$IS_HOME/lib/jdk_logging.jar"
    fi

    # Start
    Start
}

###############################################
#
###############################################
ParseArguments() {
    while getopts n: name
    do
        case $name in
            n)  nflag=1
                GW_INSTANCE="$OPTARG"
                ;;

            ?)  echo "`$GETTEXT 'Usage:'` $0 { [-n <profile-name>] start | [-n <profile-name>] stop | watchdog <on|off> | version }"
                exit 2
                ;;
        esac
    done
}

###############################################
# Main
###############################################

if test `id | /usr/bin/awk '{print $1}'` != "uid=0(root)"
then
	echo "`$GETTEXT 'You must be root user to run'` $0."
	exit 0
fi

nflag=
GW_INSTANCE=

SetPath

ParseArguments $*
shift `expr $OPTIND - 1`

umask 000
ulimit -n 10240

case "$1" in
    'start')
        if [ -z "$nflag" ]; then
            for x in $FILE_GW_INSTANCE_SETUP.*; do
                if [ -r $x ]; then
                    GW_INSTANCE=`echo $x | sed -e "s#$FILE_GW_INSTANCE_SETUP.##"`
                    StartNetletProxy
                fi
            done
        else
            if [ ! -f $FILE_GW_INSTANCE_SETUP.$GW_INSTANCE ]; then
                echo "`$GETTEXT 'Netlet Proxy cannot get started. Configuration for specified profile does not exist!'`"
                exit 1
            fi

            StartNetletProxy
        fi
        ;;

    'watchdog')
	    CRONTAB="/var/spool/cron/crontabs/root"
	    CRON="/etc/init.d/cron"
	    if [ "$2" = "on" ]; then
		    if  grep 'checknetletd' $CRONTAB >/dev/null ; then
			    echo "`$GETTEXT 'Netletd watchdog already enabled.'`"
			    exit
		    fi

		    echo "`$GETTEXT 'Enabling netletd watchdog...'`"
		    echo "0-59 * * * * ${PS_HOME}/bin/checknetletd 5 > /dev/null 2>&1" >> $CRONTAB
		    $CRON stop
		    $CRON start
	    fi

	    if [ "$2" = "off" ]; then
		    echo "`$GETTEXT 'Disabling netletd watchdog...'`"
		    cp $CRONTAB /tmp/.cron
		    sed -e '/checknetletd/d' /tmp/.cron > $CRONTAB
		    rm -f /tmp/.cron
		    $CRON stop
		    $CRON start
	    fi
	    ;;

    'stop')
        if [ -z "$nflag" ]; then
            for x in $FILE_NLP_PID.*; do
                if [ -r $x ]; then
                    GW_INSTANCE=`echo $x | sed -e "s#$FILE_NLP_PID.##"`
                    StopNetletProxy
                fi
            done
        else
            StopNetletProxy
        fi
        ;;

    'version')
        if [ -f $FILE_VERSION ]; then
            GrabConfig $FILE_VERSION "build" "="
            BUILD=$ANSWER
            GrabConfig $FILE_VERSION "productname" "="
            PNAME=$ANSWER
            GrabConfig $FILE_VERSION "productversion" "="
            echo "$BUILD $PNAME $ANSWER"
        fi
        ;;

    *)
        echo "`$GETTEXT 'Usage:'` $0 { [-n <profile-name>] start | [-n <profile-name>] stop | watchdog <on|off> | version }"
        ;;
esac

