#PKG2="Graphics Programming Utilities Package"
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"@(#)Install.tape	1.2  (Prime) 9/9/88"
# ----------------------------------------------------------------------
# PURPOSE:  Install application software from tape (V/386 3.1 AT version)
# ----------------------------------------------------------------------

INDIR=/usr/lib/installed
CONTENTS=${INDIR}/CONTENTS
tape_read=${DISTRIBUTOR:=none}
ERROR=0
trap "trap '' 1 2 3 9 15; cd /; rm -rf ${TMPDIR} ${CPLOG}; echo You have canceled the installation.; sync; exit 1" 1 2 3 9 15

SPACE=/etc/conf/bin/idspace

[ -z "$TERM" ] && TERM=vt100
CLEAR=`tput clear 2>/dev/null`

if [ ! -f ${CONTENTS} ]
then	rm -rf ${CONTENTS}
	>> ${CONTENTS}
fi

pkg=1
for i in $pkg
do
# The tape is read in /usr/admin/menu/softwaremgmt/chk_tape, if called 
# 'sysadm softwaremgmt'
	if [ ${tape_read} != PRIME ]
	then
		TMPDIR=/usr/tmp/install$$
		CPLOG=/tmp/cplog$$
		echo "Insert Installation Tape in drive and press <RETURN> \c"
		read a
		cd /
		rm -rf ${TMPDIR}
		if [ ! -d /usr/tmp ]
		then
			mkdir /usr/tmp
		fi
		mkdir ${TMPDIR}
		cd ${TMPDIR}
		echo "clear....."
		echo "$CLEAR"
		echo "                        Install in progress\n\n\n\n\n\n"
		cpio -icdu < /dev/rmt/ctape0 2>${CPLOG}
		if [ $? != "0" ]
		then
			echo "An error was encountered while reading in the tape."
			echo "The Installation is canceled."
			ERROR=1
			break
		fi
	else
		echo "clear....."
		echo "$CLEAR"
		echo "                        Install in progress\n\n\n\n\n\n"
		cd ${TMPDIR}
	fi 

	if [ ! -f ./Size ]
	then
		echo "The software package is missing the Size file"
		echo "The Installation is canceled."
		ERROR=1
		break
	fi
	# check the disk requirement
	linecnt=`expr \`cat Size | wc -l\``
	if [ \( $linecnt -ne 1 \) -a \( $linecnt -ge 3 \) ]
	then
		echo "Invalid Size file found.  Cannot determine disk requirements."
		echo "The installation is cancelled"
		ERROR=1
		break
	fi
	ROOT=10
	USR=10
	if [ "`grep ROOT= Size`" ]
	then ROOT=50
	     USR=50
	fi
	if [ "`grep USR= Size`" ]
	then USR=50
	fi
	if [ \( \( $ROOT = 50 \) -a \( $USR != 50 \) \) -o \( \( $ROOT != 50 \) -a \( $USR = 50 \) \) ]
	then
		echo "Invalid Size file found.  Cannot determine disk requirements."
		echo "The installation is cancelled"
		ERROR=1
		break
	fi
	if [ "`grep USR= Size`" ]
	then
		ROOT=`cat Size | grep ROOT | cut -d= -f2`
		USR=`cat Size | grep USR | cut -d= -f2`
	elif [ "`grep ROOT= Size`" ]
	then
		ROOT=`cat Size | grep ROOT | cut -d= -f2`
	else
		USR=`cat Size`
		ROOT=50
	fi
	$SPACE -u $USR > /dev/null 2>&1
	ERR=$?
	if [ $ERR = 2 ]
	then
		echo "There is not enough room in /usr partition to install \
the package."
		echo "The installation is canceled"
		ERROR=1
		break
	fi
	[ $ERR = 3 ] && ROOT=`expr $ROOT + $USR`
	$SPACE -r $ROOT > /dev/null 2>&1
	[ $? = 2 ] && ROOT="root (/)"
	if [ \( $ERR = 2 \) -o \( "$ROOT" = "root (/)" \) ]
	then
		echo "There is not enough room on the root partition to \
install the package."
		echo "The Installation is canceled."
		ERROR=1
		break
	fi

	if [ ! -f ./Install -o ! -f ./Remove -o ! -f ./Name ]
	then
		echo "The software package is missing the necessary installation programs."
		echo "The Installation is canceled."
		ERROR=1
		break
	else
		trap '' 1 2 3 9 15
		chmod +x ./Install
		NAME=`cat -s Name`
		# Check out if previously installed
		SNAME=`echo $NAME | sed -e 's/[ &()$#-?\\!*;|<>]/./g' -e 's/\[/./g' -e 's/\]/./g'`
		KEY=`grep " $SNAME\$" $CONTENTS`
		if [ -n "$KEY" ]
		then
		     echo "The $NAME package has already been installed.  \
The new installation will now replace the original $NAME files."
		     cd /usr/options
		     for i in *
		     do
			#grep "^${SNAME}$" $i > /dev/null 2>&1
			grep "${SNAME}$" $i > /dev/null 2>&1
			if [ $? = 0 ]
			then
				KEY2=$i
				break
			fi
		     done
		     cd ${TMPDIR}
		     eval KEY=`grep " $SNAME\$" $CONTENTS | cut -f1 -d' ' `
		     if [ "$KEY" ]
		     then
			   mv ${INDIR}/Remove/${KEY2} /tmp/${KEY}.R
			   if [ -f ${INDIR}/Files/${KEY2} ]
			   then
			   	mv ${INDIR}/Files/${KEY2} /tmp/${KEY}.F
			   fi
			   mv /usr/options/${KEY2} /tmp/${KEY}.O
			   grep " $SNAME\$" $CONTENTS > /tmp/${KEY}
		           grep -v " $SNAME\$" $CONTENTS > /tmp/$$
		           mv /tmp/$$ $CONTENTS
		     fi
		else KEY2=`echo $NAME | sed -e 's/ //g' | tr "[A-Z]" "[a-z]" | cut -c1-6`
		     if [ -f /usr/options/${KEY2}.name ]
		     then
			num=`expr 1`
			while [ -f /usr/options/${KEY2}${num}.name ]
			do
				num=`expr $num + 1`
			done
			KEY2=${KEY2}${num}.name
		     else
			KEY2=${KEY2}.name
		     fi
		fi
		echo "$CLEAR "
		./Install
		# It is the 'Install' script's job to print an error message
		# if the installation indicates the return of non-zero:
		if [ "$?" != "0" ]
		then
			if [ -n "$KEY" ]
			then
				mv /tmp/${KEY}.R ${INDIR}/Remove/${KEY2} > /dev/null 2>&1
				mv /tmp/${KEY}.F ${INDIR}/Files/${KEY2} > /dev/null 2>&1
				mv /tmp/${KEY}.O /usr/options/${KEY2} > /dev/null 2>&1
				cat /tmp/${KEY} >> $CONTENTS
				rm -f /tmp/${KEY} > /dev/null 2>&1
			fi
			break
		else
			if [ -n "$KEY" -a -s /tmp/${KEY}.un ]
			then
				rm -f /tmp/${KEY}.[RFO] /tmp/${KEY}
			fi
			if [ ! -d $INDIR ]
			then mkdir $INDIR; chmod 755 $INDIR
			fi
			if [ ! -d ${INDIR}/Files ]
			then mkdir ${INDIR}/Files; chmod 755 ${INDIR}/Files
			fi
			if [ ! -d ${INDIR}/Remove ]
			then mkdir ${INDIR}/Remove; chmod 755 ${INDIR}/Remove
			fi
			echo $KEY2 1 $NAME >> $CONTENTS
			mv ./Remove ${INDIR}/Remove/${KEY2}
			mv ./Name /usr/options/${KEY2}
			if [ -f Files ]
			then
				mv Files ${INDIR}/Files/${KEY2}
			fi
			rm -rf ${TMPDIR} ${CPLOG}
			sync; sync
			echo "The installation of the \
$NAME package is now complete."
		fi
	fi
done
#/usr/bin/tapecntl -w
cd /
rm -rf ${TMPDIR} ${CPLOG}
if [ $ERROR != 0 ]
then
	exit 1
fi
if [ -f /etc/.new_unix ]
then
	sync; sync
	exec /etc/conf/bin/idreboot
fi
