#!/bin/sh
#
#ident "@(#)prepatch   1.3     99/06/11 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#
#

# Prepatch: This script is used to modify the SUNWmd postremove script.
# 
# 1. Locate the pkg itself. We have to find the right instance by checking
#    the pkg instance.
# 2. cp the postremove script to /tmp
# 3. modify the postremove script.
# 4. cp the new script back to the location

#
# catch common signals
#

trap 'echo "Caught Signal"; exit 3' 1 2 3

#
# Find the correct instance of the pkg. With upgrades there could be more 
# then one instance
# SUNWmd_REV is the pkg rev that we are looking for. 
#

SUNWmd_REV="4.2,REV=1998.02.09.12.47.28"
if [ `uname -p` = "i386" ]; then
SUNWmd_REV="4.2,REV=1998.02.09.12.48.14"
fi
pkginst=""
pkginst=`pkginfo -R ${ROOTDIR} -x SUNWmd\* | awk '/^[^ ]/ { key = $1; }/^ / { if ($2 ~ /.*'"$SUNWmd_REV"'$/) { print key; } }'`

if [ -z "$pkginst" ]; then
  echo "prepatch bailing out: Could not find pkg with revision $SUNWmd_REV"
  exit 3
fi

tmpf="$ROOTDIR/tmp/postremove.$$"
pkgloc=${ROOTDIR}/var/sadm/pkg

if [ -f ${pkgloc}/${pkginst}/install/postremove ]; then
#
# check if postremove has already been fixed.
#
  fixed=""
  fixed=`$ROOTDIR/usr/bin/grep '\^md@' ${pkgloc}/${pkginst}/install/postremove`
  if [ -z "$fixed" ]; then
	$ROOTDIR/usr/bin/cp ${pkgloc}/${pkginst}/install/postremove ${tmpf}
  else
	exit 0
  fi
else
	echo "Cannot find $pkginst pkg at $pkgloc"
	exit 3
fi

echo "patching postremove in $pkginst"

$ROOTDIR/usr/bin/sed 's/md@/^md@/' ${tmpf} > ${tmpf}.1
cp ${tmpf}.1 ${pkgloc}/${pkginst}/install/postremove
echo "postremove changed for $pkginst"

rm ${tmpf}*
exit 0
