#!/bin/ksh

trap 2

#######################################################################################
#
# ENV vars which need to be modified
#

PATCHID="119529-02"
LOG=/dev/stdout # /dev/null or /dev/stdout
VERSION="MA6.3.1CorePatch"
RELEASE="1.0-25.2"
rel_ver="25.2"
#set -x


#######################################################################################
#
# Static ENV vars
#
SED=/bin/sed
GREP=/bin/grep
UNIQ=/usr/bin/uniq
ECHO=/bin/echo
MV=/bin/mv
CP=/bin/cp
DATE=/bin/date
CHOWN=/bin/chown
CHMOD=/bin/chmod
RM=/bin/rm
JAR=/usr/jdk/entsys-j2se/bin/jar

PWD=$(pwd)
PATCHREV=`$ECHO ${PATCHID} | /usr/bin/awk ' BEGIN { FS="-" } {print $2 } '`
PATCHBASE=`$ECHO ${PATCHID} | /usr/bin/awk ' BEGIN { FS="-" } {print $1 }'`
ISMACORE="N"
BASEDIR="/opt/sun"

# Get value of a key in a flatfile and assign it to ANSWER
$ECHO " Prebackout Processing ..."
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

#######################################################################################
#
# prebackout processing
#

rpm -qa | $GREP sun-mobileaccess-1.0 > /dev/null 2>&1
if [ $? -eq 0 ]; then
  ISMACORE="Y"

  STATE_FILE="/etc/opt/sun/mobileaccess/config.properties"
  if [ ! -f $STATE_FILE ]; then
    $ECHO "Error: $STATE_FILE does not exist. "
    exit 1
  fi

  GrabConfig $STATE_FILE "fediConfigRoot" "="
  if [ "$ANSWER" != "" ]; then
    MA_BASEDIR=`echo $ANSWER|sed 's%/mobileaccess/fedi%%'`
  else
    $ECHO "Error: Cannot determine MA_BASEDIR."
    exit 1
  fi
fi # end of checking for core package



#######################################################################################
#
# Util functions
#


restoreRPMs() {
all_revs="/tmp/patchnum.tmp"
typeset -i index=1
while [ true ]
do
        echo "" > $all_revs
	ls /var/spool/backup/ | grep $PATCHBASE > /dev/null
        if [ $? != 0 ]; then
		echo ""
                echo " Patch was installed without a proper backup"
                echo " it cannot be backout"
                echo " exiting"
		echo ""
                exit 1;
        fi
        echo "You have following patches installed on system"
        for patch in `ls /var/spool/backup/ | grep $PATCHBASE`
        do
                echo " $index) " $patch
                echo " $index)$patch" >> $all_revs
                echo""
                index=`expr $index+1`
        done
                #echo "allrevs: "
                #cat $all_revs
        echo " [A]ll (Backs out all version of patch)"
        echo ""
        echo " Which version do you want to backup to [Q]uit: "
        read ans
        #echo "ans: $ans"
        if [ "$ans" = "" ] || [ "$ans" = "Q" ] || [ "$ans" = "q" ];then 
                exit 1;
        fi
	if [ "$ISMACORE" == "Y" ]; then
                BASEDIR=$MA_BASEDIR
        fi

	echo "using Prefix $BASEDIR"
        if [ "$ans" = "a" ] || [ "$ans" = "A" ]; then
                echo " Backing out ALL the patch revisions....."
                # backout all the revisions that is restore rpm from the first patch
                patchnum=`grep " 1)" $all_revs | cut -d ")" -f2`
                for eaFile in `ls /var/spool/backup/$patchnum | grep rpm`
                do
                        $ECHO $eaFile|$GREP -q 'sun-mobileaccess-1.0'&&rpmname="sun-mobileaccess"||rpmname=`$ECHO $eaRPM | cut -d - -f-3`;
                	rpm -qa $rpmname;
                	if [ $? = 0 ]; then
				echo "Backing up $eaFile .."
				## do not relocate rpm that ship config files under /etc/opt
                        	if [ $eaFile != "sun-mobileaccess-config-1.0-25.1.i386.rpm" ]; then
                                	rpm -Uvh --force --relocate /opt/sun=$BASEDIR /var/spool/backup/$patchnum/$eaFile
                        	else
                                	rpm -Uvh --force /var/spool/backup/$patchnum/$eaFile
                        	fi

			fi
                done
                break
        else
                grep " $ans)" $all_revs > /dev/null
                if [ $? = 0 ]; then
                        patchnum=`grep " $ans)" $all_revs | cut -d ")" -f2`
                        echo "Backing out to $patchnum...."
                        # backout till this patch that is restore the rpms from the patch one above this patch number
                        typeset -i next=$ans+1
                        patchnum2=`grep " $next)" $all_revs | cut -d ")" -f2`
                        for eaFile in `ls /var/spool/backup/${patchnum2}/ | grep rpm`
                        do
                                $ECHO $eaFile|$GREP -q 'sun-mobileaccess-1.0'&&rpmname="sun-mobileaccess"||rpmname=`$ECHO $eaRPM | cut -d - -f-3`;
                        	rpm -qa $rpmname;
				if [ $? = 0 ]; then
					echo "Backing up $eaFile .."
					 ## do not relocate rpm that ship config files under /etc/opt
                                	if [ $eaFile != "sun-mobileaccess-config-1.0-25.1.i386.rpm" ]; then
                                        	rpm -Uvh --force --relocate /opt/sun=$BASEDIR /var/spool/backup/$patchnum/$eaFile
                                	else
                                        	rpm -Uvh --force /var/spool/backup/$patchnum/$eaFile
                                	fi

				fi
                        done
                        break
                else
                        echo ""
                        echo "*******************************"
                        echo " Incorrect Choice , Please try again.."
        
		fi

                echo " "
        fi
done
rm $all_revs
}

######################################################################################
#
#postbackout utility
#

# function call_rev_mods() calls rev_mods for every revision between the current
#   revision on the system, and the revision of the rpm to be installed

call_rev_mods() {
  typeset -i patch_rev=$1
  typeset -i cur_rev=$2

  while (($cur_rev < $patch_rev))
  do
    rev_mods $patch_rev;
    typeset -i patch_rev=$patch_rev-1;
  done
}

######################################################################################
#
# rev_mods handles all profile updates file manipulation and anything else which may
#   be required by the patch itself.  
#

rev_mods() {
  typeset -i rev=$1
  
  if [[ $rev == 1 ]];
  then
  #########Make changes for Rev01 here###########
  echo "Backing out changes for $PATCHBASE-01..."

  elif [[ $rev == 2 ]];
  then
  #########Make changes for Rev02here###########
  echo "Backing out changes for $PATCHBASE-02..."

  elif [[ $rev == 3 ]];
  then
  #########Make changes for Rev02here###########
  echo "Backing out changes for $PATCHBASE-03..."

  fi ## end of big if loop

}


postbackout() {
	patch_rev=`echo $rel_ver | cut -d . -f2`;
	last_rev=""
	if [ "$ISAMMA" == "Y" ]; then
		last_rel=`rpm -qa sun-mobileaccess | cut -d - -f4`
		last_rev=` echo "$last_rel." |  cut -d . -f2`
	fi
	if [ "$last_rev" == "" ]; then
		last_rev=0
	fi
	call_rev_mods $patch_rev $last_rev
}



#######################################################################################
#
# Main processing
#
restoreRPMs
postbackout


trap ''
