LOGDIR=$ROOTDIR/var/opt/SUNWesmportal/patch
LOG=$LOGDIR/${PatchNum}.log
PKGROOT=$ROOTDIR/var/sadm/pkg
THISDIR=`dirname $0`

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

# Workaround to fix pkgchk errors with installed product
relocpath=""
if [ "$ROOTDIR" != "/" ]; then
    relocpath=" -R $ROOTDIR"
fi

for f in initdb pg_ctl pg_config; do
    /usr/sbin/installf $relocpath -c changepaths SUNWesmdb $SMP_BASE/util/pgsql/bin/$f f
    if [ $? -ne 0 ]; then
        echo "Installf on $f file FAILED... continuing" | /bin/tee -a $LOG
    fi
done
/usr/sbin/installf $relocpath -f SUNWesmdb

for pkg in SUNWesmperf SUNWesmsamq SUNWesmsvcs; do
    for f in `/bin/cat $PKGROOT/$pkg/save/modules`; do
        /usr/sbin/installf $relocpath -c setpaths $pkg $f f
        if [ $? -ne 0 ]; then
            echo "Installf on $f file FAILED... continuing" | /bin/tee -a $LOG
        fi
    done
    /usr/sbin/installf $relocpath -f $pkg
done

/usr/sbin/installf $relocpath SUNWesmsvcs /etc/opt/SUNWesmportal/dpf/esmportal.properties f
if [ $? -ne 0 ]; then
    echo "Installf on esmportal.properties file FAILED... continuing" | \
      /bin/tee -a $LOG
fi
/usr/sbin/installf $relocpath -f SUNWesmsvcs

# 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:
# This convoluted approach is because of bugs in JES scripts -
# they don't return accurate return values.
# Call checkservers. A zero return means the webserver is running
# and we must try to stop it.
$SMP_BASE/bin/checkservers -wd >> $LOG 2>&1
if [ $? -eq 0 ]; then
    # Call the patched stopservers, not the broken installed
    # one to shut it down.
    $THISDIR/SUNWesmportal/reloc/SUNWesmportal/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

# Run a pre-patch backup for use later by patchrm if needed.
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.." | /bin/tee -a $LOG
    die
else
    # Move all old-schema db backups for later restoration if needed.
    if [ -d $LOGDIR/backup.${PatchNum}_unpatched ]; then
        /bin/mv $LOGDIR/backup.${PatchNum}_unpatched $LOGDIR/backup.${PatchNum}_unpatched.$$
    fi
    /bin/mv $SMP_BASE/util/pgsql/portal/backup $LOGDIR/backup.${PatchNum}_unpatched
    /bin/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
