LOGDIR=$ROOTDIR/var/opt/SUNWesmportal/patch
LOG=$LOGDIR/${PatchNum}.log
PATCHDIR=$ROOTDIR/var/sadm/patch/$PatchNum
PATCH01_INSTALLED=
showrev -p | grep 120594-01 > /dev/null && PATCH01_INSTALLED=yes
PKGROOT=$ROOTDIR/var/sadm/pkg
TMPDIR=$ROOTDIR/var/tmp
if [ "$TMPDIR" = "//var/tmp" ]; then
    TMPDIR="/var/tmp"
fi


die()
{
    # Restore original crontab entries
    if [ -f $LOGDIR/cronfile ]; then
        /bin/crontab $LOGDIR/cronfile && rm -f $LOGDIR/cronfile
    else
        echo "Cannot restore original cronjobs from $LOGDIR/cronfile" | \
          /bin/tee -a $LOG
    fi
    exit 1    
}

# Check for root
user=`/bin/id | /bin/grep root | /usr/bin/wc -l`
if [ $user -eq 0 ]; then
	echo "You must be root to execute this script."
        exit 1
fi

/bin/mkdir -p $LOGDIR
echo "" >> $LOG
echo "[`date`] Executing postbackout script ..." >> $LOG
echo "" >> $LOG

CACAO_BASE="`/usr/bin/pkginfo -r SUNWcacao 2>/dev/null`/SUNWcacao"
if [ "$CACAO_BASE" = "/SUNWcacao" ]; then
    echo "Could not determine SUNWcacao location." | /bin/tee -a $LOG
    exit 1
fi

PS_BASE="`/usr/bin/pkginfo -r SUNWps 2>/dev/null`/SUNWps"
if [ "$PS_BASE" = "/SUNWps" ]; then
    echo "Could not determine SUNWps location." | /bin/tee -a $LOG
    exit 1
fi

SMP_BASE="`/usr/bin/pkginfo -r SUNWesmportal 2>/dev/null`/SUNWesmportal"
if [ "$SMP_BASE" = "/SUNWesmportal" ]; then
    echo "Could not determine SUNWesmportal location." | /bin/tee -a $LOG
    exit 1
fi

# handle interruptions past this point
trap "die" 1 2 3 15

# Update the db schema
# Note:  this step is because the dbupdateschema run in the prebackout
# script always takes the db schema back to the 1.0 schema.  If patch01
# was previously on the system, we need to run this step to migrate
# the schema back to patch01 level.
$SMP_BASE/util/pgsql/portal/bin/dbupdateschema.sh >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Database migration FAILED.." | /bin/tee -a $LOG
    die
fi

# Move all old-schema db backups back into place.
/bin/rm -rf $LOGDIR/backup.${PatchNum}_patch
/bin/mv $SMP_BASE/util/pgsql/portal/backup $LOGDIR/backup.${PatchNum}_patch
if [ -d $LOGDIR/backup.${PatchNum}_unpatched ]; then
    /bin/mv $LOGDIR/backup.${PatchNum}_unpatched $SMP_BASE/util/pgsql/portal/backup
else
    mkdir -p $SMP_BASE/util/pgsql/portal/backup
    /bin/chmod 755 $SMP_BASE/util/pgsql/portal/backup
    /bin/chown esmprt1:esmprt1 $SMP_BASE/util/pgsql/portal/backup
fi

# Create a new backup 
echo "Running db backup" | tee -a $LOG
$SMP_BASE/util/pgsql/portal/bin/dbbackup.sh >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Database backup FAILED.. continuing" | /bin/tee -a $LOG
fi

if [ -z "$PATCH01_INSTALLED" ]; then
    # Copy popup.js to SUNWps area
    /bin/cp $SMP_BASE/web-src/esmportal/dt/js/popup.js $PS_BASE/web-src/esmportal/dt/js
    /bin/chown webservd:webservd $PS_BASE/web-src/esmportal/dt/js/popup.js
fi

# Restart the Portal Servers
echo "Starting servers" | tee -a $LOG
$SMP_BASE/bin/startservers >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Server Start FAILED.. continuing" | /bin/tee -a $LOG
fi

# Restart cacao
$CACAO_BASE/bin/cacaoadm start >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Cacao Start FAILED.. continuing" | /bin/tee -a $LOG
fi

if [ -z "$PATCH01_INSTALLED" ]; then
    # Redeploy popup.js
    $PS_BASE/bin/deploy redeploy | /bin/tee -a $LOG
    if [ $? -ne 0 ]; then
        echo "Redeploy of popup.js FAILED.. continuing" | /bin/tee -a $LOG
    fi

    # Remove compiled desktop jsp's
    DEPLOY_INSTANCE=`grep DEPLOY_INSTANCE= /etc/$PS_BASE/PSConfig.properties | sed -e "s/DEPLOY_INSTANCE=//"`
    rm -rf /var/$PS_BASE/https-${DEPLOY_INSTANCE}/portal/tmp/_jsps/_etc/_opt/_SUNWps/_desktop/_esmportal*
fi

# Restore original crontab entries
if [ -f $LOGDIR/cronfile ]; then
    /bin/crontab $LOGDIR/cronfile && rm -f $LOGDIR/cronfile
else
    echo "Cannot restore original cronjobs from $LOGDIR/cronfile" | \
      /bin/tee -a $LOG
fi

# Undo workaround for solaris bug 4788193
if [ -f $PKGROOT/SUNWesmdb/install/space.org ]; then
    /bin/mv $PKGROOT/SUNWesmdb/install/space.org $PKGROOT/SUNWesmdb/install/space
fi

# Provide instructions on manual steps to take after patchadd
echo | /bin/tee -a $LOG
echo "                         *** NOTICE ***" | /bin/tee -a $LOG
echo "Not all bugfixes are removed at the completion of this patchrm." | \
  /bin/tee -a $LOG
echo "The following step, which must be run manually, allows the" | \
  /bin/tee -a $LOG
echo "remaining fixes/features to be removed." | /bin/tee -a $LOG
echo "As the 'root' user, please manually execute the following commands:" | \
  /bin/tee -a $LOG
echo | /bin/tee -a $LOG
echo "\t$TMPDIR/backoutPatch.${PatchNum}" | /bin/tee -a $LOG
echo "\trm -f $TMPDIR/backoutPatch.${PatchNum}" | /bin/tee -a $LOG
echo "\trm -f $TMPDIR/checkJESRunning" | /bin/tee -a $LOG
echo | /bin/tee -a $LOG
