# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#ident	"$Header: preloadpkg 1.2 89/10/10 $"
#menu# preload files from a software package onto system
#help# 
#help#	This subcommand show you a list of currently previewed software
#help#	packages.
#help#	Preloadpkg preloads files from removable media onto disk
#help#  so that they can be installed from the disk latter.
#help#  You must preview the package before you can install it.

trap 'exit 0' 1 2  15

optdir=/usr/options

ddrive=/dev/rSA/ctape0n

#list sw packages - from listpkg
unset c
if [ -d ${optdir} ]
then
	cd ${optdir}
	c=`ls /usr/options 2>/dev/null`
	if [ -n \"${c}\" ]
	then
		echo '\n\tThe following software packages have been previewed:\n'
		for i in ${c}
		do
			if [ -d /usr/options/${i} ]
			then
				if [ -s /usr/options/${i}/preview.log ]
				then
					if [ -s /usr/options/${i}/${i}.name ]
					then
						echo "${i}:\t\c"
						cat /usr/options/${i}/${i}.name
					fi
				fi
			fi
		done
	fi
fi
if [ -z "${c}" ]
then
	echo '\n\tNo software packages have been previewed.'
	exit 0
fi
echo ''

echo '\n\tEnter name of software package to preload -
\tor type  "q"  to quit - \c'
read pkg
if [ -z "${pkg}" ] || [ ${pkg} = "q" ]
then
	echo '\n\tNo software packages preloaded.'
	exit 0
fi

if [ ! -f ${optdir}/${pkg}/preview.log ]
then
	echo '\n\tRequested software has not been previewed.'
	exit 1
fi

if [ ! -f ${optdir}/${pkg}/INSTALL ]
then
	echo '\n\tINSTALL program not found. Cannot remove software.'
	exit 1
fi

echo "\\n\\tPress <RETURN> when installation media is ready to be read
\\tor type  \"q\"  to quit - \\c"
read answer
if [ "${answer}" ]
then
	exit 0
fi

cd ${optdir}

if [ ${ddrive} = "/dev/rSA/ctape0n" ]
then
	echo "\\n\\tRewinding tape..."
fi
/etc/mt -f ${ddrive} rew

skip=0
#check if the directory ${pkg} is on the tape
answer=`cpio -itc < ${ddrive} 2>/dev/null | sed -n "/$pkg\/INSTALL/p"`
if [ -z "${answer}" ]
then
        # skip past bootblock and bootprogram if this is a bootable distribution
        /etc/mt -f ${ddrive} fsf 1
        answer=`cpio -itc < ${ddrive} 2>/dev/null | sed -n "/$pkg\/INSTALL/p"`
        if [ -z "${answer}" ]
        then
                echo "\\n\\tThis distribution media does not contain ${pkg} software."
                exit 1
        fi
        skip=2
fi

# Execute the software specific INSTALL program that was read from medium.
cd ${pkg}
./INSTALL preload
