#!/bin/sh

# Copyright  2004 Sun Microsystems, Inc. All rights reserved. 
# 
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
# 
# U.S. Government Rights - Commercial software. Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
# 
# Use is subject to license terms. 
# 
# This distribution may include materials developed by third parties. Sun,
# Sun Microsystems, the Sun logo, Java and Sun[tm] ONE are trademarks or
# registered trademarks of Sun Microsystems, Inc. in the U.S. and other
# countries. 
# 
# Copyright  2004 Sun Microsystems, Inc. Tous droits rservs. Sun
# Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
# 
# L'utilisation est soumise aux termes du contrat de licence.
# 
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
# 
# Sun, Sun Microsystems, le logo Sun, Java et Sun[tm] ONE sont des marques
# de fabrique ou des marques dposes de Sun Microsystems, Inc. aux
# Etats-Unis et dans d'autres pays.


# assume that Identity Server instance installed remotely on some box
# and this script is to deploy it locally to websphere server 5.1 and 
# any webapp on top of websphere will remotely talk to the IS server
# through IS SDK for SSO functionalities
#


###########################################################
# Finds amutils and sources it. This function is identical
# in all scripts. Any changes made here need to made in
# other scripts too.
source_amutils()
{
	# Try to guess the location of amutils
	if [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/bin/amutils ]; then
		AMUTILS=$BASEDIR/$PRODUCT_DIR/bin/amutils
	elif [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/share/bin/amutils ]; then
		AMUTILS=$BASEDIR/$PRODUCT_DIR/share/bin/amutils
	elif [ -f ./amutils ]; then
		AMUTILS=./amutils
	elif [ -f `dirname $0`/amutils ]; then
		AMUTILS=`dirname $0`/amutils
	elif [ -f /opt/SUNWam/bin/amutils ]; then
		AMUTILS=/opt/SUNWam/bin/amutils
	elif [ -f /opt/SUNWam/share/bin/amutils ]; then
		AMUTILS=/opt/SUNWam/share/bin/amutils ]
	elif [ -f /opt/sun/identity/bin/amutils ]; then
		AMUTILS=/opt/sun/identity/bin/amutils
	elif [ -f /opt/sun/identity/share/bin/amutils ]; then
		AMUTILS=/opt/sun/identity/share/bin/amutils ]
	else
		echo "$0: FATAL: Could not find amutils. Exiting."
		exit 1
	fi

	# Source it!
	echo "$0: Sourcing $AMUTILS"
	. $AMUTILS
}

if [ ${#} -eq 0 ]; then
	INSTALL_FILE_NOT_REQUIRED=true
	source_amutils
	do_interactive
	set -- -s $IS_SILENT_INSTALL_FILE
	INSTALL_FILE_NOT_REQUIRED=false
elif [ $1 = "-s" ]; then 
    if [ ! -f $2 ]; then 
        echo Silent file not found: $2 
        exit 
    fi 
    IS_SILENT_INSTALL_FILE=$2 
    export IS_SILENT_INSTALL_FILE 
else 
    echo "Usage: amwas51config [ -s <silent> ]" 
    exit 
fi

source_amutils

# WAS51.1 variables need to be set and used here

WAS51_HOSTNAME=`echo $WAS51_HOST |nawk -F. '{print $1}'`
WAS51_CONFIG_BASE=$WAS51_HOME/config/cells/$WAS51_CELL/nodes/$WAS51_NODE/servers/$WAS51_INSTANCE
export WAS51_CONFIG_BASE
WAS51_CONFILES="$WAS51_CONFIG_BASE/server.xml "
export WAS51_CONFILES
BACKUP_DIR=$WAS51_CONFIG_BASE/.dsame
export BACKUP_DIR
WAS51_NEWLINE=""
export WAS51_NEWLINE
IS_OPTIONS="-Djava.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader -Djava.protocol.handler.pkgs=com.ibm.net.ssl.internal.www.protocol -Djava.util.logging.manager=com.sun.identity.log.LogManager"
export IS_OPTIONS
LIB_DIR=${PKGDIR}/lib
export LIB_DIR

# Begin PORTALMA
if [ "$OSTYPE" = "Linux" ]; then
	MOBILE_ACCESS_LIB_DIR=/opt/sun/mobileaccess/share/lib
else
        MOBILE_ACCESS_LIB_DIR=/opt/SUNWma/lib
fi

MOBILE_ACCESS_JARS="$MOBILE_ACCESS_LIB_DIR/wireless_rendering.jar:$MOBILE_ACCESS_LIB_DIR/wireless_rendering_util.jar:$MOBILE_ACCESS_LIB_DIR/mobile_services.jar:$MOBILE_ACCESS_LIB_DIR/ccpp-1_0.jar:$MOBILE_ACCESS_LIB_DIR/ccpp-ri-1_0.jar:$MOBILE_ACCESS_LIB_DIR/jena-1.4.0.jar:$MOBILE_ACCESS_LIB_DIR/rdffilter.jar:$MOBILE_ACCESS_LIB_DIR/locale"

is_pkg_installed $MA_PKGNAME
if [ $? -eq 0 ]; then
    MOBILE_ACCESS_JARS="$MOBILE_ACCESS_JARS:$LIB_DIR/mobile_identity.jar"
fi
# End PORTALMA

JVM_CLASSPATH_CLASSES="$JSS_JAR_PATH/jss3.jar:$LIB_DIR/xalan.jar:$LIB_DIR/xmlsec.jar:$LIB_DIR/xercesImpl.jar:$LIB_DIR/sax.jar:$LIB_DIR/dom.jar:$LIB_DIR/dom4j.jar:$LIB_DIR/jakarta-log4j-1.2.6.jar:$LIB_DIR/saaj-api.jar:$LIB_DIR/jaxrpc-api.jar:$LIB_DIR/jaxrpc-impl.jar:$LIB_DIR/jaxrpc-spi.jar:$LIB_DIR/saaj-impl.jar:$CONFIG_DIR:$LIB_DIR:$PKGDIR/locale:$JSS_JAR_FILE:$LIB_DIR/am_sdk.jar:${LIB_DIR}/am_services.jar:${LIB_DIR}/am_sso_provider.jar:$LIB_DIR/swec.jar:$LIB_DIR/acmecrypt.jar:$LIB_DIR/iaik_ssl.jar:$LIB_DIR/jaxp-api.jar:$LIB_DIR/mail.jar:$LIB_DIR/activation.jar:${LIB_DIR}/am_logging.jar:$LIB_DIR/commons-logging.jar:$LIB_DIR/jaas.jar:$LIB_DIR/jax-qname.jar:$LIB_DIR/jaxm-api.jar:$LIB_DIR/jaxm-runtime.jar:$LIB_DIR/jce1_2_1.jar:$LIB_DIR/jdk_logging.jar:$LIB_DIR/servlet.jar:$LIB_DIR/xsltc.jar:$LIB_DIR/namespace.jar:$LIB_DIR/relaxngDatatype.jar:$LIB_DIR/xsdlib.jar:$LIB_DIR/jaxb-api.jar:$LIB_DIR/jaxb-impl.jar:$LIB_DIR/jaxb-libs.jar:$LIB_DIR/jaxb-xjc.jar:${MOBILE_ACCESS_JARS}"
export JVM_CLASSPATH_CLASSES

# add path for wdeploy
JSS_JAR_FILE=${JSS_JAR_PATH}/jss3.jar
LIB_DIR=${PKGDIR}/lib
PATH=${WAS51_HOME}/bin:${JAVA_HOME}/bin:/bin:/usr/bin
export PATH
LD_LIBRARY_PATH=${JSS_SO_PATH}:${NSS_SO_PATH}:${NSPR_SO_PATH}:${WAS51_INSTANCE}/server/lib
export LD_LIBRARY_PATH
CLASSPATH=$LIB_DIR/am_sdk.jar:$LIB_DIR/am_services.jar:$LIB_DIR:$PKGDIR/locale:$JSS_JAR_FILE:$LIB_DIR/jaas.jar:$LIB_DIR/jaxp-api.jar:$LIB_DIR/sax.jar:$LIB_DIR/xercesImpl.jar:$LIB_DIR/dom.jar:$LIB_DIR/servlet.jar:/$CONFIG_DIR
export CLASSPATH
WEBAPPS_SOURCE_DIR=$PKGDIR/web-src
CONSOLE_DIR=$WEBAPPS_SOURCE_DIR/applications
PASSWORD_DIR=$WEBAPPS_SOURCE_DIR/password
COMMON_DIR=$WEBAPPS_SOURCE_DIR/common
SERVICES_DIR=$WEBAPPS_SOURCE_DIR/services
CONSOLE_DEPLOY_DIR=$WAS51_DEPLOY_DIR/applications
PASSWORD_DEPLOY_DIR=$WAS51_DEPLOY_DIR/password
COMMON_DEPLOY_DIR=$WAS51_DEPLOY_DIR/common
SERVICES_DEPLOY_DIR=$WAS51_DEPLOY_DIR/services


backupConfig()
{
 
  if [ ! -d $BACKUP_DIR ];then
    mkdir -p $BACKUP_DIR
  fi
  for file in $WAS51_CONFILES
  do
   if [ ! -f $BACKUP_DIR/$file ];then
  	cp -f $WAS51_CONFILES $BACKUP_DIR/
   fi
  done
}

setJavaOptions()
{
  # create ${WAS51_CONFIG_BASE}/.java.login.config

  file="${WAS51_CONFIG_BASE}/.java.login.config"

cat <<EOF > $file
WSLogin {
        com.ibm.ws.security.common.auth.module.proxy.WSLoginModuleProxy required delegate=com.ibm.ws.security.common.auth.module.WSLoginModuleImpl;
EOF
}

configureJavaSecurity()
{
# change java.security of websphere jdk
file=${WAS51_JDK_HOME}/jre/lib/security/java.security
cat <<EOF >> $file
login.configuration.provider=com.sun.identity.authentication.config.AMConfiguration
EOF
}

configureServerPolicy()
{
  # modifying policy if any
  file="$WAS51_CONFIG_BASE/was.policy"
  add_to_end "$file" "// IS RELATED ADDITIONS"
  add_to_end "$file" "grant {"
  add_to_end "$file" "    permission java.lang.RuntimePermission \"modifyThreadGroup\";"
  add_to_end "$file" "    permission java.lang.RuntimePermission \"setFactory\";"
  add_to_end "$file" "    permission java.lang.RuntimePermission \"accessClassInPackage.*\";"
  add_to_end "$file" "    permission java.lang.RuntimePermission \"shutdownHooks\";"
  add_to_end "$file" "    permission java.util.logging.LoggingPermission \"control\";"
  add_to_end "$file" "    permission java.util.PropertyPermission \"java.util.logging.config.class\", \"write\";"
  add_to_end "$file" "    permission java.security.AllPermission;"
  add_to_end "$file" "    permission java.io.FilePermission \"<<ALL FILES>>\", \"execute,delete\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"getLoginConfiguration\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"setLoginConfiguration\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"modifyPrincipals\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"createLoginContext.*\";"
  add_to_end "$file" "    permission java.security.securityPermission \"insertProvider.Mozilla-JSS\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"putProviderProperty.Mozilla-JSS\";"
  add_to_end "$file" "    permission java.security.SecurityPermission \"putProviderProperty.Mozilla-JSS\";"
  add_to_end "$file" "    permission java.security.SecurityPermission \"insertProvider.Mozilla-JSS\";"
  add_to_end "$file" "    permission java.security.SecurityPermission \"removeProvider.SUN\";"
  add_to_end "$file" "    permission java.security.SecurityPermission \"insertProvider.SUN\";"
  add_to_end "$file" "    permission java.security.SecurityPermission \"removeProvider.Mozilla-JSS\";"
  add_to_end "$file" "    permission javax.security.auth.AuthPermission \"doAs\";"
  add_to_end "$file" "    permission java.util.PropertyPermission \"java.security.krb5.realm\", \"write\";"
  add_to_end "$file" "    permission java.util.PropertyPermission \"java.security.krb5.kdc\", \"write\";"
  add_to_end "$file" "    permission java.util.PropertyPermission \"java.security.auth.login.config\", \"write\";"
  add_to_end "$file" "    permission javax.security.auth.kerberos.ServicePermission \"*\", \"accept\";"
  add_to_end "$file" "};"
  add_to_end "$file" "// END OF ADDITIONS FOR IS"
}

configureEndorsedDirs()
{
  if [ ! -d ${LIB_DIR}/endorsed ];then
      mkdir -p ${LIB_DIR}/endorsed
  fi
  ln -s "${LIB_DIR}/xalan.jar" "${LIB_DIR}/endorsed/xalan.jar" 2>/dev/null
  ln -s "${LIB_DIR}/xercesImpl.jar" "${LIB_DIR}/endorsed/xercesImpl.jar" 2>/dev/null
  ln -s "${LIB_DIR}/sax.jar" "${LIB_DIR}/endorsed/sax.jar" 2>/dev/null
  ln -s "${LIB_DIR}/dom.jar" "${LIB_DIR}/endorsed/dom.jar" 2>/dev/null
  ln -s "${LIB_DIR}/xsltc.jar" "${LIB_DIR}/endorsed/xsltc.jar" 2>/dev/null
}

# configure websphere
configureWAS51()
{
  echo "Checking if WebSphere Server is configed with IS"
  check_server_xml $WAS51_HOME/config/cells/$WAS51_CELL/nodes/$WAS51_NODE/servers/$WAS51_INSTANCE/server.xml xml was51
  if [ $? -ne 1 ];then  # not configed yet
  echo "Configuring WebSphere Server"
  backupConfig
  setJavaOptions
  #configureServerPolicy
  configureJavaSecurity
  configureEndorsedDirs
  setServerXml
  configStartServer

  # online help link to webserver doc root
  # ln -s $PKGDIR/public_html/online_help $WAS51_HOME/docs/online_help    2>/dev/null
  fi

}

configStartServer()
{
cd $WAS51_HOME/bin
file=startServer.sh
newfile=new$file-$$
rm -f $newfile;touch $newfile

lno=`grep -n "SunOS)" $file|nawk -F: '{print $1}'`
lno=`expr $lno \+ 1`

line=`head -$lno $file |tail -1`
mod_line="${line}:$JSS_SO_PATH"
total_lines=`wc -l $file |awk '{print $1}'`
i=1
while [ $i -le $total_lines ];do
 if [ $i -ne $lno ];then
	thisline=`head -$i $file|tail -1`
	echo "$thisline" >> $newfile
 else
	echo "$mod_line" >> $newfile
 fi
 i=`expr $i \+ 1`
done
cp -f $newfile $file
rm -f $newfile
}

setServerXml()
{
# change server.xml of websphere
hvalue=0
vvalue=0        # 0=just jvmEntries, 1=with </jvmEntries> wrapper, 2=with <classpath>
file="server.xml"
newfile="new$file-$$"
rm -f $newfile; touch $newfile

  cd ${WAS51_CONFIG_BASE}

 #LIB_DIR=${PKGDIR}/lib
for file in $WAS51_CONFILES 
do

# 1st: decide if </jvmEntries> is there for later use
if [ `grep "<classpath>" $file |wc -c` -ne 0 ];then     # has this entry
 # further check if classpath has all IS stuff
 classpath_lno=`grep -n "<classpath>" $file |nawk -F: '{print $1}'`
 classpath_line=`head -$classpath_lno $file | tail -1`
 if [ `echo $classpath_line |grep $PRODUCT_DIR |wc -c` -ne 0 ];then	# all IS stuff there
 	vvalue=3	
 else	# just empty classpath entry
	vvalue=2
 fi
elif [ `grep "</jvmEntries>" $file |wc -c` -ne 0 ];then # has this entry
 vvalue=1
fi
#echo "vvalue=$vvalue">vvalue.log
# 2nd: modify the <jvmEntries> line

jvmEntries_lno=`cat $file |grep -n "<jvmEntries"| nawk ' BEGIN { FS=":" } { print $1}'`
#lno=`cat $file |grep -n "<jvmEntries"| nawk ' BEGIN { FS=":" } { print $1}'`
total_lines=`wc -l $file |awk '{print $1}'`
i=1

while [ $i -le $total_lines ];do
 if [ $i -eq $jvmEntries_lno ];then
        doThisline $jvmEntries_lno $file
        echo "    $WAS51_NEWLINE" >>$newfile
        if [ $vvalue -eq 0 ];then       # need add 2 entries
                echo "      <classpath>$JVM_CLASSPATH_CLASSES</classpath>" >>$newfile
                echo "    </jvmEntries>" >> $newfile
        elif [ $vvalue -eq 1 ];then     # just add classpath entry
                echo "      <classpath>$JVM_CLASSPATH_CLASSES</classpath>" >>$newfile
        elif [ $vvalue -eq 2 ];then     # just append IS to exist classpath entry
		classpath_lno=`cat $file |grep -n "<classpath>"| nawk ' BEGIN { FS=":" } { print $1}'`
		classpath_line=`echo $classpath_line |sed -e 's#<classpath>##g' \
				-e 's#</classpath>##g'`
		if [ "X$classpath_line" = "X" ];then
                	classpath_line="      <classpath>$JVM_CLASSPATH_CLASSES</classpath>"
		else
                	classpath_line="      <classpath>$classpath_line:$JVM_CLASSPATH_CLASSES</classpath>" 
		fi
        fi
 elif [ $i -eq ${classpath_lno:-0} -a $vvalue -eq 2 ];then
	echo "$classpath_line" >> $newfile
 else
        head -$i $file|tail -1 >>$newfile
 fi
 i=`expr $i \+ 1`
done	# end of while 

done	# end of for

sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync;sync
sleep 5		# wait long enough for the new change reflected
cp -f $newfile $file
rm -f $newfile
}

# change 1 line with new attributes
changeline()
{
lpattern=$1
ipattern=$2
token=$3
file=${4:-server.xml}
rm_genericjvmargs=${5:-0}	# if need to remove duplicate entry, default is no

line=`grep $lpattern $file`
firstToken=`echo $line |awk '{print $1}'`
if [ $firstToken != "" ];then
 if [ ${rm_genericjvmargs:-0} -eq 1 ];then
        line=`echo $line |nawk '{for(i=1;i<=NF;i++) if (index($i,"genericJvmArguments")==0) printf("%s ",$i); if (index($NF,"genericJvmArguments")!=0) printf (">"); }'`
 fi
        tmpline=`echo $line |nawk -v x="$ipattern" -v y="$token" '{for(i=1;i<=NF;i++) {if (index($i,x)==0) printf("%s ",$i);else printf(" %s %s ",$i,y);}}'`
        WAS51_NEWLINE=`echo $tmpline |nawk '{for(i=1;i<NF;i++) printf("%s ",$i); if((substr($NF,length($NF)-1,2))=="/>") printf ("%s>",substr($NF,1,length($NF)-2)); else printf ("%s",$NF);}'`
fi
}

# change jvm config line if necessary
doThisline()
{
lineno=$1
file=${2:-server.xml}

line=`head -$lineno $file |tail -1`
firstToken=`echo $line |awk '{print $1}'`

# 1st append entry after verboseModeJNI="false"  (horizontal)
if [ $firstToken != "" ];then
          newcp=`echo $line | awk ' BEGIN { FS=" "}
        {
             hvalue=0
             if (NF > 0) {
                 for (i = 1; i <= NF; i ++) {
                     if (index($i, "initialHeapSize") != 0) {
                         hvalue+=1
                     }
                     if (index($i, "maximumHeapSize") != 0) {
                         hvalue+=2
                     }
                     if (index($i, "genericJvmArguments") != 0) {
			if (index($i,"com.sun.identity.log.s1is.LogConfigReader") == 0) {
			 hvalue+=4 
			}
			else {
                         hvalue+=8
			}
		     }
                 }
             printf("%d",hvalue)
             }
         }'`

fi

case $newcp in
0)      # nothing
 #changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\" maximumHeapSize=\"256\" genericJvmArguments=\"-Djava.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader -Djava.protocol.handler.pkgs=com.ibm.net.ssl.internal.www.protocol -Djava.util.logging.manager=com.sun.identity.log.LogManager\"" $file 1
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\" maximumHeapSize=\"256\" genericJvmArguments=\"$IS_OPTIONS\"" $file 1
 ;;
1)      # only initialHeapSize there
 #changeline "<jvmEntries" "initialHeapSize" "maximumHeapSize=\"256\" genericJvmArguments=\"-Djava.protocol.handler.pkgs=com.ibm.net.ssl.internal.www.protocol -Djava.util.logging.manager=com.sun.identity.log.LogManager -Djava.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader\"" $file 1
 changeline "<jvmEntries" "initialHeapSize" "maximumHeapSize=\"256\" genericJvmArguments=\"$IS_OPTIONS\"" $file 1
 ;;
2)      # only maximumHeapSize there
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\"" $file 0
 #changeline "<jvmEntries" "maximumHeapSize" "genericJvmArguments=\"-Djava.protocol.handler.pkgs=com.ibm.net.ssl.internal.www.protocol -Djava.util.logging.manager=com.sun.identity.log.LogManager -Djava.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader\"" $file 1
 changeline "<jvmEntries" "maximumHeapSize" "genericJvmArguments=\"$IS_OPTIONS\"" $file 1
 ;;
3)      # only initheapsize & maximumheapsize there
 changeline "<jvmEntries" "maximumHeapSize" "genericJvmArguments=\"$IS_OPTIONS\"" $file 1
 ;;
4)	# only genericjvmarguments there but no IS stuff
 mod_genericJvmArgs "<jvmEntries" $file
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\" maximumHeapSize=\"256\" genericJvmArguments=\"$mod_line\"" $file 1
 ;;
5)	# both initheapsize & genericjvmarguments there with no IS stuff
 mod_genericJvmArgs "<jvmEntries" $file
 changeline "<jvmEntries" "initialHeapSize" "maximumHeapSize=\"256\" genericJvmArguments=\"$mod_line\"" $file 1
 ;;
6)	# both maxheapsize & genericjvmarguments there with no IS stuff
 mod_genericJvmArgs "<jvmEntries" $file
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\"" $file 0
 changeline "<jvmEntries" "maximumHeapSize" "genericJvmArguments=\"$mod_line\"" $file 1
 ;;
7)	# genericjvmarguments has no IS stuff
 mod_genericJvmArgs "<jvmEntries" $file
 changeline "<jvmEntries" "maximumHeapSize" "genericJvmArguments=\"$mod_line\"" $file 1
 ;;
8)	# full IS genericjvmarguments
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\" maximumHeapSize=\"256\"" $file 0
 ;;
9)
 changeline "<jvmEntries" "initialHeapSize" "maximumHeapSize=\"256\"" $file 0
 ;;
10)
 changeline "<jvmEntries" "verboseModeJNI" "initialHeapSize=\"256\"" $file 0
 ;;
11)	# all there
 WAS51_NEWLINE=`head -$lineno $file | tail -1 `
 ;;
esac

}

mod_genericJvmArgs()
{
pattern=$1
file=$2

#IS_pattern="-Djava.protocol.handler.pkgs=com.ibm.net.ssl.internal.www.protocol -Djava.util.logging.manager=com.sun.identity.log.LogManager -Djava.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader"

lineno=`grep -n "<jvmEntries" $file |nawk -F: '{print $1}'`
line=`head -$lineno $file |tail -1`

mod_line=`echo $line|awk '{for(i=1;i<=NF;i++)if(index($i,"genericJvmArguments")!=0) printf("%s",$i);}' |nawk -F= '{printf("%s",$NF);}' |sed -e 's#"##g' \
        -e 's#>##g'`

if [ ${mod_line:-~} = "~" ];then
 mod_line="$IS_OPTIONS"
else
 mod_line="$mod_line:$IS_OPTIONS"
fi
export mod_line
}

unconfigureWAS51()
{
  echo Unconfiguring WebSphere Server
  # restore the ws and IS to its init stage (config)
  rm -f ${BASEDIR}/${PRODUCT_DIR}/lib/endorsed/dom.jar
  rm -f ${BASEDIR}/${PRODUCT_DIR}/lib/endorsed/sax.jar
  rm -f ${BASEDIR}/${PRODUCT_DIR}/lib/endorsed/xalan.jar
  rm -f ${BASEDIR}/${PRODUCT_DIR}/lib/endorsed/xercesImpl.jar

 # unconfig websphere and remove all IS related config

 cd ${WAS51_CONFIG_BASE}

 #rm -f $WAS51_HOME/docs/online_help

 for file in $WAS51_CONFILES; do
   tmpfile=$file-tmp-$$
   rm -f $tmpfile;touch $tmpfile
 
  # get line# and modified value for next use
  classpathlineno=`grep -n "<classpath>" $file | nawk -F: '{print $1}'`
  classpathline=`grep "<classpath>" $file | sed -e 's#<classpath>##g' \
		-e 's#</classpath>##g'`
  jvmentrieslineno=`grep -n "<jvmEntries" $file | nawk -F: '{print $1}'`
  jvmentriesline=`head -$jvmentrieslineno $file |tail -1`

  classpathRemove "$classpathline" "$JVM_CLASSPATH_CLASSES"
  classpathline=$classpathremoved

  for IS_OPTION in $IS_OPTIONS
  do
   classpathRemove "$jvmentriesline" "$IS_OPTION "
   jvmentriesline=$classpathremoved
  done

  for IS_OPTION in $IS_OPTIONS
  do
   classpathRemove "$jvmentriesline" "$IS_OPTION"
   jvmentriesline=$classpathremoved
  done

  i=1; total_lines=`wc -l $file |awk '{print $1}'`
  while [ $i -le $total_lines ];do
   if [ $i -eq $classpathlineno ];then
	echo "      <classpath>${classpathline}</classpath>" >>$tmpfile
   elif [ $i -eq $jvmentrieslineno ];then
	echo "    $jvmentriesline" >>$tmpfile
   else
	head -$i $file |tail -1 >>$tmpfile
   fi
   i=`expr $i \+ 1`
  done	# end of while
  cp -f $tmpfile $file
  rm -f $tmpfile

 done	# end of for

 # restore java.security of websphere
 file="${WAS51_JDK_HOME}/jre/lib/security/java.security"
 delete_line "$file" "login.configuration.provider=com.sun.identity.authentication.config.AMConfiguration"
}

# start websphere server
start_was51()
{
  if [ -x $WAS51_HOME/bin/startServer.sh ];then
    echo "Starting WAS 5.1..."
   if [ "$WAS51_IS_SECURE" != "false" ];then
     sslfile=/tmp/sslfile-$$
     echo "$SSL_PASSWORD" > $sslfile
     $WAS51_HOME/bin/startServer.sh $WAS51_INSTANCE < $sslfile
     rm -f $sslfile
   else
    $WAS51_HOME/bin/startServer.sh $WAS51_INSTANCE
   fi
  else
    echo "WebSphere 5.1 image ($WAS51_HOME) has problem"
    exit 0
  fi
  echo "WebSphere 5.1 started."
}

# stop websphere 
stop_was51()
{
 echo "Stopping WAS 5.1 ..."
 $WAS51_HOME/bin/stopServer.sh $WAS51_INSTANCE
}

#############################################################################
# Start of main program
#############################################################################

PWD=`pwd`
# check system env. 
check_env

# DEPLOY_LEVEL level range from 10 ~ 99 of following meaning
case $DEPLOY_LEVEL in
	4)	# sdk only with container config
                configureWAS51
		;;
	14)	# unconfig sdk only with container config
                unconfigureWAS51
		;;
	31*)	# reconfig IS SDK only with container config
                unconfigureWAS51; sleep 3
                configureWAS51
		;;
	*)	echo "Unsupported DEPLOY_LEVEL value" 
		exit 0 ;;
esac



