#!/bin/sh 

# Copyright 08/02/00 Sun Microsystems, Inc. All Rights Reserved.
# @(#)postpatch	1.10 00/08/02 Sun Microsystems

AWK=/usr/bin/awk
NAWK=/usr/bin/nawk
ECHO=/usr/bin/echo
MV=/usr/bin/mv
CP=/usr/bin/cp
RM=/bin/rm
GREP=/usr/bin/grep
SED=/usr/bin/sed
HEAD=/usr/bin/head
CHMOD=/usr/bin/chmod
LS=/bin/ls
FIND=/usr/bin/find
MKD="/usr/bin/mkdir -p"
RMF="rm -rf"
CUT=/usr/bin/cut

VERSION_FILE=.version

##############################################################
#
# Test if the sdk package is installed.
#
##############################################################
check_for_sdk() {
   pkginfo -q SUNWamsdk
   if [ $? -eq 0 ]; then
      sdk="yes"
   else
      sdk="no"
   fi
}

##############################################################
#
# Test if the services package is installed.
#
##############################################################
check_for_svc() {
   pkginfo -q SUNWamsvc
   if [ $? -eq 0 ]; then
      svc="yes"
   else
      svc="no"
   fi
}

##############################################################
#
# Test if the console package is installed.
#
##############################################################
check_for_con() {
   pkginfo -q SUNWamcon
   if [ $? -eq 0 ]; then
      con="yes"
   else
      con="no"
   fi
}

##############################################################
#
# Test for web server services package is installed.
#
##############################################################
check_for_iws() {
   pkginfo -q SUNWamsws
   if [ $? -eq 0 ]; then
      iws="yes"
   else
      iws="no"
   fi
}

##############################################################
#
# Test if common domain services package is installed.
#
##############################################################
check_for_fcd() {
   pkginfo -q SUNWamfcd
   if [ $? -eq 0 ]; then
      fcd="yes"
   else
      fcd="no"
   fi
}

##############################################################
#
# Test if cross domain sso package is installed.
#
##############################################################
check_for_cds() {
   pkginfo -q SUNWamcds
   if [ $? -eq 0 ]; then
      cds="yes"
   else
      cds="no"
   fi
}

##############################################################
#
# Test if samples are installed.
#
##############################################################
check_for_sam() {
   pkginfo -q SUNWamsam
   if [ $? -eq 0 ]; then
      sam="yes"
   else
      sam="no"
   fi
}

##############################################################
#
# Test if console is deployed on app server.
#
##############################################################
check_for_sac() {
   pkginfo -q SUNWamsac
   if [ $? -eq 0 ]; then
      sac="yes"
   else
      sac="no"
   fi
}

##############################################################
#
# Test if console is deployed on web logic.
#
##############################################################
check_for_wlc() {
   pkginfo -q SUNWamwlc
   if [ $? -eq 0 ]; then
      wlc="yes"
   else
      wlc="no"
   fi
}

##############################################################
#
# Test if console is deployed on web sphere.
#
##############################################################
check_for_wsc() {
   pkginfo -q SUNWamwsc
   if [ $? -eq 0 ]; then
      wsc="yes"
   else
      wsc="no"
   fi
}


##############################################################
#
# Replaces block of code from start position to end.
#
##############################################################
replace_block() {
  FILE=$1
  START_TAG=$2
  END_TAG=$3
  NEW_TEXT_FILE=$4

  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {
           IN_REPLACE = 0;
         };
         {
           if (index($0, START_TAG) > 0) {
            while (getline str < NEW_TEXT_FILE > 0) {
              if (length(str) > 0) {
                printf("%s\n", str);
              }
            }
             IN_REPLACE = 1;
           } else if (index($0, END_TAG) > 0) {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
             IN_REPLACE = 0;
           } else {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
           }
         };
         END {};' START_TAG="$START_TAG" END_TAG="$END_TAG" NEW_TEXT_FILE="$NEW_TEXT_FILE" $FILE-tmp > $FILE
  $RM -f $FILE-tmp
}

##############################################################
#
# Inserts block of text at starting position.
#
##############################################################
insert_block() {
  FILE=$1
  TAG=$2
  NEW_TEXT_FILE=$3

  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {};
         {
           if (index($0, TAG) > 0) {
             while (getline str < NEW_TEXT_FILE > 0) {
               if (length(str) > 0) {
                 printf("%s\n", str);
               }
             }
             printf("%s\n", $0);
           } else {
             printf("%s\n", $0);
           }
         };
         END {};' TAG="$TAG" NEW_TEXT_FILE="$NEW_TEXT_FILE" $FILE-tmp > $FILE
  $RM -f $FILE-tmp
}

##############################################################
#
# moves the named file to a backup copy to be restored if
# patch is removed.
#
##############################################################
backup_file() {
    file=$1
    $CP -p $file .$file-version-61-20
}

##############################################################
#
# replaces a line in the specified file.
#
##############################################################
replace_line() {
  file=$1
  match=$2
  new=$3

  $CP $file $file-orig-$$
  $SED -e "
/$match/ {
c\\
$new
}" $file > $file-tmp
mv $file-tmp $file
rm $file-orig-$$
}


##############################################################
#
# Replace all the occurences of the string in the file.
#
##############################################################
replace_all() {
  file=$1
  match=$2
  new=$3
  cp $file $file-orig-$$
  $SED -e "s/$match/$new/g" $file > $file-tmp
  cp $file-tmp $file
  rm $file-orig-$$ $file-tmp
}

##############################################################
#
# Gets the base install dir of the product.
#
##############################################################
get_base_dir() {
  check_for_sdk
  if [ "$sdk" = "yes" ]; then
    pkg="SUNWamsdk"
  else
     check_for_fcd
     if [ "$fcd" = "yes" ]; then
        pkg="SUNWamfcd"
     else
         check_for_cds
         if [ "$cds" = "yes" ]; then
           pkg="SUNWamcds"
         else
           exit
         fi
     fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
}

##############################################################
#
# update the .version string
#
##############################################################
update_version_string() {
    cd $BASE_DIR/SUNWam/config

    if [ -f $VERSION_FILE ]; then
        $MV .version .version-61
    else
        $ECHO "Sun One Identity Server 6.1" > .version-61
    fi
     
    $ECHO "Sun One Identity Server 6.1-sp1" > .version
}

##############################################################
#
# Run an ldap search sending the results to the specified file
#
##############################################################
get_ldap_command() {
   LDAPSEARCH=$INSTALL_DIR/bin/ldapsearch
   LDAPMODIFY=$INSTALL_DIR/bin/ldapmodify
   WHP="-w $PASSWORD -h $DS_HOST -p $DS_PORT"
   if [ "$DS_SSL" = "true" ]; then
      LDAPSEARCH="$LDAPSEARCH -Z -B -P $CERTDB $WHP"
      LDAPMODIFY="$LDAPMODIFY -Z -c -P $CERTDB $WHP"
   else
      LDAPSEARCH="$LDAPSEARCH -B $WHP"
      LDAPMODIFY="$LDAPMODIFY -c $WHP"
   fi
}

ldap_search() {
   $LDAPSEARCH -D "$DIR_MANAGER" -b "ou=1.0,ou=$1,ou=services,$ROOT_SUFFIX" "(objectclass=*)" sunserviceschema > $2
}

##############################################################
#
# Run an ldap modify command.
#
##############################################################
ldap_modify() {
   `$LDAPMODIFY -D "$DIR_MANAGER" -f $1 > /dev/null 2>&1`
}

##############################################################
#
# Modify iplanet-am-auth-login-success-url of iPlanetAMAuthService
#
##############################################################
modifyAuthService() {
   BASE64="$JDK/bin/java -classpath $INSTALL_DIR/lib/am_sdk.jar com.iplanet.services.util.Base64"
   if [ -d $BASE_DIR/SUNWam/ldaplib/ldapsdk ]; then
      DIR_MANAGER=`$GREP "DIR_MANAGER=" /tmp/.patch.ans |tail -1| $SED -e "s/DIR_MANAGER=//g"`
      PASSWORD=`$GREP "PASSWORD=" /tmp/.patch.ans |tail -1| $SED -e "s/DS_PASSWORD=//g"`
      DS_HOST=`$GREP com.iplanet.am.directory.host $INSTALL_DIR/lib/AMConfig.properties | $AWK ' BEGIN { FS="=" } {print $2}`
      DS_PORT=`$GREP com.iplanet.am.directory.port $INSTALL_DIR/lib/AMConfig.properties | $AWK ' BEGIN { FS="=" } {print $2}`
      DS_SSL=`$GREP com.iplanet.am.directory.ssl.enabled $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.directory.ssl.enabled=//g"`
      CERTDB_DIR=`$GREP "^com.iplanet.am.admin.cli.certdb.dir" $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.admin.cli.certdb.dir=//g"`
      CERTDB_PREFIX=`$GREP "^com.iplanet.am.admin.cli.certdb.prefix"  $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.admin.cli.certdb.prefix=//g"`
      CERTDB=$CERTDB_DIR/"$CERTDB_PREFIX"cert8.db
      XMLFILE=/tmp/iPlanetAMAuthService.xml.$$
      get_ldap_command
      $ECHO "Modify iplanet-am-auth-login-success-url of service schema iPlanetAMAuthService to be /${is_cons_uri:-amconsole}"
      ldap_search iPlanetAMAuthService $XMLFILE
      SERVICE_BASE=`$HEAD -1 $XMLFILE`
      BEGIN=`$GREP "^sunserviceschema=" $XMLFILE`
      $SED -n -e "/$BEGIN/,/<\/ServicesConfiguration>/p" $XMLFILE | $SED -e "s/sunserviceschema=//g" > $XMLFILE.1
      $ECHO "                <AttributeSchema cosQualifier=\"default\"  i18nKey=\"a133\"  name=\"iplanet-am-auth-login-success-url\"  syntax=\"string\"  type=\"list\" >
                    <DefaultValues><Value>/${is_cons_uri:-amconsole}</Value></DefaultValues>
                </AttributeSchema>" > /tmp/.new_file
      replace_block $XMLFILE.1 iplanet-am-auth-login-success-url AttributeSchema /tmp/.new_file
      cd /etc/opt/SUNWam/config/xml
      backup_file amAuth.xml
      replace_block amAuth.xml iplanet-am-auth-login-success-url AttributeSchema /tmp/.new_file
      ENCODED=`$BASE64 $XMLFILE.1`
      if [ $? -ne 0 ]; then
        $ECHO "Failed to base64 encode service schema"
        $ECHO
        return
      fi

      $ECHO "dn: $SERVICE_BASE
changetype: modify
replace: sunserviceschema
sunserviceschema:: $ENCODED" > $XMLFILE
      ldap_modify $XMLFILE

      $ECHO "Do you want to change all sunkeyvalue of iplanet-am-auth-login-success-url to be /${is_cons_uri:-amconsole} at org/suborg level? y/n [n] \c"
      read ANS
      if [ x$ANS != "xy" ]; then
         $RM $XMLFILE $XMLFILE.1 /tmp/.new_file
         return
      fi
      
      # get auth core service settings of org/suborg
      DNS=`$LDAPSEARCH -D "$DIR_MANAGER" -b "$ROOT_SUFFIX" "(&(ou=1.0)(ou=iPlanetAMAuthService))" nsrole`
      for DN in $DNS
      do
         DN=ou=default,ou=OrganizationConfig,$DN
         $ECHO "Modify sunkeyvalue of iplanet-am-auth-login-success-url in $DN to be /${is_cons_uri:-amconsole}"
         # find and remove sunkeyvalue of iplanet-am-auth-login-success-url
         SKV=`$LDAPSEARCH -D "$DIR_MANAGER" -b "$DN" "(objectclass=*)" sunkeyvalue | $GREP iplanet-am-auth-login-success-url | $CUT -d= -f2-`
         if [ x$SKV != "x" ]; then
            $ECHO "dn: $DN
changetype: modify
delete: sunkeyvalue
sunkeyvalue: $SKV" > /tmp/.new_file
            ldap_modify /tmp/.new_file
         fi

         # add the new sunkeyvalue back
         $ECHO "dn: $DN
changetype: modify
add: sunkeyvalue
sunkeyvalue: iplanet-am-auth-login-success-url=/${is_cons_uri:-amconsole}" > /tmp/.new_file
         ldap_modify /tmp/.new_file
      done
      $RM $XMLFILE $XMLFILE.1 /tmp/.new_file
   fi
}

##############################################################
#
# main processing
#
##############################################################
get_base_dir
INSTALL_DIR=$BASE_DIR/SUNWam

LD_LIBRARY_PATH=$BASE_DIR/SUNWam/ldaplib/ldapsdk:$BASE_DIR/SUNWam/lib/jss:/usr/lib/mps/secv1:/usr/lib/mps
export LD_LIBRARY_PATH

ROOT_SUFFIX=`$GREP com.iplanet.am.rootsuffix $INSTALL_DIR/lib/AMConfig.properties | /$AWK ' BEGIN { FS="=" } { ORS="" } { print $2; for (i=3;i<=NF;i++ ) print "=" $i } `
JDK=`$GREP com.iplanet.am.jdk.path $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.jdk.path=//g"`
JAR="$JDK/bin/jar"
is_cons_uri=`grep IS_CONSOLE_URI /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`

update_version_string

TMPDIR=/tmp/IS61sp1
/usr/bin/mkdir -p $TMPDIR

check_for_sdk
if [ "$sdk" = "yes" ]; then
   cd $INSTALL_DIR/lib
   backup_file AMConfig.properties
   modifyAuthService
fi

check_for_con
check_for_sac
check_for_wlc
check_for_wsc
if [ "$con" = "yes" -o "$sac" = "yes" -o "$wlc" = "yes" -o "$wsc" = "yes" ]; then
   # add LB cookie property to AMConfig.properties
   defined=`$GREP com.iplanet.am.lbcookie.name $INSTALL_DIR/lib/AMConfig.properties | wc -l | $SED -e "s/ //g"`
   if [ $defined = "0" ]; then
      $ECHO '
# Load Balancer cookie based persistence cookie name and value.
# If no value is specified for cookieName, then this cookie will not be
# generated.
#com.iplanet.am.lbcookie.name=
#com.iplanet.am.lbcookie.value=
' >> $INSTALL_DIR/lib/AMConfig.properties
   fi

   # add UM caching properties to AMConfig.properties
   defined=`$GREP com.iplanet.am.sdk.cache.entry.expire.enabled $INSTALL_DIR/lib/AMConfig.properties | wc -l | $SED -e "s/ //g"`
   if [ $defined = "0" ]; then
      $ECHO '
# If the property 'cache.entry.expire.enabled' is set to true, the cache
# entries will expire based on the time specified in the properties below.
# The properties 'cache.user.expire.time' & 'cache.default.expire.time' specify
# time in minutes for which the user & non-user entries respectively remain
# valid after their last modification. In other words after this specified
# period of time elapses (after the last modification/read from the directory),
# the data for the entry that is cached will expire. At that instant new
# requests for data for these entries will result in reading it from the
# directory.
com.iplanet.am.sdk.cache.entry.expire.enabled=false
com.iplanet.am.sdk.cache.entry.user.expire.time=15
com.iplanet.am.sdk.cache.entry.default.expire.time=30
' >> $INSTALL_DIR/lib/AMConfig.properties
   fi

   defined=`$GREP com.sun.am.event.connection.idle.timeout $INSTALL_DIR/lib/AMConfig.properties | wc -l | $SED -e "s/ //g"`
   if [ $defined = "0" ]; then
      $ECHO '
# The key "com.sun.am.event.connection.idle.timeout" specifies timeout value
# in minutes after which the persistent searches will be restarted. Ideally,
# this value should be lower than the Load Balancer/Firewall TCP timeout, to
# make sure that the persistent searches are restarted before the connections
# are dropped. A value of "0" indicates that these searches will no be
# restarted.
com.sun.am.event.connection.idle.timeout=
' >> $INSTALL_DIR/lib/AMConfig.properties
   fi

   defined=`$GREP com.sun.am.event.connection.disable.list $INSTALL_DIR/lib/AMConfig.properties | wc -l | $SED -e "s/ //g"`
   if [ $defined = "0" ]; then
      $ECHO '
# The key "com.sun.am.event.connection.disable.list" specifies which
# event connection to be disabled. There are three valid values - aci, sm
# and um (case insensitive). Multiple values should be separated with ",".
com.sun.am.event.connection.disable.list=
' >> $INSTALL_DIR/lib/AMConfig.properties
   fi

   $RM -rf $TMPDIR
fi

check_for_sac
if [ "$sac" = "yes" ]; then
   replace_all $INSTALL_DIR/lib/AMConfig.properties "WEB_CONTAINER" "S1AS7.0."
   IAS7INSTANCEDIR=`$GREP IAS7INSTANCEDIR /var/sadm/pkg/SUNWamsac/pkginfo | $SED -e "s/IAS7INSTANCEDIR=//g"`
   cd ${IAS7INSTANCEDIR}/config
   backup_file server.xml
   backup_file server.policy
fi

check_for_wlc
if [ "$wlc" = "yes" ]; then
   replace_all $INSTALL_DIR/lib/AMConfig.properties "WEB_CONTAINER" "BEA6.1"
   WL61_BASE=`$GREP WL61_BASE /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_BASE=//g"`
   WL61_PROD_DIR=`$GREP WL61_PROD_DIR /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_PROD_DIR=//g"`
   WL61_DOMAIN=`$GREP WL61_DOMAIN /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_DOMAIN=//g"`
   cd ${WL61_BASE}/${WL61_PROD_DIR}/config/${WL61_DOMAIN}
fi

check_for_iws
if [ "$iws" = "yes" ]; then
   WS61_BASE=`grep WS61_BASE /tmp/.patch.ans | sed -e "s/WS61_BASE=//g"`
   WS61_INSTANCE=`grep WS61_INSTANCE /tmp/.patch.ans | sed -e "s/WS61_INSTANCE=//g"`
   WS61_CONFIG_BASE=$WS61_BASE/$WS61_INSTANCE/config
   cd $WS61_CONFIG_BASE
   echo "Trying to backup config in `pwd` ..."
   backup_file server.xml
   backup_file server.policy
fi

check_for_svc
if [ "$svc" = "yes" ]; then
   /usr/bin/mkdir -p $TMPDIR
   cd $TMPDIR
   $JDK/bin/jar xf $BASE_DIR/SUNWam/services.war

   AM_SERVICE_JAR_DIR=$INSTALL_DIR/web-apps/services/WEB-INF/lib
   AM_SERVICE_JS_DIR=$INSTALL_DIR/web-apps/services/js
   AM_FED_JSP_DIR=$INSTALL_DIR/web-apps/services/config/federation/default
   AM_AUTH_JSP_DIR=$INSTALL_DIR/web-apps/services/config/auth/default
   AM_AUTH_DIR=$INSTALL_DIR/web-apps/services/config/auth

   cd $AM_SERVICE_JAR_DIR
   backup_file am_auth_ui.jar
   $MV $TMPDIR/WEB-INF/lib/am_auth_ui.jar .
  
   cd ..
   backup_file auth.tld
   $MV $TMPDIR/WEB-INF/auth.tld .

fi

# start misc actions as patch content copied to disk
if [ ! -f /tmp/.patch.ans ];then	# can not do further as all pre-required info missing
 $ECHO "Please manually do as described in README file"
else
 web_con=`grep WEB_CONTAINER /tmp/.patch.ans |nawk -F= '{print $NF}'`
 if [ $web_con = "WS6" ];then	# do ws6 deploy
  $ECHO "IS/S1WS redeployment & reconfig ..."
  ws61_base=`grep WS61_BASE /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  ws61_inst=`grep WS61_INSTANCE /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  is_serv_uri=`grep IS_SERVER_URI /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  # redeploy /amserver
  PATH=$ws61_base/bin/https/bin:/usr/j2se/bin:/bin:/usr/bin
  export PATH
  LD_LIBRARY_PATH=$ws61_base/bin/https/lib:/usr/lib/mps/secv1:/usr/lib/mps:.
  export LD_LIBRARY_PATH

   for locale_dir in de es fr ja ko zh_CN zh_TW
   do
        if [ -d $AM_AUTH_DIR/default_$locale_dir ]; then
        echo "backing up $AM_AUTH_DIR/default_$locale_dir"
        $MKD $TMPDIR/default_$locale_dir
        $CP $AM_AUTH_DIR/default_$locale_dir/*.xml $TMPDIR/default_$locale_dir/
        fi
   done
   
  wdeploy delete -u /${is_serv_uri:-amserver} -i $ws61_inst -v $ws61_inst -n hard

  wdeploy deploy -u /${is_serv_uri:-amserver} -i $ws61_inst -v $ws61_inst -d $BASE_DIR/SUNWam/web-apps/services $BASE_DIR/SUNWam/services.war 

   for locale_dir in de es fr ja ko zh_CN zh_TW
   do
   	if [ -d $TMPDIR/default_$locale_dir ];then
          echo "Restoring default_$locale_dir"
          $MKD $AM_AUTH_DIR/default_$locale_dir
     	  $CP $TMPDIR/default_$locale_dir/*.xml $AM_AUTH_DIR/default_$locale_dir/
	  #$RMF $TMPDIR/default_$locale_dir
   	fi
   done

  # redeploy /amconsole
  if [ "$con" = "yes" ]; then
    $ECHO
    $ECHO "IS/S1WS redeploy console.war ..."
    $ECHO "wdeploy delete -u /${is_cons_uri:-amconsole} -i $ws61_inst -v $ws61_inst -n hard"
    wdeploy delete -u /${is_cons_uri:-amconsole} -i $ws61_inst -v $ws61_inst -n hard
    $ECHO
    $ECHO "wdeploy deploy -u /${is_cons_uri:-amconsole} -i $ws61_inst -v $ws61_inst -d $BASE_DIR/SUNWam/web-apps/applications $BASE_DIR/SUNWam/console.war"
    wdeploy deploy -u /${is_cons_uri:-amconsole} -i $ws61_inst -v $ws61_inst -d $BASE_DIR/SUNWam/web-apps/applications $BASE_DIR/SUNWam/console.war

    INDEXPAGE=$BASE_DIR/SUNWam/web-apps/applications/index.html
  fi

  # change server.policy of webcontainer
  file=$ws61_base/$ws61_inst/config/server.policy
  if [ -f $file ];then
   total=`wc -l $file |nawk '{print $1}'`
   thisline=`grep -n "ALL FILES" $file |tail -1 |nawk -F: '{print $1}'`
   mv $file $file-tmp
   rest=`expr $total \- $thisline`
   head -$thisline $file-tmp > $file
   $ECHO "    permission java.io.FilePermission \"/var/opt/SUNWam/logs/\*\", \"delete,write\";" >> $file
   tail -$rest $file-tmp >> $file
   rm -f $file-tmp
  fi
  mv $BASE_DIR/SUNWam/web-apps/services/index.html $BASE_DIR/SUNWam/web-apps/services/index.html+
  sed -e "s/SERVER_DEPLOY_URI/\/${is_serv_uri:-amserver}/g" $BASE_DIR/SUNWam/web-apps/services/index.html+ > $BASE_DIR/SUNWam/web-apps/services/index.html
  rm -f $BASE_DIR/SUNWam/web-apps/services/index.html+
 else 				# do as7 deploy
  $ECHO "IS/S1AS redeployment & reconfig ..."
  as7_uid=`grep AS7_ADMINID /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  #as7_passwd=`grep AS7_ADMINPASSWD /tmp/.patch.ans |nawk -F= '{print $NF}'`
  while [ 1 ];do
    as7_passwd=""
    $ECHO
    $ECHO "What is the password of Application Server AdminServer [$as7_passwd] \c"
    stty -echo 
    read ANS
    stty echo 
    if [ "$ANS" != "" ]; then
            as7_passwd=$ANS
	    break
    fi
  done
  as7_base=`grep AS7_BASE /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  as7_domain=`grep AS7_DOMAIN /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  as7_inst=`grep AS7_INSTANCE /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  as7_adminport=`grep AS7_ADMPORT /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  is_serv_uri=`grep IS_SERVER_URI /tmp/.patch.ans |tail -1|nawk -F= '{print $NF}'`
  $ECHO "verify $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_cons_uri:-amconsole}_1/WEB-INF/lib ..."
  if [ -d $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_cons_uri:-amconsole}_1/WEB-INF/lib ];then

   PATH=$as7_base/opt/SUNWappserver7/bin:$as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/bin:/usr/bin:/bin:.
   export PATH
   LD_LIBRARY_PATH=$as7_base/opt/SUNWappserver7/lib:$as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/lib:/usr/lib:/usr/lib/mps/secv1:/usr/lib/mps:.
   export LD_LIBRARY_PATH

   # redeploy /amconsole
   $ECHO
   $ECHO "IS/S1AS redeploy console.war ..."
   $ECHO "asadmin undeploy --user ${as7_uid:-admin} --password XXXXXXXX --port ${as7_adminport:-4848} --instance ${as7_inst:-server1} --type web ${is_cons_uri:-amconsolep}"
   asadmin undeploy --user ${as7_uid:-admin} --password ${as7_passwd:-11111111} --port ${as7_adminport:-4848} --instance ${as7_inst:-server1} --type web ${is_cons_uri:-amconsolep}
   $ECHO "asadmin deploy --user ${as7_uid:-admin} --password XXXXXXXX --port ${as7_adminport:-4848} --type web --contextroot ${is_cons_uri:-amconsole} --name ${is_cons_uri:-amconsole} --instance ${as7_inst:-server1} $BASE_DIR/SUNWam/console.war"
   asadmin deploy --user ${as7_uid:-admin} --password ${as7_passwd:-11111111} --port ${as7_adminport:-4848} --type web --contextroot ${is_cons_uri:-amconsole} --name ${is_cons_uri:-amconsole} --instance ${as7_inst:-server1} $BASE_DIR/SUNWam/console.war

   INDEXPAGE=$as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${ONSOLE_DEPLOY_URI:-amconsole}_1/index.html

   # redeploy /amserver
   $ECHO
   $ECHO "IS/S1AS redeploy services.war ..."
   $ECHO "asadmin undeploy --user ${as7_uid:-admin} --password XXXXXXXX --port ${as7_adminport:-4848} --instance ${as7_inst:-server1} --type web $is_serv_uri"

   asadmin undeploy --user ${as7_uid:-admin} --password ${as7_passwd:-11111111} --port ${as7_adminport:-4848} --instance ${as7_inst:-server1} --type web $is_serv_uri

   $ECHO "asadmin deploy --user ${as7_uid:-admin} --password XXXXXXXX --port ${as7_adminport:-4848} --type web --contextroot ${is_serv_uri:-amserver} --name ${is_serv_uri:-amserver} --instance ${as7_inst:-server1} $BASE_DIR/SUNWam/services.war "

   asadmin deploy --user ${as7_uid:-admin} --password ${as7_passwd:-11111111} --port ${as7_adminport:-4848} --type web --contextroot ${is_serv_uri:-amserver} --name ${is_serv_uri:-amserver} --instance ${as7_inst:-server1} $BASE_DIR/SUNWam/services.war 

   # change server.policy of webcontainer
   file=$as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/config/server.policy
   $ECHO "validating & modifying $file ..."
   if [ -f $file ];then
    total=`wc -l $file |nawk '{print $1}'`
    thisline=`grep -n "ALL FILES" $file |tail -1 |nawk -F: '{print $1}'`
    mv $file ${file}-tmp
    rest=`expr $total \- $thisline`
    head -$thisline ${file}-tmp > $file
    $ECHO "    permission java.io.FilePermission \"/var/opt/SUNWam/logs/\*\", \"delete,write\";" >> $file
    tail -$rest ${file}-tmp >> $file
    rm -f ${file}-tmp
   fi
   mv $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_serv_uri:-amserver}_1/index.html $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_serv_uri:-amserver}_1/index.html+
   sed -e "s/SERVER_DEPLOY_URI/\/${is_serv_uri:-amserver}/g" $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_serv_uri:-amserver}_1/index.html+ > $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_serv_uri:-amserver}_1/index.html
   rm -f $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_serv_uri:-amserver}_1/index.html+
  else
   $ECHO "can't find $as7_base/var/opt/SUNWappserver7/domains/$as7_domain/$as7_inst/applications/j2ee-modules/${is_cons_uri:-amconsole}_1/WEB-INF/lib dir .."
  fi
 fi

 if [ "$con" = "yes" -o "$sac" = "yes" ]; then
   $ECHO "Updating $INDEXPAGE"
   mv $INDEXPAGE $INDEXPAGE+
   sed -e "s/CONSOLE_DEPLOY_URI/\/${is_cons_uri:-amconsole}/g" $INDEXPAGE+ > $INDEXPAGE
   rm -f $INDEXPAGE+
 fi
fi

rm -f /tmp/.patch.ans 2>/dev/null 1>/dev/null
