#!/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 $*
}


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

get_current_sync_level ()
{
   #
   # TODO ??? JDM
   # Use sync-versions to get current release
   # 

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

   if [ ! -f $versionConfFile ]
   then
      # no versions repository, so current version is JES1
      # (or AdminServer has not been configured: TBC - JDM ???)
      CURRENTSYNCLEVEL="1"
      # workaround for bugid 
      # installing from JES3 freshbits versions.conf is not there
      if [ `uname -p` = "sparc" ]
      then 
        PATCH=115610-17
      else
        PATCH=115611-17
      fi
      showrev -p | grep $PATCH 1>/dev/null 2>&1
      if [ $? -eq 0 ]; then
	 debug "$PATCH has already been installed but no versions.conf"
         echo "1	3" > $versionConfFile
         chown -h $SYSTEMUSER:$SYSTEMGROUP $versionConfFile
         CURRENTSYNCLEVEL="3"
      fi
   else
      versionList=`cat $versionConfFile`
      for curVersion in $versionList
      do
         CURRENTSYNCLEVEL=$curVersion
      done
      if [ "$CURRENTSYNCLEVEL" = "" ]
      then
         # versions repository is empty; this case should never occur...
         CURRENTSYNCLEVEL="1"
      fi
   fi

}  # get_current_sync_level



#########################################
#
# append_current_version
#
# Append CURRENT_JES_RELEASE to the versions repository
#
#########################################

append_current_version ()
{
   versionConfFile=$SERVER_ROOT/admin-serv/upgrade/versions.conf

   if [ -f $versionConfFile ]
   then
     currentRepository=`cat $versionConfFile`
     echo $currentRepository" "$CURRENT_JES_RELEASE > $versionConfFile
   fi

} # append_current_version


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

get_DS_current_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

      if [ ! -f $versionConfFile ]
      then
         # no versions repository, so current version is JES1
         # (or AdminServer has not been configured: TBC - JDM ???)
         DS_CURRENTSYNCLEVEL="1"
      else
         versionList=`cat $versionConfFile`
         for curVersion in $versionList
         do
            DS_CURRENTSYNCLEVEL=$curVersion
         done
         if [ "$CURRENTSYNCLEVEL" = "" ]
         then
            # versions repository is empty; this case should never occur...
            DS_CURRENTSYNCLEVEL="1"
         fi
      fi
   else
      DS_CURRENTSYNCLEVEL=0
   fi

   debug "DS_CURRENTSYNCLEVEL = $DS_CURRENTSYNCLEVEL"

}  # get_DS_current_sync_level




#########################################
#
# 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_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
}


#########################################
#
# 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


#########################################
#
# 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


#########################################
#
# create_help_links
#
#########################################

create_help_links()
{
   mkdir $SERVER_ROOT/manual/help
   chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual/help

   list_help_files=`(cd $USR_ADM/manual/help/; ls 2> /dev/null)`
   make_links $LOCAL_USR_ADM/manual/help $SERVER_ROOT/manual/help $list_help_files
}


#########################################
#
# create_locale_product_link
#
#########################################

create_locale_product_link()
{
   locale=$1
   product=$2
   clpl_rc=0

   if [ -d $USR_ADM/manual/$locale/$product ] && [ ! -d $SERVER_ROOT/manual/$locale ]
   then
      mkdir -p $SERVER_ROOT/manual/$locale
      chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual/$locale
      if [ $? -ne 0 ]
      then
         clpl_rc=1
      else
         make_links $LOCAL_USR_ADM/manual/$locale $SERVER_ROOT/manual/$locale $product || clpl_rc=1
      fi
   fi

   return $clpl_rc

}  # create_locale_product_link


#########################################
#
# update_links_for_JES2
#
# Create symlink between package basedir and ServerRoot.
# These symlinks are new symlinks when coming from JES1 to JES2.
#
#########################################

update_links_for_JES2()
{
   ulf_JES2_rc=0
   debug "--> update_links_for_JES2"

   # 
   # links for admin servers and shared bin
   # 
   binLinks="
      shared/bin/sync-product-cds
      shared/bin/cert-convert
      shared/bin/sync-version
      shared/bin/ldapmodify
      shared/bin/ldapsearch
      shared/bin/ldapdelete
      shared/bin/ldapcompare
      shared/bin/admin_ip.pl
      shared/bin/modutil
      bin/admin/sync-admin-cds
      bin/admin/sync-admin
      "
   for fileName in $binLinks
   do
      if [ -f $USR_ADM/$fileName ] && [ ! -h $SERVER_ROOT/$fileName ]
      then
         make_links $LOCAL_USR_ADM $SERVER_ROOT $fileName || ulf_JES2_rc=1
      fi
   done

   # 
   # updating links for NSS3.3.4 => NSS3.3.6 or later
   # 
   sslLib="
      libssl3.so
      libnss3.so
      libsmime3.so
      "
   make_links  $LOCAL_TLS_LIBDIR  $SERVER_ROOT/lib    $sslLib       || ulf_JES2_rc=1
   make_links  $LOCAL_TLS_LIBDIR  $SERVER_ROOT/alias  libnssckbi.so || ulf_JES2_rc=1

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

   #
   # updating links related to SUNWpr delivery that moved
   # to secv1 directory
   #
   prLib="
      libplc4.so
      libplds4.so
      libnspr4.so
      "
   make_links  $LOCAL_PR_LIBDIR   $SERVER_ROOT/lib  $prLib  || ulf_JES2_rc=1

   #
   # Update JSS symlinks
   #
   make_links  $LOCAL_JSS_LIBDIR         $SERVER_ROOT/lib/jss  libjss3.so  || ulf_JES2_rc=1
   make_links  $LOCAL_JSS_SHARED_LIBDIR  $SERVER_ROOT/java     jss3.jar    || ulf_JES2_rc=1

   #
   # Remove symlink for uninstaller class in /usr
   #
   UNINSTALL_CLASS=uninstall_Sun_ONE_Administration_Distribution
   if [ -h $USR_ADM/setup/${UNINSTALL_CLASS}.class ]
   then
      rm $USR_ADM/setup/${UNINSTALL_CLASS}.class || upgrade_links_status=1
   fi

   #
   # add links for on-line help's
   #
   locale_list=`(cd $USR_ADM/manual/ ; ls 2> /dev/null)`
   if [ -h $SERVER_ROOT/manual ]
   then
      # 
      # manual is a symlink
      #
      rm    -f $SERVER_ROOT/manual
      mkdir -p $SERVER_ROOT/manual
      chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual

      for cur_locale in $locale_list
      do
         if [ "$cur_locale" = "help" ] ; then
            create_help_links
         else
            mkdir -p $SERVER_ROOT/manual/$cur_locale
            chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual/$cur_locale

            product_list=`(cd $USR_ADM/manual/$cur_locale ; ls 2> /dev/null)`
            make_links \
               $LOCAL_USR_ADM/manual/$cur_locale $SERVER_ROOT/manual/$cur_locale \
               $product_list \
               || ulf_JES2_rc=1
         fi
      done

   else
      # 
      # manual not a symlink
      # 
      for cur_locale in $locale_list
      do
         if [ "$cur_locale" = "help" ] && [ ! -d $SERVER_ROOT/manual/help ]
         then
            create_help_links || ulf_JES2_rc=1
         else
            create_locale_product_link $cur_locale console || ulf_JES2_rc=1
            create_locale_product_link $cur_locale admin   || ulf_JES2_rc=1
         fi
      done
   fi

   return $ulf_JES2_rc

}  # update_links_for_JES2


#########################################
#
# update_links_for_JES3
#
# Create symlink between package basedir and ServerRoot.
#
#########################################

update_links_for_JES3()
{
   ulf_JES3_rc=0
   debug "--> update_links_for_JES3"

   # 
   # updating links for NSS 3.9.3 (or later)
   # 
   make_links  $LOCAL_TLS_LIBDIR $SERVER_ROOT/lib libsoftokn3.so || ulf_JES3_rc=1

   # 
   # updating links for JSS 4.0
   # 
   make_links  $LOCAL_JSS_LIBDIR         $SERVER_ROOT/lib/jss libjss4.so || ulf_JES3_rc=1
   make_links  $LOCAL_JSS_SHARED_LIBDIR  $SERVER_ROOT/java    jss4.jar   || ulf_JES3_rc=1

   #
   # updating links related to AdminSDk53
   #
   admsdkLib="
      libadminutil53.so
      libadmsslutil53.so
      "
   make_links  $USR_ADM/lib  $SERVER_ROOT/lib  $admsdkLib  || ulf_JES3_rc=1

   #
   # add links under <server root>/java/jars for new jarfiles admserv523*.jar
   #
   jar_list=`(cd $USR_ADM/java/jars/ ; ls admserv523*.jar  2> /dev/null)`
   make_links $LOCAL_USR_ADM/java/jars $SERVER_ROOT/java/jars $jar_list || ulf_JES3_rc=1
   
   ## ??? JDM
   ## Keep this section?

   ## #
   ## # add links for on-line help's
   ## #
   ## locale_list=`(cd $USR_ADM/manual/ ; ls 2> /dev/null)`
   ## if [ -h $SERVER_ROOT/manual ]
   ## then
   ##    # 
   ##    # manual is a symlink
   ##    #
   ##    rm    -f $SERVER_ROOT/manual
   ##    mkdir -p $SERVER_ROOT/manual
   ##    chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual
   ##
   ##    for cur_locale in $locale_list
   ##    do
   ##       if [ "$cur_locale" = "help" ] ; then
   ##          create_help_links
   ##       else
   ##          mkdir -p $SERVER_ROOT/manual/$cur_locale
   ##          chown $SYSTEMUSER:$SYSTEMGROUP $SERVER_ROOT/manual/$cur_locale
   ##
   ##          product_list=`(cd $USR_ADM/manual/$cur_locale ; ls 2> /dev/null)`
   ##          make_links \
   ##             $LOCAL_USR_ADM/manual/$cur_locale $SERVER_ROOT/manual/$cur_locale \
   ##             $product_list \
   ##             || ulf_JES3_rc=1
   ##       fi
   ##    done
   ##
   ## else
   ##    # 
   ##    # manual not a symlink
   ##    # 
   ##    for cur_locale in $locale_list
   ##    do
   ##       if [ "$cur_locale" = "help" ] && [ ! -d $SERVER_ROOT/manual/help ]
   ##       then
   ##          create_help_links || ulf_JES3_rc=1
   ##       else
   ##          create_locale_product_link $cur_locale console || ulf_JES3_rc=1
   ##          create_locale_product_link $cur_locale admin   || ulf_JES3_rc=1
   ##       fi
   ##    done
   ## fi

   return $ulf_JES3_rc

}  # update_links_for_JES3


#########################################
#
# update_links
#
# Create symlink between package basedir and ServerRoot.
#
#########################################

update_links()
{
   update_links_rc=0
   debug "--> update_links"

   case $CURRENTSYNCLEVEL in
      1) # 
         # Update symlinks in ServerRoot when coming from JES1
         # We need to update symlinks for both JES2 and JES3
         #
         update_links_for_JES2 || update_links_rc=1
         update_links_for_JES3 || update_links_rc=1
         ;;

      2) # 
         # Update symlinks in ServerRoot when coming from JES2
         # We need to update symlinks for JES3 only
         #
         update_links_for_JES3 || update_links_rc=1
         ;;

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

   return $update_links_rc

}  # update_links


#########################################
#
# update_links_for_DS
#
# This function is called when AS has been configured; so we rely on DS
# versions (JES1, JES2, ...) to know what symlinks need to be updated.
#
#########################################

update_links_for_DS()
{
   update_links_rc=0
   debug "--> update_links_for_DS"
 
   case $DS_CURRENTSYNCLEVEL in
      0) #
         # ServerRoot does not exist, nothing to do
         #
         debug "ServerRoot does not exist, no update to do"
         ;;

      1) # 
         # Update symlinks in ServerRoot when coming from JES1
         # We need to update symlinks for both JES2 and JES3
         #
         update_links_for_JES2 || update_links_rc=1
         update_links_for_JES3 || update_links_rc=1
         ;;

      2) # 
         # Update symlinks in ServerRoot when coming from JES2
         # We need to update symlinks for JES3 only
         #
         update_links_for_JES3 || update_links_rc=1
         ;;

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

   return $update_links_rc

}  # update_links_for_DS


#########################################
#
# update_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.
#
#########################################

update_start_jvm()
{
   update_jvm_rc=0

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

   return $update_jvm_rc

}  # update_start_jvm


#########################################
#
# 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=$PKGBASEDIR/usr/lib/mps/secv1
   LOCAL_TLS_LIBDIR=$LOCAL_BASEDIR/usr/lib/mps/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=$PKGBASEDIR/usr/lib/mps/secv1
   LOCAL_PR_LIBDIR=$LOCAL_BASEDIR/usr/lib/mps/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=$LOCAL_BASEDIR/usr/lib/mps/secv1
   LOCAL_JSS_SHARED_LIBDIR=$LOCAL_BASEDIR/usr/share/lib/mps/secv1

   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`
   fi

   LOCAL_SERVER_ROOT=$LOCAL_AS_BASEDIR/var/mps/serverroot
   if [ -f $ETC_ADM/shared/config/serverroot.conf ]; then
      # JDM this variable is not used
      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 $?
}

#########################################
#
# Update certmap.conf with the copy made 
# during prepatch
#
##########################################
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.keep ] && mv $SHARED_CERTMAP_CONF_LOC/certmap.conf.keep $SHARED_CERTMAP_CONF_LOC/certmap.conf || copy_certmap_status=1

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

    return $copy_certmap_status
}

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

#
# Init set of vars, including SERVER_ROOT
# ---------------------------------------
#
set_env_vars              # init bunch of vars, including SERVER_ROOT
get_current_sync_level    # init CURRENTSYNCLEVEL
get_DS_current_sync_level # init DS_CURRENTSYNCLEVEL
global_status=0
saved_certmap_status=0


# In the case where we apply a newer revision
# of the same release.

just_start=0

if [ "$CURRENTSYNCLEVEL" = "$CURRENT_JES_RELEASE" ]
then
  debug "upgrading to a newer revision of the same release"
  append_current_version
  just_start=1
fi




# certmap.conf files copies are renammed certmap.conf.
# to perform even if AS is not configured because this is
# a shared config files.
   copy_certmap_conf_files || saved_certmap_status=1
   [ $saved_certmap_status -ne 0 ] && global_status=1


# *temporarely*
# waiting for a tool that can tell if I am coming from OR1 or rtm version.

# if we are not upgrading from a newer version
# of the same release

if [ $just_start -eq 0 ]
then

#
# Update AdminServer ServerRoot
# -----------------------------
# (only if AdminServer has been configured)
#


is_configured
if [ $? -eq 0 ]
then
   #
   # init status codes
   #
   update_start_jvm_status=0  # status for update start-jvm
   update_links_status=0      # status for symlink creation/update
   sync_admin_status=0        # status for sync-admin command
   
   #
   # Elaborate User/Group information for chown command
   # -> SYSTEMUSER
   # -> SYSTEMGROUP
   #
   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

   #
   # 01- creates the link from the server to the files delivered by patch
   #
   update_links || update_links_status=1
   [ $update_links_status -ne 0 ] && global_status=1

   #
   # 02- update start-jvm script
   #
   update_start_jvm || update_start_jvm_status=1
   [ $update_start_jvm_status -ne 0 ] && global_status=1

   #
   # 03- do sync-admin upgrade -r {serverRootPath}
   #
   OPTIONS="upgrade -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

   #
   # Warning in case of failure...
   #
   if [ $global_status -ne 0 ]
   then
      echo "Postpatch script failed to upgrade configuration data:"

      debug "update_links_status = $update_links_status"
      debug "update_start_jvm_status = $update_start_jvm_status"
      debug "sync_admin_status = $sync_admin_status"
      debug "saved_certmap_status = $saved_certmap_status"

      [ $update_links_status     -ne 0 ] && echo "- update symlinks in ServerRoot failed"
      [ $update_start_jvm_status -ne 0 ] && echo "- update start-jvm failed"
      [ $sync_admin_status       -ne 0 ] && echo "- sync-admin upgrade command failed (status = $sync_admin_status)"
      [ $saved_certmap_status    -ne 0 ] && echo "- certmap.conf files have been damaged."
      echo
   else
      echo "\nPostpatch script completed upgrade 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 update_links_for_DS function to
      # update symlinks for DS.
      # (create dir <SR>/bin/admin first as it does not exist)
      #
      mkdir -p $SERVER_ROOT/bin/admin
      update_links_for_DS
   fi
fi

fi

#
# Restart AdminServer if needed
# -----------------------------
#
PATCHNUM=`env | grep PatchNum`
if [ -f  /var/tmp/.shouldRestartAS_prepatch_${PATCHNUM} ]
then
   echo "Postpatch script is starting Administration Server..."
   USER=`cat /var/tmp/.shouldRestartAS_prepatch_${PATCHNUM}`
   su ${USER} -c "${LOCAL_AS_BASEDIR}/usr/sbin/mpsadmserver start"
   remove_force /var/tmp/.shouldRestartAS_prepatch_${PATCHNUM}

   if [ -f $SERVER_ROOT/admin-serv/logs/pid  ]
   then
      echo "Postpatch script has started Administration Server."
   else
      echo "Postpatch script failed to start Administration Server."
   fi
else
   if [ "${ROOTDIR}" != "" ] && [ "${ROOTDIR}" != "/" ]
   then
      is_configured
      if [ $? -eq 0 ]; then
         echo ""
         echo "Postpatch script cannot start Administration Server when option -R is used."
         echo "If you need to start Administration Server,"
         echo "log in to the system where Administration Server is installed"
         echo "and run the following command:"
         echo "${LOCAL_AS_BASEDIR}/usr/sbin/mpsadmserver start" | sed "s,//*,/,g"
      fi
   fi
fi

# if we are not upgrading from a newer version
# of the same release

if [ $just_start -eq 0 ]
then

#
# Let the operator knows CDS need to be manually upgraded
# -------------------------------------------------------
# (only if AdminServer has been configured)
#
is_configured
if [ $? -eq 0 ]
then
   echo "Warning"
   echo "        Upgrade of remote configuration data is not complete."
   echo "        Log in to the system where this patch is "
   echo "        installed and run the following command:"
   echo "        ${LOCAL_AS_BASEDIR}/usr/sbin/mpsadmserver sync-cds [-f {absolute path to credentials file}]"| sed "s,//*,/,g"
   echo ""
fi

fi

exit $global_status

