
###########################################################################
# Name    : process_i
# Purpose : The purpose of this routine is to create the 'Remove' file
#	    and package.name file for displaypkg, so that though the
#	    package was installed from a MIPS distribution tape, it
#	    can be displayed and removed the same way as SVR3. 
# Author  : Tripty Dhar
#
# HISTORY
#
# Date		Programmer	Comment
# 11/20/90	T. Dhar		Initial Coding
#
###########################################################################
#
trap "" 1 2 3 15
. ${INDIR}/pkg_info 	#setup Pkgroot, Pkgdir and Pkg
${DIR}/chk_install		# Check if it is really installed
if [ $? != 0 ]
then
	exit
fi
echo "
==========  One moment please  =========="
PNAME=`/usr/pkg/bin/pkginfo pkgname`
PKGID=`/usr/pkg/bin/pkginfo pkgid`
Version=`/usr/pkg/bin/pkginfo version $PNAME`
TSTAMP=`/usr/pkg/bin/pkginfo timestamp`
NAME="$PKGID  -  Version: $Version"	#Content of .name file
if [ ! -f ${CONTENTS} ]
then	rm -rf ${CONTENTS}
	>> ${CONTENTS}
fi
						#Check if previously installed
SNAME=`echo $NAME | sed -e 's/[ &()$#-?\\!*;|<>]/./g' -e 's/\[/./g' -e 's/\]/./g'`
KEY=1
INSTALLED=0
KEY=`grep " $SNAME\$" $CONTENTS`
if [ $? = 0 ]			#The package was installed before
then
				# Take care of CONTENTS file
           grep -v " $SNAME\$" $CONTENTS > /tmp/$$
           mv /tmp/$$ $CONTENTS
	   INSTALLED=1
				# Take care of /usr/options/*.name file
	   for i in ${OPTIONS}/*
	   do
		#grep "^${SNAME}$" $i > /dev/null 2>&1
		grep "${SNAME}" $i > /dev/null 2>&1
		if [ $? = 0 ]
		then
			rm -rf $i		#remove the Name file
			break
		fi
	   done
fi
if [ -f ${OPTIONS}/${PNAME}.name ]
then
	num=`expr 1`
	while [ -f /usr/options/${PNAME}${num}.name ]
	do
		num=`expr $num + 1`
	done
	FNAME=${PNAME}${num}.name
else
	FNAME=${PNAME}.name
fi
CNT="$FNAME 1 $NAME"		#Content of CONTENTS file
echo $CNT >> $CONTENTS
echo $NAME > ${OPTIONS}/${FNAME}	#OPTION file

#Now we have to write the Remove script. We will depend on 'bomfiles' 
#'rmpkg' the product specific pkginfo and the executable pkginfo
					
SUBLIST=`/usr/pkg/bin/pkginfo subpkgnames`
BOMLIST=""
PLIST=""
for i in ${SUBLIST}
do
	BOMFILE=`/usr/pkg/bin/pkginfo bomname $i`
	BOMLIST="${BOMLIST} ${Pkg}/boms/${BOMFILE}" 
	PLIST="${PLIST}${PNAME}.${i} "			#installlog entry
done

#If the package was installed, find out if it was installed under the 
#same $Pkgroot. If so we will just remove the Remove file and create a new
#one, otherwise we will remove the package alltogether. Since we have one
#copy of OPTION file and one line for each product in CONTENTS file, we
#can not keep multiple copy of same package under different ROOT directory.

RMFILE=${RMDIR}/${FNAME}
if [ ${INSTALLED} = 1 ]
then
						#if the product was installed
	grep "^Pkgroot=${Pkgroot}$" ${RMFILE} > /dev/null 2>&1
	if [ $? != 0 ]
	then				#Previously installed under different
					# $Pkgroot. So remove it
		OLDROOT=`grep "^Pkgroot=" ${RMFILE}|sed -e s/Pkgroot=//`
		echo "
The previous installation of the same ${PNAME} package is being Removed"
		${RMFILE}			
	else			# Previously installed under same $Pkgroot
				#So only fix the installlog
		OLDVER=`grep "^VER=" ${RMFILE}|sed -e s/VER=//`
		OTIM=`grep "^TIM=" ${RMFILE}|sed -e s/TIM=//`
		(
		for i in ${PLIST}		
		do
		LINE="${i} ${OLDVER} ${OTIM}"
		ed ${Pkgroot}/etc/installlog <<!
			H
			/${LINE}/d
			w
			q
!
		done
		) > /dev/null 2>&1
	fi
	rm -rf ${RMFILE}
fi
curdir=`pwd`
cd $Pkgroot
${DIR}/bom_to_rm ${BOMLIST} >> ${RMFILE}        # Write the remove file
						# Edit the Remove file
(
ed $RMFILE <<!
1
i
# Created by $0. Please do not edit it.

echo "
Removing
$NAME
From $Pkgroot
Please wait ... "
(
Pkgroot=${Pkgroot}
PLIST="${PLIST}"
VER=${Version}
TIM=${TSTAMP}
cd \$Pkgroot
.
$
a
rm -rf $Pkg
for i in \${PLIST}		
	do
	LINE="\${i} \${VER} \${TIM}"
	ed \${Pkgroot}/etc/installlog << !!
	H
	/\${LINE}/d
	w
	q
!!
	done
exit 0
) > ${RMDIR}/rm.log 2>&1
.
w
q
!
) > /dev/null 2>&1
cd $curdir
chmod 755 ${RMFILE}
sync; sync
echo "The installation of the \
$NAME package is now complete."
exit 0
