# @(#)postbackout       1.16 01/07/25 Sun Microsystems

trap 2

#######################################################################################
#
# ENV vars which need to be modified
#

PATCHID="117074-01"
LOG=/dev/stdout # /dev/null or /dev/stdout
VERSION="SunONE Portal Server: Mobile Access 6.2 PC1"
#set -x


#######################################################################################
#
# Static ENV vars
#
ECHO=/usr/bin/echo
UMASK=/usr/bin/umask
CP=/usr/bin/cp
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq
GREP=/usr/bin/grep
LS=/usr/bin/ls
BASENAME=/usr/bin/basename
CAT=/usr/bin/cat
CHMOD=/usr/bin/chmod
CP=/usr/bin/cp
CUT=/usr/bin/cut
DATE=/usr/bin/date
DIRNAME=/usr/bin/dirname
AWK=/usr/bin/awk
ENV=/usr/bin/env
GREP=/usr/bin/grep
ID=/usr/bin/id
LN=/usr/bin/ln
LS=/usr/bin/ls
MKDIR=/usr/bin/mkdir
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
PKGINFO=/usr/bin/pkginfo
RM=/usr/bin/rm
STTY=/usr/bin/stty
TOUCH=/usr/bin/touch

/usr/bin/echo $PATH | /usr/bin/grep "/usr/java/bin[^/]" > /dev/null 2>&1
if [ $? -ne 0 ] ; then
  PATH="$PATH:/usr/java/bin"
  export PATH
fi
PATCHREV=`/usr/bin/echo ${PATCHID} | /usr/bin/awk ' BEGIN { FS="-" } {print $2 }`
PATCHBASE=`/usr/bin/echo ${PATCHID} | /usr/bin/awk ' BEGIN { FS="-" } {print $1 }`


###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

BELL_CHAR='\a'
STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
if [ ! -f $STATE_FILE ]; then
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  exit 1
fi

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PORT. $BELL_CHAR"
    exit 1
  fi
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
fi

  GrabConfig $STATE_FILE "BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     PS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
     exit 1
  fi

  GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi

DEPLOY=$PS_INSTALL_DIR/SUNWps/bin/deploy
ASADMIN="$DEPLOY_DIR/bin/asadmin"
AMADMIN="$IS_INSTALL_DIR/SUNWam/bin/amadmin"

###############################################
# Set DEPLOY_INSTANCES
###############################################
SetDeployInstanceList() {
  local INSTANCE=""
  local INSTANCES=""
  local ADMIN_INSTANCE=""
  local TMP_INSTANCES=""

   
  if [ "$DEPLOY_INSTANCE" != "" ]; then
    DEPLOY_INSTANCES="$DEPLOY_INSTANCE"
  else
    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      ADMIN_INSTANCE="admin-server"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DOMAIN/* 2>&1`
      if [ $? -eq 0 ]; then
        for INSTANCE in $TMP_INSTANCES; do
          INSTANCES="$INSTANCE $INSTANCES"
        done
      fi
    elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
        GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
        INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
        GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
        INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "IWS" ]; then
      ADMIN_INSTANCE="admserv"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DIR/https-* 2>&1`
      if [ $? -eq 0 ]; then
        for INSTANCE in $TMP_INSTANCES; do
          INSTANCES="$INSTANCE $INSTANCES"
        done
      fi
    fi

    DEPLOY_INSTANCES=""
    for INSTANCE in $INSTANCES; do
      INSTANCE=`$BASENAME $INSTANCE | $SED -e "s/https-//"`
      if [ "$INSTANCE" != "$ADMIN_INSTANCE" ]; then
        DEPLOY_INSTANCES="$INSTANCE $DEPLOY_INSTANCES"
      fi
    done
  fi
}

GetDeployAdminPassword() {
        DONE="n"
        while [ "$DONE" = "n" ]; do
      $ECHO
      $ECHO "Please enter Application Server Administration Password: "
      $STTY -echo
      read PASSWORD
      $STTY echo
      if [ "$PASSWORD" != "" ]; then
        print "Again? $OMIT_CHAR"
        $STTY -echo
        read PASSWORD_REPEAT
        $STTY echo
        print ""
        if [ "$PASSWORD" != "$PASSWORD_REPEAT" ]; then
          print "Password verification failed! $BELL_CHAR"
        else
          DONE="y"
        fi
      fi
     done
}

GetIdentityServerAdminPassword() {
        DONE="n"
        while [ "$DONE" = "n" ]; do
      $ECHO
      $ECHO "Please enter Identity Server Administration (amadmin) Password: "
      $STTY -echo
      read PASSWORD
      $STTY echo
      if [ "$PASSWORD" != "" ]; then
        print "Again? $OMIT_CHAR"
        $STTY -echo
        read PASSWORD_REPEAT
        $STTY echo
        print ""
        if [ "$PASSWORD" != "$PASSWORD_REPEAT" ]; then
          print "Password verification failed! $BELL_CHAR"
        else
          DONE="y"
        fi
      fi
     done
}



##############################################################
#
# Util functions
#

insert_line() {
  file=$1
  match=$2
  new=$3

  #$CP $file $file-orig-$$
  sed -e "
/$match/ {
i\\
$new
}" $file > $file-tmp
mv $file-tmp $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
}

restoreVersionString() {
    VERSION_FILE=$PS_INSTALL_DIR/SUNWps/lib/MAVersion.properties
    grep -v $PATCHID $VERSION_FILE > $VERSION_FILE
}



#######################################################################################
#
# Helper functions specific to handling previous patch revisions 
#

# function sort_arr sorts an array lexicographically and writes a global val NEW_ARR 
#   with the results

sort_arr() {
  set -s
  set -A NEW_ARR $*
}

# function call_rev_mods() calls rev_mods for every revision between the current 
#   revision on the system, and the revision of the patch to be installed
#   for backout, the order is reversed

call_rev_mods() {
  typeset -i patch_rev=$1
  typeset -i orig_rev=$2

  while (($orig_rev < $patch_rev))
  do
    rev_mods $patch_rev;
    ((patch_rev=$patch_rev - 1))
  done
}

######################################################################################
#
# rev_mods handles all profile updates file manipulation and anything else which may
#   be required by the patch itself.  rev_mods is the workhorse of the patch install
#   script. 
#

rev_mods() {
  typeset -i rev=$1

  if [[ $rev == 1 ]]
  then
  #########Make changes for Rev01 here###########
  echo "Backing out changes for $PATCHBASE-01..."
  
  # Rev01Mod01: backout changes  for property file
  PROP_FILE=/etc/opt/SUNWps/MAP/MAPConfig.properties
  if [ -f $PROP_FILE ]; then
	 replace_line $PROP_FILE "vocabularyDefinitions=" "vocabularyDefinitions=/vocabularies/uaprof20010430.xml /vocabularies/uaprof20000405a.xml /vocabularies/uaprof20000405b.xml /vocabularies/uaprof20010330a.xml /vocabularies/uaprof20010330b.xml /vocabularies/uaprof20020710.xml /vocabularies/ccpp20000704.xml /vocabularies/universalvocab.xml"
         if [ -f $PROP_FILE.pre$PATCHID ]; then
		/usr/bin/rm $PROP_FILE.pre$PATCHID
	 fi
  fi

  # end of Rev01Mod01
  # Rev01Mod02:  backout changes for classpath
  DEPLOY_INSTANCE=""
  SetDeployInstanceList
  if [ $DEPLOY_TYPE="IWS" ]; then

     #
     # Modify server.xml
     #
    for DEPLOY_INSTANCE in $DEPLOY_INSTANCES; do
      FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/server.xml"
      $ECHO "Updating $DEPLOY_INSTANCE Instance..."
      if [ -f $FILE ]; then
                $SED 's/\<ccpp-1_0.jar\>/ccpp.jar/g' $FILE > $FILE.tmp
                $SED 's/\<ccpp-ri-1_0.jar\>/ccpp-ri.jar/g' $FILE.tmp > $FILE
		if [ -f $FILE.pre$PATCHID ]; then
			/usr/bin/rm $FILE.pre$PATCHID
		fi
      fi
    done
   elif [ $DEPLOY_TYPE="SUNONE" ]; then

     #
     # Modify server.xml
     #
	DEPLOY_ADMIN_PASSWORD=""
	GetDeployAdminPassword
	if [ $PASSWORD != "" ]; then
        	DEPLOY_ADMIN_PASSWORD=$PASSWORD
	else
        	$ECHO "Error: Cannot determine DEPLOY_ADMIN_PASSWORD. $BELL_CHAR"
  		exit 1
	fi

    for DEPLOY_INSTANCE in $DEPLOY_INSTANCES; do
	instance=$DEPLOY_INSTANCE
        java_cp_property=$instance.java-config.classpathsuffix
	java_classpath=`$ASADMIN get -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT $java_cp_property | $CUT -d "=" -f 2 | $SED -e "s/ //g" `
	java_classpath=`$ECHO $java_classpath | $SED 's/\<ccpp-1_0.jar\>/ccpp.jar/g' `
	java_classpath=`$ECHO $java_classpath | $SED 's/\<ccpp-ri-1_0.jar\>/ccpp-ri.jar/g' `
	$ASADMIN set -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT "$java_cp_property=$java_classpath"
        $ECHO "Executing asadmin reconfig with --keepmanualchanges=true on $instance"
        $ASADMIN reconfig -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT --keepmanualchanges=true "$instance"

    done
   fi

  # end of Rev01Mod02
  # Rev01MOD03 backout chnage sin HDML template
    TEMP_DIR=/etc/opt/SUNWps/desktop/MAP/LoginProvider
   FILES="$TEMP_DIR/aml/display.template $TEMP_DIR/aml/display_AuthLDAP.template $TEMP_DIR/aml/display_AuthUnix.template $TEMP_DIR/wml/display.template $TEMP_DIR/wml/display_AuthLDAP.template $TEMP_DIR/wml/display_AuthUnix.template"
   for FILE in $FILES; do
    if [ -f $FILE ]; then
	/usr/bin/sed 's/IDT/Login.T/g' $FILE > $FILE.tmp
	/usr/bin/mv $FILE.tmp $FILE
	if [ -f $FILE.pre$PATCHID ]; then
		/usr/bin/rm $FILE.pre$PATCHID
	fi
    fi
   done
  ## End of Rev01Mod03
  ## Rev01Mod04 backout chnages for update.xml
  ##
  FILE="$PS_INSTALL_DIR/SUNWps/export/request/backout_update.request.$PATCHID"
  if [ -f $FILE ]; then
       /usr/bin/cp $FILE $PS_INSTALL_DIR/SUNWps/export/request/backout_update.xml
       XML_FILE="$PS_INSTALL_DIR/SUNWps/export/request/backout_update.xml"
       IS_ADMIN_PASSWORD=""
       GetIdentityServerAdminPassword
         if [ $PASSWORD != "" ]; then
               IS_ADMIN_PASSWORD=$PASSWORD
         else
               $ECHO "Error: Cannot determine IS_ADMIN_PASSWORD. $BELL_CHAR"
         fi

        if [ "$IS_ADMIN_PASSWORD" != "" ]; then
        	AMCONFIG_FILE=$IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties
		AMADMIN_UID=""
        	GrabConfig $AMCONFIG_FILE "com.sun.identity.authentication.super.user" "="
		if [ "$ANSWER" != "" ]; then
  			AMADMIN_UID=$ANSWER
		else
  			$ECHO "Error: Cannot get AMADMIN uid "
		fi

		if [ "$AMADMIN_UID" != "" ]; then
			$ECHO "Updating the Client Data to the original Value ..."
        		$AMADMIN -u "${AMADMIN_UID}" -w $IS_ADMIN_PASSWORD --data $XML_FILE
		else
		        $ECHO "backout to client updates are not done"
		fi
         else
		 $ECHO "backout to client updates are not done"
	fi
	/usr/bin/rm $XML_FILE
	/usr/bin/rm $FILE
   fi
  ### end of REv01Mod04
   ## Rev01Mod05 backout mailproperties chnages
	FILE=$PS_INSTALL_DIR/SUNWps/web-src/WEB-INF/classes/mail.properties
        if [ -f $FILE ]; then
		$GREP -v "textEncoding=Q" $FILE > $FILE.tmp
		/usr/bin/mv $FILE.tmp $FILE
		 $GREP -v "Encoding to be used for" $FILE > $FILE.tmp
		/usr/bin/mv $FILE.tmp $FILE
		 $GREP -v "for Japanese and Korean" $FILE > $FILE.tmp  
                /usr/bin/mv $FILE.tmp $FILE    
		 $GREP -v "Refer to RFC2047 for" $FILE > $FILE.tmp
		/usr/bin/mv $FILE.tmp $FILE
		if [ -f $FILE.pre$PATCHID ]; then
			/usr/bin/rm $FILE.pre$PATCHID
		fi
	fi
  ### end of Rev01Mod05
  ## 	
  elif [[ $rev == 2 ]]
  then
  #########Make changes for Rev02 here###########
  echo "Backing out changes for $PATCHBASE-02"
  echo "Nothing to backout ..."
  ###############################################

  fi
}

#######################################################################################
#
# Check for previous patch revisions.  If there are not any, then continue with profile
#    updates and flatfile manipulation.  For more than one patch revision, use another
#    'if' block so that the statements are not executed multiple times.  Check for the
#    highest rev first, and then only make changes from it to the current rev.
#    EX:  PATCHREV=04
#         if 03, then only apply changes for 04
#         if 02, then apply changes for 03, and 04
#         if 01, then apply changes for 02, 03, and 04
#         else, just apply changes for 04
#    
#    Note: These steps are necessary for the cummulative patch process to work correctly
#

# Function check_patch_revs checks for the existence of previous patch revisions for 
#    the current patch being installed.

check_patch_revs() {

  /usr/bin/echo "Checking for previous patch revisions..."

  # First get highest rev for the patch
  set -A CURRENT_REVS `showrev -p | /usr/bin/nawk ' { print substr($0, match($0, "Patch:")+7)} ' | \
  /usr/bin/sed 's/ Obsoletes:.*//g' | grep ${PATCHBASE} | /usr/bin/awk ' BEGIN { FS="-" } {print $2} '`

  typeset -i NUM_REVS=${#CURRENT_REVS[*]}

  # Check case where there may be no patch revisions installed
  if [[ $NUM_REVS > 0 ]]
  then 

    # Now sort it and get the highest rev currently installed
    sort_arr ${CURRENT_REVS[*]}

    HIGHEST_REV=${NEW_ARR[${NUM_REVS}-1]}
  
    call_rev_mods ${PATCHREV} ${HIGHEST_REV}
  else
    call_rev_mods ${PATCHREV} '00'
  fi

} 

#######################################################################################
#
# Main processing
#
check_patch_revs
restoreVersionString

#######################################################################################
#
# Restart the Server
#

pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  /usr/bin/echo ""
  # redeploy
  $ECHO ""
  $ECHO "Redeploying......"
  $DEPLOY redeploy > /dev/null 2>&1

fi

#######################################################################################
#
# Restart the INSTANCES
#

DEPLOY_INSTANCE=""
SetDeployInstanceList
if [ $DEPLOY_TYPE="IWS" ]; then
    $ECHO "Restarting webserver instances ..."
    /etc/init.d/amserver startall
    #for DEPLOY_INSTANCE in $DEPLOY_INSTANCES
    #  do
    #    $ECHO
    #    $ECHO "Instance --- $DEPLOY_INSTANCE"
    #    $DEPLOY_DIR/https-$DEPLOY_INSTANCE/stop
    #    $DEPLOY_DIR/https-$DEPLOY_INSTANCE/start
    #  done
elif [ $DEPLOY_TYPE="SUNONE" ]; then
     $ECHO "Restarting Appserver instances ..."
     for DEPLOY_INSTANCE in $DEPLOY_INSTANCES
      do
         $ECHO
         $ECHO "Instance --- $DEPLOY_INSTANCE"
         $DEPLOY_DOMAIN/$INSTANCE/bin/stopserv
         $DEPLOY_DOMAIN/$INSTANCE/bin/startserv
    done
fi

#
# Done
#

/usr/bin/echo ""
/usr/bin/echo "Postbackout processing complete."

trap ''
