#!/bin/ksh

##############################################################
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.#
# Use is subject to license terms.                           #
#                                                            #
# "@(#)sscs    1.30    05/04/18     SMI"                             #
#                                                            #
# "@(#)sscs    1.22    03/01/07    SMI"                      #
#                                                            #
# @version 1.22                                              #
##############################################################

EXIT_CODE=0
BASE_DIR=`dirname $0`
HOME=$HOME/
LIB_HOME=$BASE_DIR/../lib
export HOME=$HOME/

# set path to C client
CCLIENT=$BASE_DIR/pclient

# specify keystore in your env
KEYSTORE=/opt/se6x20/.keystore

# set up environment and start a new proxy server
start_proxy () {
    # set up client
    CLASSPATH=$LIB_HOME/cli.jar
    #setup for jsse for JDK/JRE 1.2.x and JDK/JRE 1.3.x
    CLASSPATH=$CLASSPATH:$LIB_HOME/jcert.jar
    CLASSPATH=$CLASSPATH:$LIB_HOME/jnet.jar
    CLASSPATH=$CLASSPATH:$LIB_HOME/jsse.jar
    #setup for soap
    CLASSPATH=$CLASSPATH:$LIB_HOME/xerces.jar
    CLASSPATH=$CLASSPATH:$LIB_HOME/soap.jar
    CLASSPATH=$CLASSPATH:$LIB_HOME/mail.jar
    CLASSPATH=$CLASSPATH:$LIB_HOME/activation.jar
    #setup for logging
    CLASSPATH=$CLASSPATH:$LIB_HOME/log4j-1.2.6.jar
    CLASSPATH=$CLASSPATH:../../../MaseratiBOL/classes
   
    # fire off a proxy
    java  \
    	-Dsscs.version=2.1.4 \
	-Duser.home=$HOME \
	-DKEYSTORE=$KEYSTORE \
	-cp $CLASSPATH \
	com.sun.netstorage.array.mgmt.cfg.cli.client.ProxyServer & 
}

# check for existance of the users .sscs_conf file,
# if it exists they may already have a proxy started, so try without
# starting a new proxy, if that fails with exit code 6, start a new
# proxy and try again.

# check for users .sscs_conf file
if [ -f $HOME/.sscs_conf ] ; then
        # found one, try the login
        $CCLIENT "$@"
	EXIT_CODE=$?
        # if the login failed with error code 6
        # start a proxy and try again
        if [ $EXIT_CODE -eq  6 ] ; then
            start_proxy
	    $CCLIENT "$@"
	    EXIT_CODE=$?
	    chmod 600 $HOME/.sscs_conf
        fi
else
        # if no .sscs_conf file exists, start a proxy and then login
        start_proxy
	$CCLIENT "$@"
	EXIT_CODE=$?
	chmod 600 $HOME/.sscs_conf
fi

if [ $EXIT_CODE -eq 6 ]; then
    echo "Connection failed!";
fi

exit $EXIT_CODE

