#!/bin/ksh


#---------------------------------------------------------------------
# See what revision of a patch would be installed for a package 
# if the highest rev was removed.
# Arguments: package patchid
# Returns second highest rev of the patch installed on the system.
# Returns 0 if there is no second highest rev of the patch installed.
#
# WARNING: Does not take into consideration obsoletion.
#
# Assumptions: pkginfo is in ${FS_ROOTDIR}/var/sadm/pkg
# Package's patches are listed in pkginfo in the PATCHLIST line
#---------------------------------------------------------------------
get_patchrev()
{
        TMP_FILE=/tmp/get_patchrev.$$
	pkginfo=${FS_ROOTDIR}/var/sadm/pkg/$1/pkginfo

	if [ ! -f $pkginfo ]; then
		# Package not installed
		return 0
	fi;

        # Get list of patches installed for this package.
        pids=`$EGREP '^PATCHLIST' $pkginfo | $NAWK -F= '{print $2}'`

        # Go through list of patches and find highest rev of this
        # patch on the system after this patch has been removed.

        $RM -f $TMP_FILE
        for i in `echo $pids`; do
           echo $i | $EGREP -s $2 || continue
           rev=`$EXPR //$i : '.*-\(.*\)'`
           echo "$rev" >>$TMP_FILE
        done

        rev_left=0       # Initialize
        if [ -f $TMP_FILE ]; then
           num_revs=`$CAT $TMP_FILE | $WC -l`
           if [ $num_revs -gt 1 ]; then
              rev_left=`$SORT $TMP_FILE | $TAIL -2 | $HEAD -1`
           fi
           $RM -f $TMP_FILE
        fi

	# Return patch revision left on system or 0 if no rev
        # is left.
	return $rev_left;
}

#---------------------------------------------------------------------
# Determine which release of SC 3.1 would remain on the system if
# the core patch were removed.
# Arguments:  None
# Returns "2" if < u3 would be left on the system.
#         "3" if u3 would be left on the system.
#         "4" if > u3 would be left on the system.
# 
# Following is the Core patch matrix being used.
#               U2/JES2      U3/JES3    U4/JES4
#           -----------------------------------
#
#  S8 SPARC    113800-11    117950-08   117950-12
#  S9 SPARC    113801-11    117949-08   117949-12
#  S9 X86      117953-01    117909-07   117909-12
#
#
# WARNING: Does not take into consideration obsoletion.
#---------------------------------------------------------------------
get_sc31_release()
{
        ARCH=`$UNAME -p`
        OS=`$UNAME -r`
	if [ $ARCH = "sparc" ]; then
           if [ $OS = "5.9" ]; then
              # Define minimum patch levels for each release
              base_patchid="117949"
              u4_rev=12
              u3_rev=08
           elif [ $OS = "5.8" ]; then
              base_patchid="117950"
              u4_rev=12
              u3_rev=08
           fi;
        elif [ $ARCH = "i386" ]; then
           base_patchid="117909"
           u4_rev=12
           u3_rev=07
	fi;

        # Check for the remaining rev of the base_patchid
        get_patchrev SUNWscr $base_patchid
        remaining_rev=$?

        # Determine which release this rev represents 
        if [ $remaining_rev -ge $u4_rev ]; then
           release=4
        elif [ $remaining_rev -ge $u3_rev ]; then
           release=3
        else 
           release=2
        fi

        return $release
}

################################################################
# Main
################################################################

CAT=/bin/cat
CP=/bin/cp
EGREP=/bin/egrep
EXPR=/bin/expr
HEAD=/bin/head
MV=/bin/mv
NAWK=/bin/nawk
RM=/bin/rm
SED=/bin/sed
SORT=/bin/sort
TAIL=/bin/tail
UNAME=/bin/uname
WC=/bin/wc

# Set rootdir for relocatability and alternate install locations.
# All file system access should be relative to this root.
if [ $ROOTDIR = "/" ]; then
   FS_ROOTDIR=""
else 
   FS_ROOTDIR="${ROOTDIR}"
fi

#
# Determine which update release of SC 3.1 would remain on the system
# if this Core patch were removed.
#
get_sc31_release
release_remains=$?

#
# Post-backout would be at SC 3.1 U3
#
if [ $release_remains = 3 ]; then
   echo "Checking if allowed to roll back to 3.1 u$release_remains".

   #check for any vlan configuration in the cluster
   ${FS_ROOTDIR}/usr/cluster/lib/sc/vlan_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #check for any netapp quorum deices configured in the cluster
   ${FS_ROOTDIR}/usr/cluster/lib/sc/netapp_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi
fi

#
# Post-backout would be at SC 3.1 something less than U3.
#
if [ $release_remains = 2 ]; then
   echo "Checking if allowed to roll back to 3.1 u2 or earlier".

   # rgm check
   ${FS_ROOTDIR}/usr/cluster/lib/sc/rgm_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #check for any vlan configuration in the cluster
   ${FS_ROOTDIR}/usr/cluster/lib/sc/vlan_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #check if the heartbeat parameters are correct before backing out the patch
   ${FS_ROOTDIR}/usr/cluster/lib/sc/tunhb_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #check if IPv6 feature is supported on CCR 
   ${FS_ROOTDIR}/usr/cluster/lib/sc/ipv6_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #check for any netapp quorum deices configured in the cluster
   ${FS_ROOTDIR}/usr/cluster/lib/sc/netapp_check_ccr ${FS_ROOTDIR}
   if [ $? -ne 0 ]; then
      exit 1
   fi

fi


#
# This script removes the entry for the rgm_vp_version table from the
# CCR directory file.  It also removes the rgm_vp_version table itself
# from the CCR directory.
#
# It performs these actions only if the SC update release that
# remains on the system after the patch is removed is less than
# U4.
#
if [ $release_remains -lt 4 ]; then
   echo "Removing rgm_vp_version table."

   CCRADM=${FS_ROOTDIR}/usr/cluster/lib/sc/ccradm
   DIRFILE=${FS_ROOTDIR}/etc/cluster/ccr/directory
   TMPFILE=${FS_ROOTDIR}/etc/cluster/ccr/directory.no_vp_version
   VPTABLE=${FS_ROOTDIR}/etc/cluster/ccr/rgm_vp_version
   
   $RM -f $TMPFILE 2>&1
   $SED -e '/rgm_vp_version/d' $DIRFILE > $TMPFILE
   $MV -f $TMPFILE $DIRFILE
   $CCRADM -i $DIRFILE -o
   $RM $VPTABLE

   # The following code was provided by developement as part of
   # RTI 6282852.

   INFRASTRUCTUREFILE=${FS_ROOTDIR}/etc/cluster/ccr/infrastructure

   $EGREP -v 'cluster\.quorum_devices\.[0-9+]\.properties\.type' $INFRASTRUCTUREFILE >/tmp/infrastructure.$$
   $CP /tmp/infrastructure.$$ $INFRASTRUCTUREFILE
   $CCRADM -i $INFRASTRUCTUREFILE -o
   $RM -f /tmp/infrastructure.$$ 2>&1

fi 
  
exit 0
