#!/bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Role : This script
# ----     - restarts DPS instances running on the server targeted by the patch
#
# set -x

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

CURRENT_JES_RELEASE=50204

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

start_failure=no
SERVERROOT=
instance_list=
instance_name=

#=============================================================================
#
# Processes the VERBOSE2 env var to have more 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 "No package $1 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 ()
{
    get_basedir SUNWasvu || return 1

    _serverroot=/etc/mps/admin/v5.2/shared/config/serverroot.conf
    AS_BASEDIR=$PKGBASEDIR
    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
}

#=============================================================================
#
# Get list of dps instances on the target server
#
#=============================================================================

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 "You need to restart the servers after removing this patch."
        if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
        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}\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 [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    then
        # patch is applied locally
        if [ -f ${cmd_torestart_dps} ]
        then
            ${cmd_torestart_dps}
        fi

        if [ ! -f ${instance_name}/tmp/DPS.pid ]
        then
            warns_manual_restart
        fi
    else
        warns_manual_restart
    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
}

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

errors_manual_update_cds ()
{
    echo ""
    echo "*******************************************"
    echo "The CDS will not be updated by this script. "
    echo "If you have not updated it manually before removing that patch, you will have to"
    echo "reapply that patch, check that the CDS is running "
    if [ "${ROOTDIR}" = "" ] || [ "${ROOTDIR}" = "/" ]
    then
       echo "and run the following commands:"
    else
       echo "log on the machine on which this patch is to be installed and run the"
       echo "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"
}


#=============================================================================
#
# 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 or ServerRoot layout does not
# exist because AdminServer has noe been configured.
#
#=============================================================================

get_previous_sync_level ()
{
   if [ "$SERVERROOT" = "" ]
   then
      PREVIOUSSYNCLEVEL=0
   else
      #
      # retrieve content of versions.conf file of any DPS instance
      # exit if fail to get these info
      #
      inst_list=`/bin/ls -d $SERVERROOT/dps-* 2>/dev/null`
      one_inst=`echo $inst_list | cut -d' ' -f1`
      DPS_INSTANCE_ROOT=$one_inst
      VCONF_FILE=$DPS_INSTANCE_ROOT/upgrade/versions.conf
      if [ ! -f $VCONF_FILE ]
      then
         echo "File $VCONF_FILE is missing, cannot backout patch."
         exit 1
      fi
      CONF_CONTENT=`cat $VCONF_FILE`

      #
      # keep last version before the last one in versions.conf
      #
      PREVIOUSSYNCLEVEL=0
      last=0
      for curr in $CONF_CONTENT
      do
         PREVIOUSSYNCLEVEL=$last
         last=$curr
      done
   fi

   debug "PREVIOUSSYNCLEVEL = $PREVIOUSSYNCLEVEL"
   return 0

}  # get_previous_sync_level


#=============================================================================
#
# remove_last_sync_level
#
# Remove the last sync_level in the file versions.conf for each DPS
# instance. If versions.conf contains "1 2 3" then "3" is removed.
#
#=============================================================================

remove_last_sync_level ()
{
   remove_rc=0

   inst_list=`/bin/ls -d $SERVERROOT/dps-* 2>/dev/null`
   cd $SERVERROOT/shared/bin
   for instance in $inst_list
   do
      ./sync-version -downgradeNotification -i $instance
      [ $? != 0 ] && remove_rc=$?
      debug "Last sync level has been removed for instance $instance"
   done

   return $remove_rc

}  # remove_last_sync_level


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

#
# Checks
# - Init SERVERROOT variable, exit if error
# - No ServerRoot -> nothing to do
# - DPS is not configured -> nothing to do
# - Init PREVIOUSSYNCLEVEL variable
#
get_serverroot               || exit 1
[ "$SERVERROOT" != "" ]      || exit 0
[ -d ${SERVERROOT}/bin/dps ] || exit 0
get_previous_sync_level      || exit 1

#
# At this step, ServerRoot exists, DPS has been configured.
#

#
# If previous version in versions.conf is the same than current binary version
# then we have patched JESx upon JESx, so there is nothing to do in
# prebackout but remove the last version number in versions.conf
#
if [ $PREVIOUSSYNCLEVEL = $CURRENT_JES_RELEASE ]
then
   debug "--- postbackout script ---"
   debug "Patch $CURRENT_JES_RELEASE has been installed upon $CURRENT_JES_RELEASE."
   remove_last_sync_level
   remove_rc=$?
   restart_instances
   debug "-------------------------"
   exit $remove_rc
fi

#
# Regular backout out from JESx back to JESy, x > y
# -------------------------------------------------
#

#
# Fix bug 6197846: when backing out to JESx > JES1 do call
# sync-dps-cds, otherwise don't.
#
dps_patchlist=`pkgparam SUNWdps PATCHLIST`
if [ "$dps_patchlist" != "" ]
then
   get_basedir SUNWdps
   if [ $? -ne 0 ]
   then
      echo "Error: cannot get SUNWdps basedir"
      exit 1
   else
      DPS_BASEDIR=$PKGBASEDIR
   fi
   DPS_INSTALLDIR=${DPS_BASEDIR}/usr/sadm/mps/admin/v5.2
   TCL_CMD="env TMPDIR=/tmp ${DPS_INSTALLDIR}/bin/tcl8.2/tclsh"
   SYNC_DPS_CDS_SCRIPT=${DPS_INSTALLDIR}/bin/dps/install/script/sync-dps-cds.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
   if [ ! $? = 0 ]
   then
      errors_manual_update_cds
   fi
fi

#
# Now restart instances
#
restart_instances

exit 0

