#!/bin/sh
#
# Copyright (c) 2004 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#

#
# Current JES release. This release is intended to be stored in versions
# repository (versions.conf).
#
CURRENT_JES_RELEASE=3


#########################################
#
# debug
#
#########################################

debug()
{
   [ "$VERBOSE" != "" ] && echo $*
}


#########################################
#
# remove_force
#
# Remove a file/symlink with -f option
# Return 0 if remove is ok, 1 otherwise.
#
# Params: list of files/symlinks
#
#########################################

remove_force()
{
   file_list=$*
   remove_force_rc=0

   for file in $file_list
   do
      if [ -h $file ]
      then
         file_type="link"
      else
         file_type="file"
      fi
      debug "remove $file_type $file"

      rm -f $file
      if [ $? -ne 0 ]
      then
         echo "Failed to remove $file_type: $file"
         remove_force_rc=1
      fi
   done

   return $remove_force_rc

}  # remove_force


#########################################
#
# get_previous_sync_level
#
# Initialize PREVIOUSSYNCLEVEL with the previous version of binary.
# The previous version of binary is the version before the last one
# in versions repository.
#
# If versions repository contains: 1 2 3
# then previous version is: 2
#
# No versions repository means OR1 binary.
#
#########################################

get_previous_sync_level ()
{
   prev_sync_level_rc=0

   #
   # TODO ??? JDM
   # Use sync-versions to get current and previous release
   # 

   versionConfFile=$SERVER_ROOT/admin-serv/upgrade/versions.conf

   PREVIOUSSYNCLEVEL=""
   if [ ! -f $versionConfFile ]
   then
      # no versions repository; this case should not occur!
      echo "Error: versions repository does not exist"
      prev_sync_level_rc=1
   else
      versionList=`cat $versionConfFile`
      for curVersion in $versionList
      do
         PREVIOUSSYNCLEVEL=${CURRENTSYNCLEVEL:="1"}
         CURRENTSYNCLEVEL=$curVersion
      done

      # if [ "$PREVIOUSSYNCLEVEL" = "$CURRENTSYNCLEVEL" ]
      # then
      #    echo "Error: previous bin version ($PREVIOUSSYNCLEVEL) and current bin version are the same"
      #    echo "       versions repository (versions.conf) is possibly corrupted"
      # fi
   fi

   return $prev_sync_level_rc

}  # get_previous_sync_level


#########################################
#
# remove_current_sync_level
#
# Remove latest level in the versions repository.
# This procedure is used when downgrading 
# to older revisions of the same release.
#
#########################################

remove_current_sync_level ()
{

   cd $SERVER_ROOT/shared/bin
   ./sync-version -downgradeNotification -i $SERVER_ROOT/admin-serv
   return $?

}  # remove_current_sync_level


#########################################
#
# get_DS_previous_sync_level
#
# Initialize DS_PREVIOUSSYNCLEVEL with the previous version of binary.
# The previous version of binary is the version before the last one
# in versions repository.
#
# If versions repository contains: 1 2 3
# then previous version is: 2
#
# No versions repository means OR1 binary or ServerRoot layout does not
# exist because AdminServer has noe been configured.
#
#########################################

get_DS_previous_sync_level ()
{
   if [ "$SERVER_ROOT" != "" ]
   then
      inst_list=`/bin/ls -d $SERVER_ROOT/slapd-* 2>/dev/null`
      one_inst=`echo $inst_list | cut -d' ' -f1`
      versionConfFile=$one_inst/upgrade/versions.conf

      DS_PREVIOUSSYNCLEVEL=0
      if [ ! -f $versionConfFile ]
      then
         # no versions repository; this case should not occur!
         echo "Error: versions repository does not exist"
      else
         versionList=`cat $versionConfFile`
         for curVersion in $versionList
         do
            DS_PREVIOUSSYNCLEVEL=${DS_CURRENTSYNCLEVEL:="1"}
            DS_CURRENTSYNCLEVEL=$curVersion
         done

         if [ "$DS_PREVIOUSSYNCLEVEL" = "$DS_CURRENTSYNCLEVEL" ]
         then
            echo "Error: previous bin version ($DS_PREVIOUSSYNCLEVEL) and current bin version are the same"
            echo "       versions repository (versions.conf) is possibly corrupted"
         fi
      fi
   else
      DS_PREVIOUSSYNCLEVEL=0
   fi

   debug "DS_PREVIOUSSYNCLEVEL = $DS_PREVIOUSSYNCLEVEL"

}  # get_DS_previous_sync_level


#########################################
#
# make_link
#
# Remove exiting link at dest if necessary, create the link,
# set the owner id ans group of the link that just have been created.
#
# Use global variables:
# - SYSTEMUSER
# - SYSTEMGROUP
#
# $1 = source
# $2 = dest
#
#########################################

make_link ()
{
   make_link_rc=0
   src=$1
   dest=$2

   # if the dest link already exists: remove it
   remove_force $dest || make_link_rc=1

   # create the link
   debug "create link $dest"
   ln -s $src $dest

   if [ $? -ne 0 ];then
      make_link_rc=1
      echo "Creation of $dest link failed"
   else
      if [ "$SYSTEMUSER" != "" ] && [ "$SYSTEMGROUP" != "" ]; then
         # set the owner id and group of the link.
         chown -h $SYSTEMUSER:$SYSTEMGROUP $dest
         if [ $? -ne 0 ];then
            make_link_rc=1
            echo "chown of $dest link owner failed"
         fi
      fi
   fi

   return $make_link_rc

}  # make_link


#########################################
#
# make_links
#
# Create symlinks for a set of files.
#
# $1 = dir_source
# $2 = dir_dest
# $* = list of files
#
#########################################

make_links ()
{
   make_links_rc=0
   dir_src=$1;  shift 1
   dir_dest=$1; shift 1
   files=$*

   for file in $files
   do
      make_link $dir_src/$file $dir_dest/$file || make_links_rc=1
   done

   return $make_links_rc

}  # make_links


#########################################
#
# restore_links_for_JES1
#
# Remove all symlinks that have been created by JES2 upgrade
# and restore previous symlinks.
#
# Return 0 if all is ok, 1 otherwise
#
#########################################

restore_links_for_JES1()
{
   #
   # Returned status code
   #
   rlf_JES1_rc=0

   #
   # Binary to remove
   #
   binLinksToRemove="
      shared/bin/sync-product-cds
      shared/bin/cert-convert
      shared/bin/sync-version
      bin/admin/sync-admin-cds
      bin/admin/sync-admin
      "

   # If the link on ns-slapd does not exist, we assume that DS is not
   # configured anymore. So, we can remove the links on ldap tools.
   ns_slapd_location="$SERVER_ROOT/bin/slapd/server/ns-slapd"
   if [ ! -h $ns_slapd_location ]
   then
      binLinksToRemove="$binLinksToRemove
         shared/bin/ldapmodify
         shared/bin/ldapsearch
         shared/bin/ldapdelete
         shared/bin/ldapcompare
         shared/bin/admin_ip.pl
         shared/bin/modutil
         "
   fi

   for fileName in $binLinksToRemove
   do
      remove_force $SERVER_ROOT/$fileName || rlf_JES1_rc=1
   done

   #
   # Restore symlinks SUNWpr
   # 
   prLib="
      libplc4.so
      libplds4.so
      libnspr4.so
      "
   make_links $LOCAL_TLS_LIBDIR_334 $SERVER_ROOT/lib $prLib || rlf_JES1_rc=1

   # 
   # Restore symlinks to NSS3.3.4
   #
   sslLib="
      libssl3.so
      libnss3.so
      libsmime3.so
      "
   make_links $LOCAL_TLS_LIBDIR_334 $SERVER_ROOT/lib $sslLib || rlf_JES1_rc=1

   if [ `uname -p` = "sparc" ]
   then
      sslLibSparc="
         libfreebl_hybrid_3.so
         libfreebl_pure32_3.so
         "
      make_links $LOCAL_TLS_LIBDIR_334 $SERVER_ROOT/lib $sslLibSparc || rlf_JES1_rc=1
   fi

   make_links $LOCAL_TLS_LIBDIR_334        $SERVER_ROOT/alias   libnssckbi.so || rlf_JES1_rc=1
   make_links $LOCAL_JSS_LIBDIR_334        $SERVER_ROOT/lib/jss libjss3.so    || rlf_JES1_rc=1
   make_links $LOCAL_JSS_SHARED_LIBDIR_334 $SERVER_ROOT/java    jss3.jar      || rlf_JES1_rc=1

   #
   # Restore uninstaller class in /usr
   #
   UNINSTALL_CLASS=uninstall_Sun_ONE_Administration_Distribution
   if [ ! -h $USR_ADM/setup/${UNINSTALL_CLASS}.class ] \
   && [ -f $SERVER_ROOT/setup/${UNINSTALL_CLASS}.class ]
   then
      make_links $LOCAL_SERVER_ROOT/setup $USR_ADM/setup $UNINSTALL_CLASS.class \
         || downgrade_links_status=1
   fi

   #
   # Restore previous help
   #
   if [ ! -h $SERVER_ROOT/manual ]
   then
      rm -rf $SERVER_ROOT/manual || rlf_JES1_rc=1
      make_links $LOCAL_USR_ADM $SERVER_ROOT manual || rlf_JES1_rc=1
   fi

   return $rlf_JES1_rc

}  # restore_links_for_JES1


#########################################
#
# restore_links_for_JES2
#
# Remove all symlinks created by JES3 upgrade and restore previous symlinks.
#
# Return 0 if all is ok, 1 otherwise
#
#########################################

restore_links_for_JES2()
{
   #
   # Returned status code
   #
   rlf_JES2_rc=0

   #
   # Remove symlinks for admserv523*.jar
   # 
   jar_list=`ls $SERVER_ROOT/java/jars/admserv523*.jar 2> /dev/null`
   [ "$jar_list" != "" ] && remove_force $jar_list

   icon_list=`ls $SERVER_ROOT/java/jars/admserv523*.icon 2> /dev/null`
   [ "$icon_list" != "" ] && remove_force $icon_list

   # 
   # Remove symlink specific to NSS 3.9.3
   #
   remove_force $SERVER_ROOT/lib/libsoftokn3.so

   # 
   # Remove symlink specific to JSS 4.0
   #
   remove_force $SERVER_ROOT/lib/jss/libjss4.so
   remove_force $SERVER_ROOT/java/jss4.jar

   # 
   # Remove AdminSDK53 symlinks
   # 
   admsdkLib="
      libadminutil53.so
      libadmsslutil53.so
      "
   for fileName in $admsdkLib
   do
      remove_force $SERVER_ROOT/lib/$fileName || rlf_JES2_rc=1
   done

   ## ??? JDM
   ## What about help?
   ##
   ## #
   ## # Restore previous help
   ## #
   ## if [ ! -h $SERVER_ROOT/manual ]
   ## then
   ##    rm -rf $SERVER_ROOT/manual || rlf_JES2_rc=1
   ##    make_links $LOCAL_USR_ADM $SERVER_ROOT manual || rlf_JES2_rc=1
   ## fi

   return $rlf_JES2_rc

}  # restore_links_for_JES2


#########################################
#
# restore_links
#
# Remove all symlinks created by upgrade and restore previous symlinks
# in place before upgrade.
#
# Return 0 if all is ok, 1 otherwise
#
#########################################

restore_links()
{
   #
   # Returned status code
   #
   restore_links_rc=0

   case $PREVIOUSSYNCLEVEL in
      1) # 
         # Restore symlinks in ServerRoot when coming back to OR1
         # We need to undo symlinks created by both JES2 and JES3 upgrade
         #
         restore_links_for_JES2 || restore_links_rc=1
         restore_links_for_JES1 || restore_links_rc=1
         ;;

      2) # 
         # Restore symlinks in ServerRoot when coming back to OR2
         # We need to undo symlinks created by both JES3 upgrade only
         #
         restore_links_for_JES2 || restore_links_rc=1
         ;;

      *) #
         # This case should not occur
         #
         echo
         echo "Versions repository reports previous version = [$PREVIOUSSYNCLEVEL]"
         echo "This cannot be possible, versions repository is probably corrupted."
         echo
         restore_links_rc=1
         ;;
   esac

   return $restore_links_rc

}  # restore_links


#########################################
#
# restore_links_for_DS
#
# Remove all symlinks created by upgrade and restore previous symlinks
# in place before upgrade.
#
# This function is called when AS has been configured; so we rely on DS
# versions (JES1, JES2, ...) to know what symlinks need to be removed.
#
# Return 0 if all is ok, 1 otherwise
#
#########################################

restore_links_for_DS()
{
   #
   # Returned status code
   #
   restore_links_rc=0

   case $DS_PREVIOUSSYNCLEVEL in
      0) #
         # ServerRoot does not exist, nothing to do
         #
         debug "ServerRoot does not exist, no update to do"
         ;;

      1) # 
         # Restore symlinks in ServerRoot when coming back to OR1
         # We need to undo symlinks created by both JES2 and JES3 upgrade
         #
         restore_links_for_JES2 || restore_links_rc=1
         restore_links_for_JES1 || restore_links_rc=1
         ;;

      2) # 
         # Restore symlinks in ServerRoot when coming back to OR2
         # We need to undo symlinks created by both JES3 upgrade only
         #
         restore_links_for_JES2 || restore_links_rc=1
         ;;

      *) #
         # This case should not occur
         #
         echo
         echo "Versions repository reports previous version = [$PREVIOUSSYNCLEVEL]"
         echo "This cannot be possible, versions repository is probably corrupted."
         echo
         restore_links_rc=1
         ;;
   esac

   return $restore_links_rc

}  # restore_links_for_DS


#########################################
#
# restore_start_jvm
#
# Update start-jvm script: change JRE library path
# use ${NSES_JRE}/lib/sparc before ${NSES_JRE}/lib/sparc/server
#
# Do not change this function for upgrade subsequent to JES3.
#
#########################################

restore_start_jvm()
{
   restore_jvm_rc=0

   # only restore when coming from JES3 to JES1/JES2
   if [ $CURRENT_JES_RELEASE -eq 3 ] \
   && [ $PREVIOUSSYNCLEVEL -eq 1 -o $PREVIOUSSYNCLEVEL -eq 2 ]
   then
      # 
      # restore start-jvm script
      # in NSES_JRE_RUNTIME_LIBPATH variable do swap
      # ${NSES_JRE}/lib/sparc and ${NSES_JRE}/lib/sparc/server
      #
      debug "restore start-jvm script"
      START_JVM_FILE=$SERVER_ROOT/bin/https/bin/start-jvm
      sed -e \
         's/${NSES_JRE}\/lib\/sparc:${NSES_JRE}\/lib\/sparc\/server:/${NSES_JRE}\/lib\/sparc\/server:${NSES_JRE}\/lib\/sparc:/' \
         $START_JVM_FILE > $START_JVM_FILE.tmp
      mv -f $START_JVM_FILE.tmp $START_JVM_FILE
   fi

   return $restore_jvm_rc

}  # restore_start_jvm


#########################################
#
# pkg_get_pkgname
#
# sets PKGNAME variable
#
##########################################

pkg_get_pkgname()
{
   PKG=$1

   pkginfo -R $ROOTDIR ${PKG}.\* 1>/dev/null 2>&1
   if [ $? -ne 0 ]
   then
      debug "No package $PKG installed"
      return 1
   fi
   # Fix bug 6189447: we can have more than one package
   # just keep the first one
   _all_pkgs=`pkginfo -R ${ROOTDIR} $PKG.\* | awk '{print $2}'`
   PKGNAME=`echo $_all_pkgs | awk '{print $1}'`
   return 0

}  # pkg_get_pkgname


#########################################
#
# pkg_get_basedir
#
# set PKGBASEDIR variable
#
##########################################

pkg_get_basedir()
{
   pkg_get_pkgname $1 || return 1

   LOCAL_BASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
   if [ ${ROOTDIR} != '/' ]
   then
      PKGBASEDIR=${ROOTDIR}/${LOCAL_BASEDIR}
   else
      PKGBASEDIR=${LOCAL_BASEDIR}
   fi
   #debug "pkg_get_basedir for $1 returns $PKGBASEDIR"
   return 0

}  # pkg_get_basedir


#########################################
#
# set_env_vars
#
#########################################

set_env_vars ()
{
   pkg_get_basedir SUNWasvu
   AS_BASEDIR=$PKGBASEDIR
   LOCAL_AS_BASEDIR=$LOCAL_BASEDIR
   USR_ADM=$AS_BASEDIR/usr/sadm/mps/admin/v5.2
   ETC_ADM=$AS_BASEDIR/etc/mps/admin/v5.2

   LOCAL_USR_ADM=$LOCAL_AS_BASEDIR/usr/sadm/mps/admin/v5.2
   LOCAL_ETC_ADM=$LOCAL_AS_BASEDIR/etc/mps/admin/v5.2

   pkg_get_basedir SUNWtls
   TLS_LIBDIR_334=$PKGBASEDIR/usr/lib/mps
   LOCAL_TLS_LIBDIR_334=$LOCAL_BASEDIR/usr/lib/mps
   TLS_LIBDIR=$TLS_LIBDIR_334/secv1
   LOCAL_TLS_LIBDIR=$LOCAL_TLS_LIBDIR_334/secv1

   pkg_get_basedir SUNWldk
   LDK_LIBDIR=$PKGBASEDIR/usr/lib/mps
   LOCAL_LDK_LIBDIR=$LOCAL_BASEDIR/usr/lib/mps

   pkg_get_basedir SUNWpr
   PR_LIBDIR_334=$PKGBASEDIR/usr/lib/mps
   LOCAL_PR_LIBDIR_334=$LOCAL_BASEDIR/usr/lib/mps
   PR_LIBDIR=$PR_LIBDIR_334/secv1
   LOCAL_PR_LIBDIR=$LOCAL_PR_LIBDIR_334/secv1

   pkg_get_basedir SUNWsasl
   SASL_LIBDIR=$PKGBASEDIR/usr/lib/mps/sasl2
   LOCAL_SASL_LIBDIR=$LOCAL_BASEDIR/usr/lib/mps/sasl2

   pkg_get_basedir SUNWicu
   ICU_LIBDIR=$PKGBASEDIR/lib/
   LOCAL_ICU_LIBDIR=$LOCAL_BASEDIR/lib/

   pkg_get_basedir SUNWjss
   LOCAL_JSS_LIBDIR_334=$LOCAL_BASEDIR/usr/lib/mps
   LOCAL_JSS_SHARED_LIBDIR_334=$LOCAL_BASEDIR/usr/share/lib/mps

   SERVER_ROOT=$AS_BASEDIR/var/mps/serverroot
   if [ -f $ETC_ADM/shared/config/serverroot.conf ]
   then
      SERVER_ROOT=$ROOTDIR/`cat $ETC_ADM/shared/config/serverroot.conf`
      LOCAL_SERVER_ROOT=`cat $ETC_ADM/shared/config/serverroot.conf`
   fi

}  # set_env_vars


#########################################
#
# is_configured
#
# return 0 if configuration has been done
#
##########################################
is_configured ()
{
   [ -f $SERVER_ROOT/admin-serv/config/adm.conf ]
   return $?
}


#########################################
#
# stops Admin server instances 
#
##########################################

stop_server()
{
   stop_patch=no
   cmd_torun_admin=

   AS_BASEDIR=${ROOTDIR}/`pkginfo -R ${ROOTDIR} -r SUNWasvu`

   # BugId 4943278:
   # Error 'cat: cannot open /[...]/serverroot.conf' when patchadd 115614-01
   # add existence file test
   if [ -f ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf ]
   then
      SERVER_ROOT=${ROOTDIR}/`cat ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf`
   fi

   if [ -f $SERVER_ROOT/admin-serv/logs/pid  ]
   then
      # server is running, try to stop it
      stop_patch=yes
      TMP_BASE_DIR=`pkginfo -R ${ROOTDIR} -r SUNWasvu`
      cmd_torun_admin="${TMP_BASE_DIR}/usr/sbin/mpsadmserver stop\n"
   else
      # server is not running, return ok
      return 0
   fi

   if [ "$ROOTDIR" = "" ] || [ "$ROOTDIR" = "/" ]
   then
      echo "Prepatch script is stopping Administration Server..."
      USER=`/usr/bin/ls -l $SERVER_ROOT/admin-serv/logs/pid | awk '{print $3}'`
      echo $cmd_torun_admin | sh
      if [ $? -ne 0 ]
      then
         echo "Prepatch script failed to stop Administration Server." 
         return 1
      fi
      PATCHNUM=`env | grep PatchNum`
      echo $USER > /var/tmp/.shouldRestartAS_prepatch_$PATCHNUM
      echo "Administration Server stopped."
      return 0
   else
      echo "Prepatch script cannot stop Administration Server when option -R is used."
      echo "Log in to the system where Administration Server is running"
      echo "and run the following command:"
      echo "$cmd_torun_admin" | sed "s,//*,/,g"
      return 1  
   fi

}  # stop_server

#########################################
#
# Makes a copy of certmap.conf files
#
##########################################
copy_certmap_conf_files () {

   copy_certmap_status=0
   SHARED_CERTMAP_CONF_LOC=$AS_BASEDIR/etc/mps/admin/v5.2/shared/config/
   SSUSER_CERTMAP_CONF_LOC=$AS_BASEDIR/etc/mps/admin/v5.2/userdb/
 
   [ -f $SHARED_CERTMAP_CONF_LOC/certmap.conf ] && mv $SHARED_CERTMAP_CONF_LOC/certmap.conf $SHARED_CERTMAP_CONF_LOC/certmap.conf.keep || copy_certmap_status=1

    [ -f $SSUSER_CERTMAP_CONF_LOC/certmap.conf ] && mv $SSUSER_CERTMAP_CONF_LOC/certmap.conf $SSUSER_CERTMAP_CONF_LOC/certmap.conf.keep || copy_certmap_status=1

    return $copy_certmap_status
}

#########################################
#
# main
#
##########################################

#
# Init set of vars, including SERVER_ROOT
# ---------------------------------------
#
set_env_vars
get_DS_previous_sync_level # init DS_PREVIOUSSYNCLEVEL
global_status=0

#
# Stop AdminServer before backout
# -------------------------------
#
stop_server || exit 1

#
# make a copy ofcertmap.conf files
#
# certmap.conf files may have be updated by te user so,
# downgrade files must not overwrite them. so we make a copy.

saved_certmap_status=0       # status for saving certmap.conf files
copy_certmap_conf_files || saved_certmap_status=1 
[ $saved_certmap_status -ne 0 ] && debug "certmap.conf files may be damaged during patch removal."

#
# Update AdminServer ServerRoot
# -----------------------------
# (only if AdminServer has been configured)
#
is_configured
if [ $? -eq 0 ]
then
   #
   # init status codes
   #
   restore_links_status=0       # status for symlink restore
   sync_admin_status=0          # status for sync-admin command
   restore_start_jvm_status=0   # status for sync-admin command

   #
   # Elaborate User/Group information for chown command
   #
   ssusersConfFile=$SERVER_ROOT/shared/config/ssusers.conf
   if [ -f $ssusersConfFile ]
   then
      SYSTEMUSER=` cat $ssusersConfFile | awk '/^SuiteSpotUser/  { print $2; }' `
      SYSTEMGROUP=`cat $ssusersConfFile | awk '/^SuiteSpotGroup/ { print $2; }' `
   fi

   #
   # Caution: get_previous_sync_level needs to access versions
   # repository, so do it before sync-admin otherwise versions
   # repository can be deleted by sync-admin.
   # Fix 6198289: if versions.conf does not exist, just exit with status NOK
   #
   get_previous_sync_level || exit 1

   # If the previous version is the same than current release
   # then we are downgrading to a lower revision of the same release
   # we don't do anything but remove the latest version
   if [ "$CURRENT_JES_RELEASE" = "$PREVIOUSSYNCLEVEL" ]
   then
     rc=remove_current_sync_level
     exit $?
   fi

   #
   # 01- do sync-admin downgrade -r {serverRootPath}
   #
   OPTIONS="downgrade -r $SERVER_ROOT"
   REMOTE_LD_LIBRARY_PATH=$TLS_LIBDIR:$LDK_LIBDIR:$PR_LIBDIR:$SASL_LIBDIR:$ICU_LIBDIR:$USR_ADM/lib
   REMOTE_LD_LIBRARY_PATH=$REMOTE_LD_LIBRARY_PATH:$LD_LIBRARY_PATH
   env LD_LIBRARY_PATH=$REMOTE_LD_LIBRARY_PATH $USR_ADM/bin/admin/sync-admin $OPTIONS
   sync_admin_status=$?
   [ $sync_admin_status -ne 0 ] && global_status=1

   #
   # 02- restore start-jvm script
   #
   restore_start_jvm || restore_start_jvm_status=1
   [ $restore_start_jvm_status -ne 0 ] && global_status=1

   #
   # 03- restore the symlinks in ServerRoot
   #
   restore_links || restore_links_status=1
   [ $restore_links_status -ne 0 ] && global_status=1

   #
   # Some warning around certificate database
   #
   echo "Reverting to prepatch certificate database."
   echo "Certificates added after patching the server must be downgraded manually."
   echo "See 'Installation and Migration Guide' or patch README for instructions."

   #
   # Warning in case of failure...
   #
   if [ $global_status -ne 0 ]
   then
      echo "\nPrebackout script failed to downgrade configuration data:"
      [ $sync_admin_status        -ne 0 ] && echo "- sync-admin downgrade command failed (status = $sync_admin_status)"
      [ $restore_start_jvm_status -ne 0 ] && echo "- restoring start-jvm script failed"
      [ $restore_links_status     -ne 0 ] && echo "- restoring symlinks in ServerRoot failed"
      echo
   else
      echo "\nPrebackout script completed downgrade of configuration data.\n"
   fi
else
   #
   # AS is not configured
   # --------------------
   # Fix bug 6196533: when AS is not configured but ServerRoot exists,
   # this means ServerRoot was created by DS. So we have to update
   # symlinks in ServerRoot for DS.
   #
   if [ -f $ETC_ADM/shared/config/serverroot.conf ]
   then
      #
      # AS is not configured but serverroot.conf exists, this means DS
      # has been configured. So we call restore_links_for_DS function to
      # restore symlinks for DS.
      # (create dir <SR>/bin/admin first as it does not exist)
      #
      mkdir -p $SERVER_ROOT/bin/admin
      restore_links_for_DS
   fi
fi

exit $global_status

