LOGDIR=$ROOTDIR/var/opt/SUNWesmportal/patch
LOG=$LOGDIR/${PatchNum}.log
PATCHDIR=$ROOTDIR/var/sadm/patch/$PatchNum
PKGROOT=$ROOTDIR/var/sadm/pkg
TMPDIR=$ROOTDIR/var/tmp

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
    # Put moved files back as they were
    if [ ! -f $SMP_BASE/util/pgsql/portal/ddl/updates/versions-patchrm.toc ]; then
        /bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc $SMP_BASE/util/pgsql/portal/ddl/updates/versions-patchrm.toc
        /bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc.save $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc
    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 prebackout 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

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

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

# Get rid of crontab entries temporarily, after saving current ones
/bin/crontab -l > $LOGDIR/cronfile
if [ $? -ne 0 ]; then
    echo "Warning: could not create saved cron file in $LOGDIR" | \
     /bin/tee -a $LOG
fi

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

tmpfile=/tmp/$$crontab
/bin/crontab -l | sed -e "/^#SUNWesmdb - begin/,/^#SUNWesmsvcs - end/ d" > $tmpfile
if [ $? -ne 0 ]; then
    echo "Warning: could not create temporary cron file $tmpfile" | \
     /bin/tee -a $LOG
else
    /bin/crontab $tmpfile
    if [ $? -ne 0 ]; then
        echo "Warning: could not remove cron entries" | /bin/tee -a $LOG
    fi
fi
rm -f $tmpfile

# Stop cacao 
echo "Stopping servers" | /bin/tee -a $LOG
$CACAO_BASE/bin/cacaoadm stop >> $LOG 2>&1
if [ $? -ne 0 ]; then
    echo "Cacao Stop FAILED.." | /bin/tee -a $LOG
    die
fi

# Stop Portal Servers:
$SMP_BASE/bin/checkservers -wd >> $LOG 2>&1
if [ $? -eq 0 ]; then
    $SMP_BASE/bin/stopservers >> $LOG 2>&1
    $SMP_BASE/bin/checkservers -wd >> $LOG 2>&1
    if [ $? -eq 0 ]; then
        echo "Server Stop FAILED.." | /bin/tee -a $LOG
        die
    fi
fi

# Replace the old db schema
# Note:  this step returns the db schema back to the 1.0 schema
# whether patch01 was previously on the system or not.
# The postbackout script will run dbupdateschema a 2nd time to
# restore the db schema to post-patch01 schema if necessary.
ec=0
/bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc.save || ec=1
/bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions-patchrm.toc $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc || ec=1
if [ $ec -eq 1 ]; then
    echo "Preparation for dbupdateschema FAILED" | /bin/tee -a $LOG
    die
fi

$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

/bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc $SMP_BASE/util/pgsql/portal/ddl/updates/versions-patchrm.toc
/bin/mv $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc.save $SMP_BASE/util/pgsql/portal/ddl/updates/versions.toc

# Save a copy of the backoutPatch scripts and the patch README file from
# being removed by patchrm - the backoutPatch scripts need to be executed
# after the patched files are removed.
cp $SMP_BASE/config/backoutPatch.* $TMPDIR
cp $PATCHDIR/README.$PatchNum $TMPDIR

# Save a copy of the patched checkJESRunning script too, so backoutPatch
# doesn't show a thrown exception (keep user from seeing bug#6334109)
cp $SMP_BASE/bin/checkJESRunning $TMPDIR
