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

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

# For completeness, cat the contents of the patchadd log to our logfile.
if [ -s "$PATCHDIR/log" ]; then
    echo "" >> $LOG
    echo "Contents of $PATCHDIR/log included here for completeness..." >> $LOG
    echo "----------------------------------------------------------------------" >> $LOG
    cat $PATCHDIR/log >> $LOG
    echo "----------------------------------------------------------------------" >> $LOG
    echo "" >> $LOG
fi

echo "[`date`] Executing postpatch 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

# Set the paths in the patched samqfs xml file
CWD=`pwd`
cd $SMP_BASE/services/samqfs/lib
cp com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml.tmp
cacaocfgroot=`pkginfo -r SUNWcacaocfg`
cat com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml.tmp | \
sed -e "s=%%INSTALL_SAMQ%%=$SMP_BASE/services/samqfs=g" \
    -e "s=%%QUARTZROOT%%=$SMP_BASE/services/base=g" \
    -e "s=%%CACAOROOT%%=\\$cacaocfgroot=g" > com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml
if [ $? -ne 0 ]; then
    echo "Unable to fix samqfs xml file"
fi
rm -f com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml.tmp
cd $CWD

relocpath=""
if [ "$ROOTDIR" != "/" ]; then
    relocpath=" -R $ROOTDIR"
fi

/usr/sbin/installf $relocpath -c setpaths SUNWesmsamq $SMP_BASE/services/samqfs/lib/com.sun.netstorage.mgmt.esm.logic.collector.adapter.samqfs.impl.SamQfsModule.xml f
if [ $? -ne 0 ]; then
    echo "Installf on $f file FAILED... continuing" | /bin/tee -a $LOG
fi
/usr/sbin/installf $relocpath -f SUNWesmsamq

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

# Update the db schema
$SMP_BASE/util/pgsql/portal/bin/dbupdateschema.sh >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Database migration FAILED.." | /bin/tee -a $LOG
    if [ -d $LOGDIR/backup.${PatchNum}_unpatched ]; then
        /bin/rm -rf $SMP_BASE/util/pgsql/portal/backup
        /bin/mv $LOGDIR/backup.${PatchNum}_unpatched $SMP_BASE/util/pgsql/portal/backup
    fi
    die
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 available at the completion of this patchadd." | \
  /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 made available." | /bin/tee -a $LOG
echo "As the 'root' user, please manually execute the following command:" | \
  /bin/tee -a $LOG
echo | /bin/tee -a $LOG
echo "\t$SMP_BASE/config/updatePatch.${PatchNum}" | /bin/tee -a $LOG
echo | /bin/tee -a $LOG
