#!/bin/sh

UNAME=/bin/uname
OSTYPE=`$UNAME -s`
DIRNAME=/usr/bin/dirname

if [ "$OSTYPE" != "Linux" ]; then
  exit
fi

DATE=/bin/date
ECHO=/bin/echo
MKDIR=/bin/mkdir
RPM=/bin/rpm

PS_IMAGE_DIR=`$DIRNAME $0`

PS_PKG_LIST="sun-portal-core sun-portal-core-config sun-webnfs sun-portal-sample sun-portal-sample-config sun-portal-instantmessaging sun-portal-instantmessaging-config sun-portal-netmail sun-portal-onlinehelp sun-portal-onlinehelp-identity sun-portal-ssoadapter sun-portal-addressbookapi sun-portal-addressbookapi-config sun-portal-portlet sun-portal-portlet-config sun-portal-ssoadapteradmin sun-portal-container sun-portal-kssl sun-portal-subscriptions sun-portal-subscriptions-config sun-portal-jsptaglib sun-portal-jsptaglib-config sun-portal-calendarapi sun-portal-calendarapi-config sun-portal-mobileaccess sun-portal-mobileaccess-config sun-portal-mobileaccess-doc sun-portal-mobileaccess-identity sun-portal-portletsample sun-portal-portletsample-config sun-portal-wsrpcommon sun-portal-discussions sun-portal-discussions-config sun-portal-rewriter sun-portal-wsrpconsumer sun-portal-desktop sun-portal-desktop-config sun-portal-rewriteradmin sun-portal-wsrpconsumerconfig sun-portal-desktopadmin sun-portal-wsrpconsumersample sun-portal-desktopserviceconfig sun-portal-desktopserviceconfig-config sun-portal-sdk sun-portal-wsrpproducer sun-portal-desktopdatamgmt sun-portal-mail sun-portal-mail-config sun-portal-searchserver sun-portal-wsrpproducersample sun-portal-desktoppapi sun-portal-desktoppapi-config sun-portal-searchadmin sun-portal-desktopextension sun-portal-desktopextension-config sun-portal-searchui sun-portal-searchui-config"
SRA_PKG_LIST="sun-portal-gatewayidentityagent sun-portal-gatewayidentityagent-identity sun-portal-gatewayadmin sun-portal-netlet sun-portal-netlet-config sun-portal-netfile sun-portal-proxylet sun-portal-proxylet-config sun-portal-srasample"
GW_PKG_LIST="sun-portal-gateway sun-portal-gateway-config"
NLP_PKG_LIST="sun-portal-netletproxy sun-portal-netletproxy-config"
RWP_PKG_LIST="sun-portal-rewriterproxy sun-portal-rewriterproxy-config"

CFG_PKG_LIST="sun-portal-configurator"

COMPLETE_PKG_LIST="$CFG_PKG_LIST $PS_PKG_LIST $SRA_PKG_LIST $GW_PKG_LIST $NLP_PKG_LIST $RWP_PKG_LIST"

LOG_DIR=/var/opt/sun/portal/debug
LOG_FILE=$LOG_DIR/portal_upgrade_err.log

###############################################
# Upgrades all portal rpms to those specidied 
# in the image directory
###############################################

RpmUpgrade() {
  $MKDIR -p $LOG_DIR
  $ECHO "#########################################################" >> $LOG_FILE
  $ECHO "running upgradeportalrpms: on " `$DATE +%D` " at " `$DATE +%r` >> $LOG_FILE
  $ECHO "#########################################################" >> $LOG_FILE

  if [ -d $PS_IMAGE_DIR ]; then

    for PKG in ${COMPLETE_PKG_LIST}
    do
      $RPM -q --quiet ${PKG}
      if [ $? -eq 0 ]; then
	INSTL_PFX=`$RPM -q --queryformat "%{INSTALLPREFIX}" ${PKG}`
	if [ $? -eq 0 ];then
	  CUR_VER=`$RPM -q --queryformat "%{VERSION}" ${PKG}`
	  CUR_REL=`$RPM -q --queryformat "%{RELEASE}" ${PKG}`
          
          $RPM -qp --quiet $PS_IMAGE_DIR/${PKG}-[0-9]*.rpm
	  if [ $? -eq 0 ]; then
	    NEW_VER=`$RPM -qp --queryformat "%{VERSION}" $PS_IMAGE_DIR/${PKG}-[0-9]*.rpm`
	    NEW_REL=`$RPM -qp --queryformat "%{RELEASE}" $PS_IMAGE_DIR/${PKG}-[0-9]*.rpm`
	    $ECHO "Upgrading ${PKG} from ${CUR_VER}-${CUR_REL} to ${NEW_VER}-${NEW_REL}"
	    $RPM -Uvh --replacefiles --prefix ${INSTL_PFX} $PS_IMAGE_DIR/${PKG}-[0-9]*.rpm 2>> $LOG_FILE
	    if [ $? -ne 0 ]; then
	      $ECHO "Upgrade of ${PKG} from ${CUR_VER}-${CUR_REL} to ${NEW_VER}-${NEW_REL} failed"
	    fi	
          else
	    $ECHO "Cannot find upgrade rpm:${PKG} in $PS_IMAGE_DIR. Not upgrading"
          fi		
	else
	  $ECHO "Error in querying installed rpm: ${PKG}. Not upgrading"
	fi
      else
	$ECHO "${PKG} is not installed"
      fi
    done

  else
    $ECHO "Patch directory: ${PS_IMAGE_DIR} not found. Exiting"
    exit 1
  fi
 
  $ECHO "Please check $LOG_FILE for patching related errors/warnings"
  return
}

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

RpmUpgrade

exit 0

