#!/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"
last_rel=""
#set -x


#######################################################################################
#
# Static ENV vars
#
SED=/bin/sed
GREP=/bin/grep
UNIQ=/usr/bin/uniq
ECHO=/bin/echo
MKDIR=/bin/mkdir
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 " PrePatch Processing ..."
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

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


#########################################################################################
#
# preinstall 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
#

replace_line() {
  file=$1
  match=$2
  new=$3

  #$CP $file $file-orig-$$
  sed -e "
/$match/ {
c\\
$new
}" $file > $file-tmp
mv $file-tmp $file
}

insert_line() {
  file=$1
  match=$2
  new=$3
                                                                                                                                                   
  #$CP $file $file-orig-$$
  sed -e "
/$match/ {
i\\
$new
}" $file > $file-tmp
mv $file-tmp $file
}



#######################################################################################
#
# Installing RPM
#

updateRPMs() {

  ## first see if the patch was ever applied before by checking if backup directory is present
  ## if the backup directory is present dont backup new files else back them up

  BACKDIR=/var/spool/backup/$PATCHID
  if [ -d $BACKDIR ]; then
        ## get the prefix to use for updating the rpms because it can be installed in
        ## non default location

        if [ "$ISMACORE" == "Y" ]; then
                BASEDIR=$MA_BASEDIR
        fi
        echo "Using Prefix $BASEDIR"

        ## upgrade rpms without backing up
        ## update rpm only if it is already present on the system

        for eaRPM in `ls $PWD | grep ".rpm"`
        do
                $ECHO $eaRPM|$GREP -q 'sun-mobileaccess-1.0'&&rpmname="sun-mobileaccess"||rpmname=`$ECHO $eaRPM | cut -d - -f-3`;
                rpm -qa $rpmname;
                if [ $? = 0 ]; then
                        $ECHO "Updating  $eaRPM ...."
			## do not relocate rpm that ship config files under /etc/opt
                        if [ $eaRPM != "sun-mobileaccess-config-1.0-25.1.i386.rpm" ]; then
                                rpm -Uvh --repackage --replacefiles --relocate /opt/sun=$BASEDIR $eaRPM
                        else
                                rpm -Uvh --repackage --replacefiles $eaRPM
                        fi

                fi
        done

  else

	## first check if macros file  is present and has repackage directory specified
        FILE="/usr/lib/rpm/macros"
        FILECHECK=n;
        if [ -f $FILE ]; then
                 FILECHECK=y;
                 $CP $FILE $FILE.pre$PATCHBASE-$PATCHREV
                 $GREP -i "repackage_dir" $FILE >> /dev/null
                 if [ $? = 0 ]; then
                        replace_line $FILE "%_repackage_dir" "%_repackage_dir         /var/spool/backup/$PATCHID"
                 else
                        $ECHO  "%_repackage_dir         /var/spool/backup/$PATCHID" >> $FILE
                 fi
		 chmod 755 $FILE
		 rm -fr /var/spool/backup/$PATCHID
		 mkdir -p /var/spool/backup/$PATCHID
        else
                $ECHO ""
                $ECHO ""
                $ECHO "   /usr/lib/rpm/macros cannot be found"
                $ECHO "   It is needed for valid backup "
                $ECHO "   Do you want to continue without backup"
                $ECHO "   If you Continue, patch cannot be backed out"
                $ECHO ""
                $ECHO "     [C]ontinue       [Q]uit"
                read ans
                if [ $ans = 'q' ] || [ $ans = 'Q' ]; then
                        $ECHO "Exitting patch install"
                        exit 1;
                fi
                if [ $ans = 'c'  ] || [ $ans = 'C' ]; then
                        $ECHO
                else
                        while [ $ans ]
                        do
                                $ECHO "Answer not understood"
                                $ECHO "  \n\t[C]ontinue\t\t[Q]uit"
                                read ans
                                if [ $ans = 'q' ] || [ $ans = 'Q' ]; then
                                        $ECHO "Exitting patch install..."
                                        exit 1;
                                fi
                                if [ $ans = 'c'  ] || [ $ans = 'C' ]; then
                                 break
                                fi
                        done
                fi
        fi
	## get the prefix to use for updating the rpms because it can be installed in 
        ## non default location
	
        if [ "$ISMACORE" == "Y" ]; then
                BASEDIR=$MA_BASEDIR
        fi
	echo "Using Prefix $BASEDIR"
        ### update rpms by keeping valid backup
        ## update rpm only if it is already present on the system
       
        for eaRPM in `ls $PWD | grep ".rpm"`
        do
                $ECHO $eaRPM|$GREP -q 'sun-mobileaccess-1.0'&&rpmname="sun-mobileaccess"||rpmname=`$ECHO $eaRPM | cut -d - -f-3`;
                rpm -qa $rpmname;
                if [ $? = 0 ]; then
                        $ECHO "Updating  $eaRPM ...."
			## do not relocate rpm that ship config files under /etc/opt
			if [ $eaRPM != "sun-mobileaccess-config-1.0-25.1.i386.rpm" ]; then
                        	rpm -Uvh --repackage --replacefiles --relocate /opt/sun=$BASEDIR $eaRPM
			else
				rpm -Uvh --repackage --replacefiles $eaRPM
			fi
                fi
        done

        ### restore the original macrosfile
        if [ "$FILECHECK" = "y" ]; then
               $CP $FILE.pre$PATCHID $FILE
	       chmod 755 $FILE
        fi
 fi

}

###############################################################################################
#
# postinstall utiltity
#


# 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
    ((cur_rev=$cur_rev + 1))
    rev_mods $cur_rev;
  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 "Making changes for $PATCHBASE-01..."

elif [[ $rev == 2 ]]
then
#########Make changes for Rev02 here###########
echo "Making changes for $PATCHBASE-02..."

elif [[ $rev == 3 ]]
then
#########Make changes for Rev03 here###########
echo "Making changes for $PATCHBASE-03..."

fi ## end of big if block

}

## get the last rev on the system before upgrading the rpms
setlastrev() {

        if [ "$ISMACORE" == "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
}


postinstall() {
	##  get the last version install
	patch_rev=`echo $rel_ver | cut -d . -f2`;
        typeset -i last_rev=$last_rev;

	call_rev_mods $patch_rev $last_rev
}

	
	
	
	


#######################################################################################
#
# Main processing
#
setlastrev
updateRPMs
postinstall

trap ''
