# WARNING: Do not run this script to uninstall a Fixpack.
# To uninstall a Fixpack please run the script $COBDIR/bin/mfadminfp

# start: $Id: rfpuninstall.app,v 1.7 2005/04/28 13:11:32 nobody Exp $
test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

DATESTAMP="Fri Sep 30 14:23:37 GMT 2005"

$echocmd $0 | grep '^[\.\/]*.uninstall$' > /dev/null
if [ $? -eq 0 ]; then
        $echocmd "This script is not intended to be run." >&2
        $echocmd "exiting ..."                            >&2
        exit 1
fi

# start: $Id: rfpcommonroutines.app,v 1.14 2005/04/28 13:11:26 nobody Exp $
umask 000
INDENT="   "  
log()
{
	DATE=`date`
	[ -f "$INSTALLLOGPATH" ] && $echocmd "${DATE}: $1" >>$INSTALLLOGPATH
}
log1()
{
	log "$1"
}
log2()
{
	log "$1"
	$echocmd "$1"
}
if [ ".$COBDIR" = "." ] || [ ! -f $COBDIR/etc/cobver ]; then
	$echocmd "*** COBDIR must be set to point to a valid product." >&2
	$echocmd "exiting ..."                                         >&2
	exit 1
fi
pwd | grep "^$COBDIR/" >/dev/null
if [ $? -eq 0 ]; then
        $echocmd "*** This script must not be run from" >&2
        $echocmd "    inside the product directory."    >&2
        $echocmd "exiting ..."                          >&2
	exit 1
fi
if [ ! -w /etc/passwd ]; then
	$echocmd "*** You must be root to run this script." >&2
	$echocmd "exiting ..."                              >&2
	exit 1
fi
CHECKSTR=40.01
grep "$CHECKSTR$" $COBDIR/etc/cobver >/dev/null
if [ $? -ne 0 ]; then
        $echocmd "*** This Fixpack is not valid for this product." >&2
        $echocmd "exiting ..."                                     >&2
        exit 1
fi
MCCODESTR=VB
grep "$MCCODESTR/" $COBDIR/etc/cobver >/dev/null
if [ $? -ne 0 ]; then
        $echocmd "*** This Fixpack is not valid for this product." >&2
        $echocmd "exiting ..."                                     >&2
        exit 1
fi
yorn ()
{
        YN=b
        while [ "$YN" != "n" -a "$YN" != "no" -a \
                "$YN" != "N" -a "$YN" != "NO" -a \
                "$YN" != "y" -a "$YN" != "yes" -a \
                "$YN" != "Y" -a "$YN" != "YES" -a \
                "$YN" != "Yes" -a "$YN" != "No" ]; do
                $echocmd $1
                read YN
                if [ "$YN" = "y" -o "$YN" = "yes" -o "$YN" = "Yes" -o \
                     "$YN" = "Y" -o "$YN" = "YES" ]; then
                        return 1
                fi
                if [ "$YN" = "n" -o "$YN" = "no" -o "$YN" = "No" -o \
                     "$YN" = "N" -o "$YN" = "NO" ]; then
                        return 0
                fi
                $echocmd "Please enter y or n"
        done
}
cont ()
{
        yorn "Do you want to continue? (y/n): \c"
        if [ $? -ne 1 ]; then
                exit 0
        fi
}
setvars()
{
	if [ ".$FPCOBDIR" = "." ]; then
		FPCOBDIR=$COBDIR
	fi
	if [ ".$INSTALLDIR" = "." ]; then
		INSTALLDIR=`pwd`
	fi
	FPDIR=${FPCOBDIR}/fixpacks/Fixpack40.01_46
	FPRELDIR=fixpacks/Fixpack40.01_46
	BACKUPTARPATH=${FPDIR}/Fixpack40.01_46-backup.tar
	UNINSTALLSCRIPT=Fixpack40.01_46-uninstall
	UNINSTALLSCRIPTPATH=${FPDIR}/$UNINSTALLSCRIPT
	INSTALLLOGPATH=${FPCOBDIR}/fixpacks/install.log
}
getfpverno()
{
	_FP=$1
	VERNO=`echo $_FP | sed 's/ committed//' | cut -d_ -f2`
	echo "$VERNO" | grep '^[0-9][0-9]*$' >/dev/null
	if [ $? -ne 0 ]; then
		$echocmd "Bad Fixpack version number: $_FP" >&2
		$echocmd "exiting ..."                      >&2
		exit 1
	fi
	return 0 ;# Success
}
getlastfp()
{
	LASTFP=`grep '^PTI=Fixpack[0-9][0-9]*\.[0-9][0-9]*_[0-9][0-9]*' $COBDIR/etc/cobver | tail -1`
	[ ".$LASTFP" != "." ]
}
getlastfpverno()
{
	if getlastfp ; then
		getfpverno "$LASTFP"
		LASTVERNO=$VERNO
		return 0 ;# Success
	fi
	return 1 ;# Failure
}
checkfpverno()
{
	if getlastfpverno ; then
		getfpverno Fixpack40.01_46
		if [ $VERNO -lt $LASTVERNO ]; then
			return 1 ;# Failure
		fi
	fi
	return 0 ;# Success
}
islastfpcommitted()
{
	getlastfp && echo "$LASTFP" | grep ' committed$' > /dev/null
}
isfplastinstalled()
{
	getlastfp && echo "$LASTFP" | sed 's/ committed//' | grep '^PTI=Fixpack40.01_46$' > /dev/null
}
isfpinstalled()
{
	sed 's/ committed//' $COBDIR/etc/cobver | grep '^PTI=Fixpack40.01_46$' > /dev/null
}
isesprod()
{
	grep '^PTI=ES$' $FPCOBDIR/etc/cobver 1>/dev/null 2>/dev/null ||
	grep '^PTI=ES$' $COBDIR/etc/cobver 1>/dev/null 2>/dev/null
}
displayinstalledfixpacks()
{
	$echocmd "The following Fixpacks are currently installed:"
	grep '^PTI=Fixpack' $COBDIR/etc/cobver | sed -e 's/^PTI=//'
}
# end: $Id: rfpcommonroutines.app,v 1.14 2005/04/28 13:11:26 nobody Exp $
# start: $Id: rfpuninstallroutines.app,v 1.12 2005/04/28 13:11:34 nobody Exp $
abort()
{
   if [ ".$_ABORT" = "." ]; then
      _ABORT=yes
      log2 "*** Uninstall aborted."
      if [ "x$FPDIR" != "x" ]; then
         rm -f $FPDIR/.filelist*
         rm -f $FPDIR/.Fixpack40.01_46.fiu.$$.tar
      fi
      rm -f /tmp/Fixpack40.01_46.$$
   fi
   exit 1
}
indicatefpuninstalled()
{
	cd $FPCOBDIR/etc
	cp cobver /tmp/cobver.$$
	grep -v "^PTI=Fixpack40.01_46\$" /tmp/cobver.$$ >cobver
	rm /tmp/cobver.$$
}	
detectfilesinuse()
{
   if [ ! -f $BACKUPTARPATH ]; then
      log2 "*** Error (detectfilesinuse) - no backup tar"
      abort
   fi
   log2 "${INDENT}Checking for running programs. Please wait..."
   rm -f $FPDIR/.filelist
   newfilelist ;# New files.
   tar tf $BACKUPTARPATH | awk '{print $1}' >>$FPDIR/.filelist ;# Old files.
   # Split filelist into multiple files.
   _ID=1
   rm -f $FPDIR/.filelist?*
   while read _FILE ; do
      echo "$_FILE" >>$FPDIR/.filelist$_ID
      _LINES=`cat $FPDIR/.filelist$_ID | wc -l`
      if [ $_LINES -ge 500 ]; then
         _ID=`expr $_ID + 1`
      fi
   done < $FPDIR/.filelist
   rm -f $FPDIR/.filelist
   # Check for files in use.
   _ID=1
   while [ -f $FPDIR/.filelist$_ID ]; do
      cd $FPCOBDIR
      log1 "Checking .filelist$_ID ..."
      log1 "`cat $FPDIR/.filelist$_ID`"
      rm -f ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar
      tar cf ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar `cat $FPDIR/.filelist$_ID`
      _RET=0
      rm -f /tmp/Fixpack40.01_46.$$
      tar xf ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar 2>/tmp/Fixpack40.01_46.$$ || _RET=$?
      if [ $_RET -ne 0 ]; then
         if [ $_RET = "$TFB_ERROR" ]; then
	    log2 "\nERROR: Detected a running program."
	    if [ -f /tmp/Fixpack40.01_46.$$ ]; then
               log1 "`cat /tmp/Fixpack40.01_46.$$`"
            fi
    $echocmd "See the install log COBDIR/fixpacks/install.log for more details."
    $echocmd "Please ensure that all programs currently running under"
    $echocmd "COBDIR have been stopped before uninstalling this Fixpack.\n"
	    rm -f ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar
            rm -f ${FPDIR}/.filelist*
            rm -f /tmp/Fixpack40.01_46.$$
	    abort
         else
	    log2 "*** Unexpected error (detectfilesinuse) ($_RET)."
	    log2 "A backup of the product files that was used"
	    log2 "to detect files in use, has been left in:"
	    log2 "${FPDIR}/.Fixpack40.01_46.fiu.$$.tar"
	    if [ -f /tmp/Fixpack40.01_46.$$ ]; then
               log1 "`cat /tmp/Fixpack40.01_46.$$`"
            fi
	    rm -f ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar
            rm -f ${FPDIR}/.filelist*
            rm -f /tmp/Fixpack40.01_46.$$
	    abort
	 fi
      fi
      rm -f ${FPDIR}/.Fixpack40.01_46.fiu.$$.tar
      rm -f /tmp/.Fixpack40.01_46.$$
      _ID=`expr $_ID + 1`
   done 
   rm -f ${FPDIR}/.filelist*
}
uninstallfiles()
{
   if [ ! -f $BACKUPTARPATH ]; then
      log2 "*** Unexpected error (uninstallfiles) - no backup tar"
      abort
   fi
   log2 "${INDENT}Restoring product files. Please wait..."
   _RET=0
   cd $FPCOBDIR
   tar xf $BACKUPTARPATH 2>/tmp/Fixpack40.01_46.$$ || _RET=$?
   if [ $_RET -ne 0 ]; then
      log2 "*** Error uninstalling files ($_RET)"
      if [ -f /tmp/Fixpack40.01_46.$$ ]; then
         log1 "`cat /tmp/Fixpack40.01_46.$$`"
      fi
      $echocmd "Unexpected error (uninstallfiles)"
      $echocmd "Your product has been left in an unknown state."
      $echocmd "Please retry uninstalling this Fixpack."
      rm -f /tmp/Fixpack40.01_46.$$
      abort
   fi
   rm -f /tmp/Fixpack40.01_46.$$
}
removefpfiles()
{
	$echocmd $FPDIR | grep 'fixpacks/F*' >/dev/null && rm -fr $FPDIR
}
isfpinstalled || {
	$echocmd "*** Fixpack40.01_46 is not installed." >&2
	displayinstalledfixpacks
	$echocmd "exiting ..."                               >&2
  	exit 1
}
isfplastinstalled || {
	$echocmd "*** Only the last installed Fixpack can be uninstalled." >&2
	displayinstalledfixpacks
	$echocmd "exiting ..."                                             >&2
	exit 1
}
if islastfpcommitted ; then
	$echocmd "*** Fixpack40.01_46 cannot be uninstalled because it has been committed." >&2
	displayinstalledfixpacks
	$echocmd "exiting ..." >&2
	exit 1
fi
$echocmd "\nWARNING: This will remove Fixpack40.01_46 from your product."
cont
$echocmd "\nWARNING: We recommend that you stop all programs currently"
$echocmd "running under COBDIR before uninstalling this Fixpack."
cont
$echocmd ""
# end: $Id: rfpuninstallroutines.app,v 1.12 2005/04/28 13:11:34 nobody Exp $

setvars

log1 "Starting uninstall of Fixpack40.01_46 ..."
log1 "Fixpack date stamp: $DATESTAMP"
log1 "COBDIR is set to $COBDIR"

trap "" 2 3 14 15

set -e ; trap "abort" 0

# end: $Id: rfpuninstall.app,v 1.7 2005/04/28 13:11:32 nobody Exp $
