# start: $Id: rfpinstall.app,v 1.12.26.3 2006/06/13 14:24:49 jeremyc Exp $
test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

DATESTAMP="Thu Jun 22 12:41:11 BST 2006"

$echocmd $0 | grep '^[\.\/]*install$' >/dev/null
if [ $? -ne 0 ]; then
        $echocmd "*** This script must be run from the directory"   >&2
        $echocmd "    where the  Fixpack  package was extracted,"   >&2
        $echocmd "    using the following syntax: \"sh ./install\"" >&2
        $echocmd "exiting ..."                                      >&2
        exit 1
fi

# start: $Id: rfpcommonroutines.app,v 1.14.26.4 2006/06/13 14:24:49 jeremyc 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.03
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=TO
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.03_59
	FPRELDIR=fixpacks/Fixpack40.03_59
	BACKUPTARPATH=${FPDIR}/Fixpack40.03_59-backup.tar
	UNINSTALLSCRIPT=Fixpack40.03_59-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.03_59
		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.03_59$' > /dev/null
}
isfpinstalled()
{
	sed 's/ committed//' $COBDIR/etc/cobver | grep '^PTI=Fixpack40.03_59$' > /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=//'
}
_STOPPEDESLM=no
stopeslm()
{
        log2 "${INDENT}Checking the Enterprise Server licensing daemon is stopped. Please wait..."
	_LOG=`sleep 3 && $COBDIR/bin/eslmfgetpv k || exit 0`
	if [ "$_LOG" = 'ES License Manager successfully terminated' ]; then
	   _STOPPEDESLM=yes
	fi
	log "$_LOG"
}
starteslm()
{
	if [ "$_STOPPEDESLM" = 'yes' ]; then
           log2 "${INDENT}Restarting the Enterprise Server licensing daemon. Please wait..."
	   sleep 3 && $COBDIR/bin/eslm &
	fi
}
# end: $Id: rfpcommonroutines.app,v 1.14.26.4 2006/06/13 14:24:49 jeremyc Exp $
# start: $Id: rfpinstallroutines.app,v 1.19.8.3 2006/06/13 14:24:49 jeremyc Exp $
abort()
{
   if [ ".$_ABORT" = "." ]; then
      _ABORT=yes
      log2 "*** Installation aborted."
      if [ "x$INSTALLDIR" != "x" ]; then
         if [ -d $INSTALLDIR/$PRODUCT/.estmp.Fixpack40.03_59 ]; then
            mv $INSTALLDIR/$PRODUCT/.estmp.Fixpack40.03_59 $INSTALLDIR/$PRODUCT/estmp
         fi
         rm -f $INSTALLDIR/.oldfilelist*
         rm -f $INSTALLDIR/.newfilelist
      fi
      rm -f /tmp/Fixpack40.03_59.$$
      if isesprod ; then
         starteslm
      fi
   fi
   exit 1
}
createfpdir()
{
	mkdir -p $FPDIR || abort
	touch     $INSTALLLOGPATH
	chmod 644 $INSTALLLOGPATH
}
indicatefpinstalled()
{
	cd $FPCOBDIR/etc
	cp cobver /tmp/cobver.$$
	grep -v '^PTI=$' /tmp/cobver.$$ >cobver
	cat >>cobver <<EOF
PTI=Fixpack40.03_59
EOF
	rm /tmp/cobver.$$
}
detectfilesinuse()
{
   log2 "${INDENT}Checking for running programs. Please wait..."
   if [ ! -f $BACKUPTARPATH ]; then
      log2 "*** Error (detectfilesinuse) - no backup tar"
      log2 "${INDENT}Removing Fixpack backup directory. Please wait..."
      rm -fr $FPDIR
      abort
   fi
   # Perform the test.
   _RET=0
   cd $FPCOBDIR
   rm -f /tmp/Fixpack40.03_59.$$
   tar xf $BACKUPTARPATH 2>/tmp/Fixpack40.03_59.$$ || _RET=$? 
   if [ $_RET -ne 0 ]; then
      if [ $_RET = "$TFB_ERROR" ]; then
         log2 "\nERROR: Detected a running program."
	 if [ -f /tmp/Fixpack40.03_59.$$ ]; then
            log1 "`cat /tmp/Fixpack40.03_59.$$`"
            rm -f /tmp/Fixpack40.03_59.$$
         fi
         log2 "See install log COBDIR/fixpacks/install.log for more details."
         log2 "Please ensure that all programs currently running under"
         log2 "COBDIR have been stopped before installing this Fixpack."
         log2 "\n${INDENT}Removing Fixpack backup. Please wait..."
	 rm -fr $FPDIR
	 abort
      else
         log2 "*** Unexpected error (detectfilesinuse) ($_RET)."
	 if [ -f /tmp/Fixpack40.03_59.$$ ]; then
            log1 "`cat /tmp/Fixpack40.03_59.$$`"
	    rm -f /tmp/Fixpack40.03_59.$$
         fi
	 abort
      fi
   fi
   rm -f /tmp/Fixpack40.03_59.$$
}
installfiles()
{
   if [ ! -f $BACKUPTARPATH ]; then
      log2 "*** Error (installfiles) - no backup tar"
      log2 "${INDENT}Removing Fixpack backup directory. Please wait..."
      rm -fr $FPDIR
      abort
   fi
   # Install the non-ES files.
   log2 "${INDENT}Installing files. Please wait..."
   cd $INSTALLDIR/$PRODUCT
   rm -f ./core*
   [ -d estmp ] && mv estmp .estmp.Fixpack40.03_59
   rm -f /tmp/Fixpack40.03_59.$$
   tar cf - . | (cd $FPCOBDIR && tar xf - || touch /tmp/Fixpack40.03_59.$$) 
   [ -d .estmp.Fixpack40.03_59 ] && mv .estmp.Fixpack40.03_59 estmp 
   # Install the ES files.
   if isesprod && [ ! -f /tmp/Fixpack40.03_59.$$ ] && 
      [ -d $FPCOBDIR/.estmp.Fixpack40.03_59 ]; then
      cd $FPCOBDIR/.estmp.Fixpack40.03_59
      log2 "${INDENT}Installing Enterprise Server files. Please wait..."
      tar cf - . | (cd .. && tar xf - || touch /tmp/Fixpack40.03_59.$$)
      cd ..
   fi
   rm -fr $FPCOBDIR/.estmp.Fixpack40.03_59
   # Restore the backup if there was an error.
   if [ -f /tmp/Fixpack40.03_59.$$ ]; then
      log2 "*** Error installing files."
      log2 "${INDENT}Restoring Fixpack backup. Please wait..."
      _RET=0
      cd $FPCOBDIR 
      rm -f /tmp/Fixpack40.03_59.$$
      tar xf $BACKUPTARPATH 2>/tmp/Fixpack40.03_59.$$ || _RET=$?
      if [ $_RET -ne 0 ]; then
         log2 "*** Error restoring backup ($_RET)."
	 if [ -f /tmp/Fixpack40.03_59.$$ ]; then 
            log2 "`cat /tmp/Fixpack40.03_59.$$`"
         fi
	 log2 "The product has been left in an unknown state."
	 log2 "A backup of the original product files can be found here:"
         log2 "$BACKUPTARPATH"
      fi
      rm -f /tmp/Fixpack40.03_59.$$
      abort
   fi
}
installuninstallscript()
{
   [ -f $UNINSTALLSCRIPTPATH ] && return
   log2 "${INDENT}Generating uninstall script. Please wait..."
   cp $INSTALLDIR/.uninstall $UNINSTALLSCRIPTPATH
   createoldnewfilelists
   cat >>$UNINSTALLSCRIPTPATH <<EOF
newfilelist ()
{
EOF
   if [ -f $INSTALLDIR/.newfilelist ]; then
      cat >>$UNINSTALLSCRIPTPATH <<EOF
rm -f \$FPDIR/.filelist
cat >\$FPDIR/.filelist <<EOF2
EOF
      cat $INSTALLDIR/.newfilelist >>$UNINSTALLSCRIPTPATH
      cat >>$UNINSTALLSCRIPTPATH <<EOF 
EOF2
EOF
   else
      cat >>$UNINSTALLSCRIPTPATH <<EOF
log1 "No new files"
EOF
   fi
   cat >>$UNINSTALLSCRIPTPATH <<EOF
}
uninstallfiles
indicatefpuninstalled
removefpfiles
EOF
   if [ -f $INSTALLDIR/.newfilelist ]; then
      while read _FILE ; do
         cat >> $UNINSTALLSCRIPTPATH <<EOF
log1 "Removing file \$FPCOBDIR/$_FILE ..."
rm \$FPCOBDIR/$_FILE 2>/dev/null || log2 "WARNING: could not remove file \$FPCOBDIR/$_FILE"
EOF
      done < $INSTALLDIR/.newfilelist
      rm -f $INSTALLDIR/.newfilelist
   fi
   cat >>$UNINSTALLSCRIPTPATH <<EOF
log2 "\nUninstall of Fixpack40.03_59 complete."
set +e ; trap "" 0
EOF
}
createoldnewfilelists()
{
   cd $INSTALLDIR/$PRODUCT
   [ -d .estmp.Fixpack40.03_59 ] && mv .estmp.Fixpack40.03_59 estmp
   rm -f $INSTALLDIR/.oldfilelist*
   rm -f $INSTALLDIR/.newfilelist
   _ID=1
   for _FILE in `find . -type f`; do
      _RET=0
      echo $_FILE  | grep '^./estmp/' >/dev/null || _RET=$?
      if [ $_RET -eq 0 ]; then
         isesprod || continue
      fi
      _FILE=`echo $_FILE | sed 's#^./estmp/#./#'`
      if [ -f $FPCOBDIR/$_FILE ]; then
         grep "^$_FILE\$" $INSTALLDIR/.oldfilelist* 2>/dev/null 1>/dev/null || _RET=$?
         if [ $_RET -eq 0 ]; then
            log1 "Ignoring duplicate file $_FILE"
         else
            if [ -f $INSTALLDIR/.oldfilelist$_ID ]; then
	       _LINES=`cat $INSTALLDIR/.oldfilelist$_ID | wc -l`
               [ $_LINES -ge 500 ] && _ID=`expr $_ID + 1`
            fi
	    echo "$_FILE" >> $INSTALLDIR/.oldfilelist$_ID
         fi
      else
         echo "$_FILE" >> $INSTALLDIR/.newfilelist
      fi
   done
}
backupfiles()
{
   [ -f $BACKUPTARPATH ] && return
   log2 "${INDENT}Backing up files. Please wait..."
   createoldnewfilelists
   cd $FPCOBDIR
   _RET=0
   rm -f /tmp/Fixpack40.03_59.$$
   if [ -f $INSTALLDIR/.oldfilelist1 ]; then
      log1 "Creating backup $BACKUPTARPATH ..."
      log1 "`cat $INSTALLDIR/.oldfilelist1`"
      tar cf $BACKUPTARPATH `cat $INSTALLDIR/.oldfilelist1` 2>/tmp/Fixpack40.03_59.$$ || _RET=$?
      if [ $_RET -eq 0 ]; then
         _ID=2
         while [ -f $INSTALLDIR/.oldfilelist$_ID ]; do
            log1 "Appending .oldfilelist$_ID ..."
            log1 "`cat $INSTALLDIR/.oldfilelist$_ID`"
	    tar rf $BACKUPTARPATH `cat $INSTALLDIR/.oldfilelist$_ID` 2>/tmp/Fixpack40.03_59.$$ || _RET=$?
            [ $_RET -ne 0 ] && break
            _ID=`expr $_ID + 1`
         done
      fi
   else
      # Some platforms complain about empty tar files.
      touch $FPRELDIR/.Fixpack40.03_59
      tar cf $BACKUPTARPATH $FPRELDIR/.Fixpack40.03_59 2>/tmp/Fixpack40.03_59.$$ || _RET=$?
   fi
   # Check for an error.
   if [ $_RET -ne 0 ]; then
      log2 "*** Error backing up files ($_RET)."
      if [ -f /tmp/Fixpack40.03_59.$$ ]; then
         log2 "`cat /tmp/Fixpack40.03_59.$$`"
      fi
      log2 "${INDENT}Removing Fixpack backup. Please wait..."
      rm -fr $FPDIR
      rm -f $INSTALLDIR/.newfilelist
      rm -f $INSTALLDIR/.oldfilelist*
      rm -f /tmp/Fixpack40.03_59.$$
      abort
   fi
   rm -f $INSTALLDIR/.newfilelist
   rm -f $INSTALLDIR/.oldfilelist*
   rm -f /tmp/Fixpack40.03_59.$$
}
setperms()
{
        chmod -R 544 $FPDIR
        find $FPDIR -type d -exec chmod +x {} \;
	if [ -f $COBDIR/aslmf/aslmrmshm ]; then
	   if [ ! -u $COBDIR/aslmf/aslmrmshm ]; then
	      chown root $COBDIR/aslmf/aslmrmshm
	      chmod 4755 $COBDIR/aslmf/aslmrmshm
	   fi
	fi 
	if [ -f $COBDIR/aslmf/aslmupdb ]; then
	   if [ ! -u $COBDIR/aslmf/aslmupdb ]; then
	      chown root $COBDIR/aslmf/aslmupdb
	      chmod 4755 $COBDIR/aslmf/aslmupdb
	   fi
	fi 
}
checkcasperm()
{
   grep Fixpack_Install $COBDIR/bin/casperm.std 1>/dev/null 2>/dev/null ||
   grep Fixpack_Install ./$PRODUCT/estmp/bin/casperm.std 1>/dev/null 2>/dev/null ||
   grep Fixpack_Install ./$PRODUCT/bin/casperm.std 1>/dev/null 2>/dev/null || return 1 ;# Failure
   return 0 ;# Success
}
runcasperm()
{
   if isesprod && [ -f $COBDIR/bin/casperm.std ] && [ -f $COBDIR/etc/cas/cas.cfg ]; then
      _OUTPUT=`sh $COBDIR/bin/casperm.std Fixpack_Install`
      log2 "${INDENT}$_OUTPUT"
   fi
}
WSN=Fixpack40.03_59
if [ -f $COBDIR/bin/editor ]; then
	PRODUCT=${WSN}_dev
else
	if isesprod ; then
		PRODUCT=${WSN}_entsrv
	else
		PRODUCT=${WSN}_appsrv
	fi
fi
if [ ! -d ./$PRODUCT ]; then
	$echocmd "*** Could not identify product." >&2
	$echocmd "exiting ..."                     >&2
	exit 1
fi
if [ -d ./$PRODUCT/estmp ] && 
   [ -f $COBDIR/bin/casperm.std ] && 
   [ ! -f $COBDIR/etc/cas/cas.cfg ]; then
   $echocmd "*** You have installed Enterprise Server, but have"       >&2 
   $echocmd "not configured it. This must be done by running"          >&2
   $echocmd "$COBDIR/bin/casperm.std"                                      >&2 
   $echocmd "before you can install this Fixpack."                     >&2 
   $echocmd "exiting ..."                                              >&2
   exit 1
fi
SIZE=`du -sk ./$PRODUCT 2>/dev/null | cut -f1`
if isfpinstalled ; then
	$echocmd "*** Fixpack40.03_59 is already installed." >&2
	displayinstalledfixpacks          
	$echocmd "exiting ..." >&2
  	exit 1
fi
checkfpverno || {
	$echocmd "*** Fixpack40.03_59 cannot be installed out of sequence." >&2
	displayinstalledfixpacks
	$echocmd "exiting ..." >&2
	exit 1
}
if isesprod ; then
	checkcasperm || {
		$echocmd "*** Cannot find a suitable version of casperm." >&2
		$echocmd "exiting ..."                                    >&2
		exit 1
	}
fi
$echocmd "WARNING: This script will install Fixpack40.03_59"
$echocmd "COBDIR is set to $COBDIR"
$echocmd 
$echocmd "We recommend that you stop all programs currently"
$echocmd "running under COBDIR before installing this Fixpack."
$echocmd 
$echocmd "Please ensure that you have a minimum of ${SIZE}Kb"
$echocmd "disk space free before attempting to install this Fixpack."
$echocmd 
$echocmd Installing this Fixpack might replace product files \(including
$echocmd the Run-Time System\) that you have linked with non-Micro Focus modules.
$echocmd Please be aware that if you install this Fixpack you may need to relink
$echocmd these product files. Refer to the appropriate third-party vendor or
$echocmd in-house documentation for instructions on how to do this.
$echocmd
cont
$echocmd
# end: $Id: rfpinstallroutines.app,v 1.19.8.3 2006/06/13 14:24:49 jeremyc Exp $

setvars
createfpdir

log1 "Starting installation of Fixpack40.03_59 ..."
log1 "Fixpack date stamp: $DATESTAMP"
log1 "COBDIR is set to $COBDIR"
log1 "PRODUCT type identified as $PRODUCT"

trap "" 2 3 14 15

set -e ; trap "abort" 0

installuninstallscript
backupfiles
installfiles
indicatefpinstalled
setperms
runcasperm

log2 "\nInstallation of Fixpack40.03_59 complete."
set +e ; trap "" 0

# end: $Id: rfpinstall.app,v 1.12.26.3 2006/06/13 14:24:49 jeremyc Exp $
