#ident	"@(#)sadmin:admin/menu/softwaremgmt/installpkg	35.1"
#menu# install new software package onto built-in disk
#help# 
#help#	Installpkg copies files from removable media onto the built-in disk and
#help#	performs additional work as necessary so you can run that software.
#help#	From then on, you will have access to those commands.

flags="-qq -k$$"
trap 'exit 0' 1 2  15

dir=${0}.d
if [ -d ${dir} ]
then
	patterns=`ls ${dir} 2>/dev/null`
else
	patterns=
fi

ddrive=`selectdevice -b $$ /dev/SA ${patterns}`
ndrive=`drivename ${ddrive}`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${ddrive}`
fi

question="
Insert the removable medium for the package you want to install
into the ${ndrive}.
Press <RETURN> when ready.  Type  q  to quit."

case ${ddrive} in
*/diskette1 | */diskette2 | */hddiskette1 | */hddiskett2 )
	while true
	do
		fulllabel=`disklabel -q "${question}" $$ ${ddrive}`
		fsname=
		eval `labelfsname "${fulllabel}"`
		case "${fsname}" in
		instal | install )
			trap '	trap "" 1 2
				cd /;  diskumount -n "${ndrive}" ${ddrive}' 0

			/etc/umount ${ddrive} 2>/dev/null
			chmod 700 /install
			/etc/mount ${ddrive} /install -r  ||  exit 1
			;;
		'' )
			echo >&2 '
	This removable medium is not labeled and will not work.
	Try another.'
			continue
			;;
		* )
			echo >&2 "
	This removable medium is labeled '${label}' and is not a package.
	The file system name is '${fsname}'.  It should be 'install'.
	The full label is:
${fulllabel}
	Try another."
			continue
		esac

		if [ ! -r /install/install/INSTALL ]
		then
			admerr $0 'Improperly built software removable medium.'
			exit 1
		fi

		trap '	trap "" 1 2
			rm -f /tmp/$$*
			cd / '"
			diskumount -n '${ndrive}' ${ddrive}" 0

		cd /tmp
		cp /install/install/INSTALL $$INSTALL
		chmod +x $$INSTALL

		trap : 1 2
		if [ -r /etc/coredirs  -a  ! -f $$installpkg ]
		then
			#	A problem in cpio makes it change the modes and
			#	ownership of directories it did not create.  This trick
			#	records and later restores the existing values.  The
			#	directories to be protected are listed in /etc/coredirs.
			cpio -o </etc/coredirs >$$installpkg 2>/dev/null
		fi
		/tmp/$$INSTALL ${ddrive} /install "${ndrive}"  ||
			echo 'WARNING:  Installation may not have completed properly.'
		trap '' 1 2
		if [ -f $$installpkg ]
		then
			#	Restore the directory modes and ownerships.
			cpio -id <$$installpkg >/dev/null 2>&1
		fi
		rm -f $$INSTALL
		diskumount -n "${ndrive}" ${ddrive}
		trap 'rm -f /tmp/$$*' 0
		trap exit 1 2
	done
	exit 0
	;;
*/ctape )
	ddrive=/dev/r40tn
	rewind_cmd="/etc/m350ctl -r ${ddrive}"
	;;
* )
	admerr $ME "Device '${ndrive}' not supported"
	exit 1
	;;
esac

trap 'exit 0' 1 2  15

INSDIR=/usr/tmp/igfdir
INSTSCRIPT=${INSDIR}/INSTALL
ME=$0

echo "$question"
read rep
if [ x$rep = xq ]
then
	exit 0
fi

#
# Get package description and name during this pass
# Remember:
#	$OUT in quotes, "", will have new-lines, and space
#	searaters if not inclosed in qoutes.
#
OUT=`ixf -dp ${ddrive}`
if [ $? -ne 0 ]
then
	admerr $ME 'Failed to read removalbe medium, exiting'
	eval ${rewind_cmd}
	exit 1
fi
if [ `echo "$OUT" | wc -l` = 2 ]
then
	set $OUT
	PACK_NAME=$1 ; shift
	PACK_DESC="$*"
else
	PACK_DESC="$*"
fi
	
echo  "\nInstalling \"$PACK_DESC\""

echo "\nPress <RETURN> to continue, type q to quit."
read rep
if [ x$rep = xq ]
then
	exit 0
fi

Here=`pwd`
mkdir -p $INSDIR 2>/dev/null
ixf -s $INSDIR ${ddrive}

if [ $? -ne 0 ]
then
	admerr $ME 'Failed to completely install package, exiting'
	eval ${rewind_cmd}
	exit 1
fi

#
# Make directory for install scripts and go there
#

cd $INSDIR 2>/dev/null
if [ -f $INSTSCRIPT ]
then
	if [ ! -x $INSTSCRIPT ]
	then
		chmod 755 $INSTSCRIPT
	fi

	$INSTSCRIPT
	Rtn=$?
else
	Rtn=0
fi

cd $Here
rm -rf $INSDIR

if [ ! -d /usr/options ]
then
	mkdir /usr/options
fi

if [ -z "$PACK_NAME" ]
then PACK_NAME=Unknown
fi

if [ $Rtn = 0 ]
then
	ixf -pdVc ${ddrive} >/usr/options/$PACK_NAME
	echo "\n\"$PACK_DESC\" is now installed"
fi
exit 0
