#!/bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Role
# ----
# - upgrade serverroot for DPS (global section and instance specific)
# - restart DPS instances that have been stopped by prepatch
# 
# set -x

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

CURRENT_JES_RELEASE=50204

#=============================================================================
#
# Global Variables
#
#=============================================================================

warns_to_start=no
start_failure=no
SERVERROOT=
instance_list=
instance_name=

#=============================================================================
#
# To have debug traces.
#
#=============================================================================

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

#=============================================================================
#
# Get a package name on the target server
#
#=============================================================================

get_pkgname()
{
    debug "get_pkgname $1"
    PKG=$1
    pkginfo -R $ROOTDIR $PKG.\* 1>/dev/null 2>&1
    if [ $? -ne 0 ]; then
        debug "Package $1 is not installed"
        return 1
    else
        PKGNAME=`pkginfo -R ${ROOTDIR} $1.\* | awk '{print $2}'`
    fi
    debug "get_pkgname returns $PKGNAME"
    return 0
}

#=============================================================================
#
# Get a package basedir on the target server. Result includes ROOTDIR
#
#=============================================================================

get_basedir()
{
    debug "get_basedir $1"    
    get_pkgname $1 || return 1

    PKGBASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
    if [ ${ROOTDIR} != '/' ] ; then
       PKGBASEDIR=${ROOTDIR}${PKGBASEDIR}
       PKGBASEDIR=`echo ${PKGBASEDIR} | sed "s,//*,/,g"`
    fi

    debug "get_basedir returns $PKGBASEDIR"
    return 0
}

#=============================================================================
#
# Get the SERVERROOT from serverroot.conf. Result includes ROOTDIR
#
#=============================================================================

get_serverroot ()
{
    _serverroot=/etc/mps/admin/v5.2/shared/config/serverroot.conf
    if [ -f ${AS_BASEDIR}$_serverroot ] ; then
        SERVERROOT=${ROOTDIR}`cat ${AS_BASEDIR}$_serverroot`
        SERVERROOT=`echo ${SERVERROOT} | sed "s,//*,/,g"`
    fi
    debug "get_serverroot returns $SERVERROOT"
    return 0
}

#=============================================================================
#
# Init TCL command and scripts
# - TCL_CMD
# - SYNC_DPS_SCRIPT
# - SYNC_DPS_CDS_SCRIPT
#
#=============================================================================

init_tcl_cmd()
{
   TCL_CMD="env TMPDIR=/tmp ${DPS_INSTALLDIR}/bin/tcl8.2/tclsh"
   SYNC_DPS_SCRIPT=${DPS_INSTALLDIR}/bin/dps/install/script/sync-dps.tcl
   SYNC_DPS_CDS_SCRIPT=${DPS_INSTALLDIR}/bin/dps/install/script/sync-dps-cds.tcl
}

#=============================================================================
#
# Init BASEDIR for AS and DPS
# - AS_BASEDIR      eg. /
# - DPS_BASEDIR     eg. /
# - DPS_INSTALLDIR  eg. /usr/sadm/mps/admin/v5.2
#
#=============================================================================

init_basedir()
{
    init_basedir_rc=0

    get_basedir SUNWasvu
    if [ $? -eq 0 ]; then
        AS_BASEDIR=$PKGBASEDIR
    else
        init_basedir_rc=1
    fi

    get_basedir SUNWdps
    if [ $? -eq 0 ]; then
        DPS_BASEDIR=$PKGBASEDIR
    else
        init_basedir_rc=1
    fi

    DPS_INSTALLDIR=$DPS_BASEDIR/usr/sadm/mps/admin/v5.2

    return $init_basedir_rc
}

#=============================================================================
#
# Get list of dps instances on the target server
# set instance_list variable
#
#=============================================================================

get_instance_list ()
{
    instance_list=`ls -d ${SERVERROOT}/dps-* 2>/dev/null`
    debug "instance list = [$instance_list]"
}

#=============================================================================
#
# Warns to manually restart a DPS instance on the target server
#
#=============================================================================

warns_manual_restart ()
{
    if [ ${start_failure} = no ]
    then
        start_failure=yes
        echo ""
        echo "*********************************************************"
        echo "You need to restart the servers after applying this patch."
        if [ "${ROOTDIR}" = "" ] || [ "${ROOTDIR}" = "/" ]
        then
            echo "Please run the following commands:"
        else
            echo "Please log on the machine on which this patch is to be"
            echo "installed and run the following commands:"
        fi
    fi
    cmd_torestart_dps=`echo /${cmd_torestart_dps} | sed "s,^$ROOTDIR,,"` 
    cmd_torestart_dps=`echo ${cmd_torestart_dps} | sed "s,//*,/,g"`
    echo "${cmd_torestart_dps}"
}

#=============================================================================
#
# Manually update the CDS
#
#=============================================================================

warns_manual_update_cds ()
{
    echo ""
    echo "*********************************************************"
    echo "You need to update the CDS after applying this patch."
    if [ "${ROOTDIR}" = "" ] || [ "${ROOTDIR}" = "/" ]
    then
        echo "Please run the following commands:"
    else
        echo "Please log on the machine on which this patch is to be"
        echo "installed and run the following commands:"
    fi
    cmd_cds=`echo /${cmd_sync_cds} | sed "s,^$ROOTDIR,,"`
    cmd_cds=`echo ${cmd_sync_cds} | sed "s,//*,/,g"`
    echo "${cmd_cds}\n"
}

#=============================================================================
#
# restart_instance ()
#
# If ran on the target server
# Then 
#   Tries to start a DPS instance
#   Best effort, no exit
# Else
#   Warns to manually stop
#
#=============================================================================

restart_instance ()
{
    cmd_torestart_dps="${instance_name}/restart-dps"
    cmd_torestart_dps=`echo ${cmd_torestart_dps} | sed "s,//*,/,g"`

    if [ ${warns_to_start} = yes ]
    then
        warns_manual_restart
    else
        # patch is applied locally      
        if [ -f ${cmd_torestart_dps} ]
        then
            ${cmd_torestart_dps}

            if [ ! -f ${instance_name}/tmp/DPS.pid ]
            then
                warns_manual_restart
            fi
        fi
    fi
}

#=============================================================================
#
# Tries to restart DPS instances
#
#=============================================================================

restart_instances ()
{
    debug "restart DPS instances..."
    get_instance_list
    for instance_name in "${instance_list}"
    do
       if [ -f /var/tmp/.shouldRestart_DPS_`basename ${instance_name}` ]
       then
          restart_instance
          rm -f /var/tmp/.shouldRestart_DPS_`basename ${instance_name}`
       fi
    done
}

#=============================================================================
#
# 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 ()
{
   if [ "$SERVERROOT" = "" ]
   then
      CURRENTSYNCLEVEL=0
   else
      #
      # retrieve content of versions.conf file of any DPS instance
      # exit if fail to get the file content
      #
      get_instance_list
      one_inst=`echo $instance_list | cut -d' ' -f1`
      DPS_INSTANCE_ROOT=$one_inst
      VCONF_FILE=$DPS_INSTANCE_ROOT/upgrade/versions.conf
      if [ ! -f $VCONF_FILE ]
      then
         # No versions.conf file, assuming JES1 is installed
         CURRENTSYNCLEVEL=1
      fi
      CONF_CONTENT=`cat $VCONF_FILE`
      
      #
      # keep last version in versions.conf
      #
      CURRENTSYNCLEVEL=0
      for curr in $CONF_CONTENT
      do
         CURRENTSYNCLEVEL=$curr
      done
   fi

   debug "CURRENTSYNCLEVEL = $CURRENTSYNCLEVEL"
   return 0

}  # get_current_sync_level

#=============================================================================
#
# add_sync_level
#
# Add the current binary version in versions.conf for each DPS instance
#
#=============================================================================

add_sync_level ()
{
   add_rc=0

   get_instance_list
   for instance in "$instance_list"
   do
      versionConfFile=$instance/upgrade/versions.conf
      currentRepository=`cat $versionConfFile`
      echo $currentRepository"   "$CURRENT_JES_RELEASE > $versionConfFile
      debug "Add sync level [$CURRENT_JES_RELEASE] in versions.conf for instance $instance"
   done

   return $add_rc

}  # add_sync_level

#=============================================================================
#
# main
#
#=============================================================================

#
# Checks:
# - Get AS and DPS basedir
# - Init TCL cmd and scripts
# - Init SERVERROOT variable, exit if error
# - No ServerRoot -> nothing to do
# - If DPS is not configured -> nothing to do
# - Init CURRENTSYNCLEVEL variable
#

init_basedir
init_tcl_cmd
get_serverroot           || exit 1
[ "$SERVERROOT" != "" ]  || exit 0
[ -h ${SERVERROOT}/bin/dps -o -d ${SERVERROOT}/bin/dps ] || exit 0
get_current_sync_level   || exit 1

#
# If current version in versions.conf is the same than current binary version
# then we are patching JESx upon JESx, so there is nothing to do in
# prebackout but add a new sync level in versions.conf repository.
#
if [ $CURRENTSYNCLEVEL = $CURRENT_JES_RELEASE ]
then
   debug "--- postpatch script ---"
   debug "Patch $CURRENT_JES_RELEASE has been installed upon $CURRENT_JES_RELEASE."
   add_sync_level
   add_rc=$?
   restart_instances
   debug "------------------------"
   exit $add_rc
fi

#
# This is a regular upgrade from JESx to JESy, x < y
# --------------------------------------------------
#

debug "--- postpatch script ---"
debug "Starting Regular Upgrade"
debug "------------------------"

#
# Synchronize dps part of serverroot only 
# -> sync-dps.tcl
#

VERBOSE_OPTION=""
[ "$VERBOSE2" != "" ] && VERBOSE_OPTION="-verbose"

# Set LD_LIBRARY_PATH is required when patching from remote server
INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/lib:$LD_LIBRARY_PATH
INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/lib/mps/sasl2:$LD_LIBRARY_PATH
INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/lib/mps:$LD_LIBRARY_PATH
INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/lib/mps/secv1:$LD_LIBRARY_PATH
INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/lib:$LD_LIBRARY_PATH

# Upgrade the serverroot - env is required when remote and instal servers
# have different OS levels
cmd_sync_dps="${TCL_CMD} ${SYNC_DPS_SCRIPT} -syncType upgrade -cid ${DPS_INSTALLDIR} -serverroot ${SERVERROOT} ${VERBOSE_OPTION}"
cmd_sync_dps=`echo ${cmd_sync_dps} | sed "s,//*,/,g"`
debug "LD_LIBRARY_PATH = $LD_LIBRARY_PATH"
debug "cmd_sync_dps = $cmd_sync_dps"
env LD_LIBRARY_PATH=$INSTAL_LD_LIBRARY_PATH  ${cmd_sync_dps}
err=$?
if [ ! ${err} = 0 ]
then
   echo "sync-dps.tcl script failed"
   exit ${err}
fi

#
# Upgrade CDS
# -> sync-dps.tcl
#

cmd_sync_cds="${TCL_CMD} ${SYNC_DPS_CDS_SCRIPT} -cid ${DPS_INSTALLDIR} -serverroot ${SERVERROOT} ${VERBOSE_OPTION}"
cmd_sync_cds=`echo ${cmd_sync_cds} | sed "s,//*,/,g"`
debug "LD_LIBRARY_PATH = $LD_LIBRARY_PATH"
debug "cmd_sync_cds = $cmd_sync_cds"
env LD_LIBRARY_PATH=$INSTAL_LD_LIBRARY_PATH ${cmd_sync_cds} >/dev/null 2>&1
err=$?
if [ ! ${err} = 0 ]
then
    warns_manual_update_cds
    warns_to_start=yes
fi

if [ ! "${ROOTDIR}" = "" ] && [ ! "${ROOTDIR}" = "/" ]
then
    warns_to_start=yes
fi

#
# Restart instances .. if possible
#
restart_instances    

exit 0

