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

#set -x


#########################################
#
# Debug
#
##########################################

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


#########################################
#
# getPkgBase
#
# sets PKGNAME variable 
#
##########################################

get_pkgname()
{
   PKG=$1

   pkginfo -R $ROOTDIR $PKG.\* 1>/dev/null 2>&1
   if [ $? -ne 0 ]
   then
      debug "No package $PKG installed"
      return 1
   else
      # 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}'`
   fi
   return 0
}


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

pkg_get_basedir()
{
   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 "get_basedir returns $PKGBASEDIR"
   return 0
}


#########################################
#
# 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="${SERVER_ROOT}/stop-admin\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


#########################################
#
# security_version_check 
# ----------------------
#
# Precondition to patch the system: having NSS3.3.6 or later
# installed on the system. To do the check, we are looking for
# nss3.so lib (SUNWtls package).
#
# Returns 0 if security package is ok, 1 otherwise.
#
##########################################

security_version_check ()
{
   scv_rc=1

   pkg_get_basedir SUNWtls
   if [ $? -eq 0 ]
   then
     TLS_LIBDIR=$PKGBASEDIR/usr/lib/mps/secv1  
     [ -f $TLS_LIBDIR/libnss3.so ] && scv_rc=0
   fi

   return $scv_rc
}

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

  copy_certmap_status=1

  pkg_get_basedir SUNWasvu
  if [ $? -eq 0 ]
  then
      
     SHARED_CERTMAP_CONF_LOC=$PKGBASEDIR/etc/mps/admin/v5.2/shared/config/
     SSUSER_CERTMAP_CONF_LOC=$PKGBASEDIR/etc/mps/admin/v5.2/userdb/

     copy_certmap_status=0
     [ -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
  fi
  
  return $copy_certmap_status
}

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

# 
# At least NSS3.3.6 version must be installed on the system. 
# 
security_version_check
if [ $? -ne 0 ]
then
   echo "NSS version 3.3.6 or later must be installed on the system."
   echo "Abandoning patch addition..."
   exit 1
fi

#
# Stop AdminServer before patch
#
stop_server
if [ $? -ne 0 ]
then
   exit 1
fi

#
# store certmap.conf files before patching
#
    saved_certmap_status=0
    copy_certmap_conf_files || saved_certmap_status=1
    [ $saved_certmap_status -ne 0 ] && debug "certmap.conf files may be damaged during patch addition."

exit 0

