
# This script extracts the lines of a specific group from the master list.
# It creates one or more proto files in the INSDIR directory.
# It also creates the installation and setup files in INSDIR.
# Use :mkrel to make release diskettes from the files in INSDIR.

# If PACK is non-zero, the proto files will be packed as close to
#  the maximum blocksize as possible. This has the effect of scattering
#  some directories over several diskettes in the same group. If PACK
#  is zero, the files are put in the proto list in directory order
#  until the max size is exceeded. This makes for a better ordering
#  but make more diskettes in the group.
PACK=0

CWD=/usr/src/local/release
cd $CWD
INSDIR=/install
ROOT_HIBLKS=2350
ROOT_HINODES=300
HIBLKS=2370
HINODES=500
LOBLKS=680
LONODES=150

ARG0=`basename $0`

if [ ! -d $INSDIR ]; then
	echo "$ARG0: $INSDIR not found"
	exit 1
fi

trap "rm -f /tmp/*$$" 0
trap "sync; exit" 1 2 3 15

GRP=$1
CPU=$2
REV=$3


usage()
{
	echo "Usage: $ARG0 [groupname] [cpu_type (-,1,3,4)] ['REV'|'NOREV']"
	exit 1
}

if [ -z "$GRP" -o -z "$REV" ]; then
	usage
fi

case $CPU in
	-|1|3|4)
		;;
	*)
		usage
		;;
esac

if [ $CPU = 4 ]; then
	ROOT_HIBLKS=2350
fi
case $REV in
	NOREV)
		REV=""
		;;
	[A-Z][0-9])
		REV=" [Rev $REV]"
		break
		;;
	[a-z][0-9])
		REV=`echo $REV | tr '[a-z]' '[A-Z]'`
		REV=" [Rev $REV]"
		;;
	*)
		echo "\nMust be a letter followed by a number.\c"
		usage
		;;
esac

if [ "$RELEASE" = mkall.m88k ]; then
	MASTER1=/usr/src/local/release/master.list
	MASTER2=/usr/src/local/release/master.rel
elif [ "$RELEASE" = mkall.xw88 ]; then
	MASTER1=/usr/src/local/release/master.rel
	MASTER2=/usr/src/local/release/master.list
else
	while :; do
		echo $CLEAR
		echo "\n\nWhat type of release are you working with?"
		echo "\n1) All or part of a full Opus5 release."
		echo "\n2) A smaller release."
		echo "\nEnter [1-2]: \c"
		read ANS
		case $ANS in
			1)
				MASTER1=/usr/src/local/release/master.list
				MASTER2=/usr/src/local/release/master.rel
				break;;
			2)
				MASTER1=/usr/src/local/release/master.rel
				MASTER2=/usr/src/local/release/master.list
				break;;
			*)
				echo "\n\nInvalid response."
				sleep 1;;
		esac
	done
fi
export MASTER1 MASTER2

GRP=`echo $GRP | tr '[a-z]' '[A-Z]'`
VALID_GROUP=`grep "\	$GRP\	" $MASTER1`
if [ "`grep "\	$GRP\	" $MASTER1`" = "" ]; then
	echo "\n${BELL}The group '$GRP' not found in $MASTER1"
	if [ "`grep "\	$GRP\	" $MASTER2`" != "" ]; then
		echo "\nIt does exist in the secondary"
		echo "master list ($MASTER2)"
	fi
	exit 1
fi

$CWD/dup_dir.search
if [ $? = 1 ]; then
	exit 1
fi

# Hardwire high density
#ANS=0
#while [ $ANS -ne 1 -a $ANS -ne 2 -a $ANS -ne 3 ]; do
	#echo "Select the number of blocks per proto file:"
	#echo "\n	1) Hi density floppy ($HIBLKS blks / $HINODES i-nodes)"
	#echo "\n	2) Lo density floppy ($LOBLKS blks / $LONODES i-nodes)\n"
	#echo "\n	3) Other"
	#echo "\nEnter [1-3]: \c"
	#read ANS
	ANS=1
#done
case $ANS in
	1 ) blocks=$HIBLKS
	    inodes=$HINODES
	  ;;
	2 ) blocks=$LOBLKS
	    inodes=$LONODES
	  ;;
	3 ) echo "\nEnter block count : \c"
		read ANS
		blocks=`expr $ANS`
		inodes=`expr $ANS / 7`
		echo "blocks = $blocks  inodes=$inodes"
	  ;;
esac

# Make or clean install directory
if [ -d $INSDIR/$GRP ]; then
	rm $INSDIR/* > /dev/null 2>&1
	rm $INSDIR/$GRP/* > /dev/null 2>&1
else
	mkdir $INSDIR/$GRP
fi
INSDIR="$INSDIR/$GRP"

# Extract all lines with package=$GRP and cpu_type=$CPU.
# Strip these fields out at this point along with any comment lines.
echo $CLEAR
echo "\n\nExtracting $GRP files..."
awk "{ if ( \$6 ~ /^${GRP}$/ && ( \$7 ~ /-/ || \$7 ~ /$CPU/ ) ) print \$1,\$2,\$3,\$4,\$5 }" $MASTER1 | grep -v '^:' > /tmp/lista$$

# if source_name field is '-' then default it to the dest_name
awk '{ if ( $5 ~ /-/ ) print $1,$2,$3,$4,$1; else print $1,$2,$3,$4,$5 }' /tmp/lista$$ > /tmp/listb$$
mv /tmp/listb$$ /tmp/lista$$

# make a list of regular files.
awk '{ if ( $2 !~ /^d/ && $2 !~ /^c/ && $2 !~ /^b/ ) print $0 }' /tmp/lista$$ | sort > /tmp/listb$$

# make lists for devs & dirs.
awk '{ if ( $2 ~ /^d/ ) print $0 }' /tmp/lista$$ > /tmp/listdir$$
awk '{ if ( $2 ~ /^c/ || $2 ~ /^b/ ) print $0 }' /tmp/lista$$ > /tmp/listdev$$
awk '{ if ( $2 ~ /^d/ ) print $1,$2,$3,$4 }' $MASTER1 | grep -v '^:' > /tmp/listDIR$$

# Create an /etc/coredirs for CORE group
if [ $GRP = CO ]; then
	awk '{ print $1 }' /tmp/listdir$$ | \
sed '/^\/install/d
/^\/usr\/mail\/saved/d' > $INSDIR/coredirs
fi

echo "Finding file sizes..."
# find size in blocks of each file.
> /tmp/lista$$
IFS='?'; export IFS
for LINE in `awk '{ printf("%s?",$0) }' /tmp/listb$$`
do
	IFS=" 	
"
	set $LINE
	NAME=$5
	if [ -f $NAME -o -b $NAME -o -c $NAME ]; then
		set `ls -s $NAME`
		SIZE=$1
		echo "$SIZE	$LINE" >> /tmp/lista$$
	else
		echo "$ARG0: WARNING: $NAME not found"
		echo "0	$LINE" >> /tmp/lista$$
	fi
done

if [ $PACK -ne 0 ]; then
	echo "Sorting..."
	# Sort by size, largest to smallest.
	sort -nr /tmp/lista$$ > /tmp/listb$$
else
	mv /tmp/lista$$ /tmp/listb$$
fi

echo "Dividing into proto groups..."
# Make diskette list, starting with largest file first.
rm $INSDIR/$GRP.* > /dev/null 2>&1
IBLKS=`expr $inodes / 16`
DIRBLKS=60; # 30 dirs with 2 blks per dir
DEVBLKS=`grep -c '^' /tmp/listdev$$`
HIWATER=`expr $blocks - 140`
LOWATER=`expr $blocks - 150`
CNT=1
while [ $CNT -ne 0 ]; do
	if [ $CNT -eq 1 ]; then
		SUM=`expr "3" + $IBLKS + $DIRBLKS + $DEVBLKS`
	else
		SUM=`expr "3" + $IBLKS + $DIRBLKS`
	fi
	echo "	$GRP.$CNT...\c"
	SKIP=0
	>/tmp/listc$$
	IFS='?'; export IFS
	for LINE in `awk '{ printf("%s?",$0) }' /tmp/listb$$`
	do
		IFS=" 	
"
		set $LINE
		BLKS=$1
		# adjust 512 blks to 1k min file size on disk
		RMDR=`expr $BLKS % 2`
		if [ $RMDR -eq 1 ]; then
			BLKS=`expr $BLKS + 1`
		fi
		NEWSUM=`expr $SUM + $BLKS`
		if [ $SKIP -eq 1 ]; then
			echo "$LINE" >> /tmp/listc$$
			continue
		fi
		if [ $NEWSUM -ge $HIWATER ]; then
			if [ $PACK -eq 0 ]; then
				echo $LINE >> /tmp/listc$$
				continue
			else
				echo $LINE >> /tmp/listc$$
				SKIP=1
				continue
			fi
		else
			SUM=$NEWSUM
			echo $LINE >> ${INSDIR}/$GRP.$CNT
			if [ $SUM -ge $LOWATER ]; then
				SKIP=1
				continue
			fi
		fi
		
	done
	echo "  $SUM blks total"
	FLOPCNT=$CNT
	if [ -s /tmp/listc$$ ]; then
		mv /tmp/listc$$ /tmp/listb$$
		CNT=`expr $CNT + 1`
	else
		CNT=0
	fi
done

# Remove size field from the file lists and prepend dir & dev lists.
IFS='
'
echo "/install	d--755	0 3" > /tmp/listx$$
echo "/install/INSTALL	---755	0 3	/install/INSTALL" >> /tmp/listx$$
echo "/install/ORDER	---444	0 3	/install/ORDER" >> /tmp/listx$$
echo "/usr/options/$GRP.Rlist	---444	0 3	/usr/options/$GRP.Rlist" >> /tmp/listx$$
echo "/install/UNINSTALL	---755	0 3	/install/UNINSTALL" >> /tmp/listx$$
echo "/install/$GRP.preins	---755	0 3	/install/$GRP.preins" >> /tmp/listx$$
echo "/usr/options/$GRP.unsetup	---755	0 3	/usr/options/$GRP.unsetup" >> /tmp/listx$$
echo "/usr/options	d--775 2 2" >> /tmp/listx$$
#cp $CWD/install.list /tmp/listx$$
if [ $GRP = ROOT ]; then
	echo "/install	d--775 0 3	-	ROOT	-" >> /tmp/listx$$
	echo "/mnt	d--775 0 3	-	ROOT	-" >> /tmp/listx$$
	echo "/tmp	d--777 0 3	-	ROOT	-" >> /tmp/listx$$
fi
echo "/usr/options/$GRP.name	 ---644 0 3	/usr/options/$GRP.name" >> /tmp/listx$$
LIST1=/tmp/listx$$
cp /tmp/listx$$ /tmp/listy$$
echo "/install/$GRP.setup	---644  0 3	/install/$GRP.setup
/install/LAST	---644  0 3	/install/LAST" >> /tmp/listy$$
LIST2=/tmp/listy$$

CNT=1
while [ $CNT -le $FLOPCNT ]
do
	awk '{ print $2,$3,$4,$5,$6 }' ${INSDIR}/${GRP}.${CNT} > /tmp/lista$$
	if [ $FLOPCNT -eq 1 -a $CNT -eq 1 ]; then
		cat $LIST2 /tmp/listdir$$ /tmp/listdev$$ /tmp/lista$$ | sort > $INSDIR/$GRP.$CNT
	elif [ $CNT -eq 1 ]; then
		cat $LIST1 /tmp/listdir$$ /tmp/listdev$$ /tmp/lista$$ | sort > $INSDIR/$GRP.$CNT
	elif [ $CNT -eq $FLOPCNT ]; then
		cat $LIST2 /tmp/lista$$ | sort > $INSDIR/$GRP.$CNT
	else
		cat $LIST1 /tmp/lista$$ | sort > $INSDIR/$GRP.$CNT
	fi
	CNT=`expr $CNT + 1`
done

# Add directories that are in this list but not specified as this
# group in master list.
echo "Adding directories..."
IFS='
'
CNT=1
echo "	proto file: \c"
while [ $CNT -le $FLOPCNT ]; do
	echo " $CNT \c" 
	> /tmp/listbase.$$
	for file in `cat $INSDIR/$GRP.$CNT | awk '{ print $1 }'`; do
		expr "${file:-.}/" : '\(/\)/*[^/]*//*$'  \| \
		"${file:-.}/" : '\(.*[^/]\)//*[^/][^/]*//*$' \| \
		. >> /tmp/listbase.$$
	done
	cat /tmp/listbase.$$ | sort -u -o /tmp/listbase.$$
	for LINE in `cat /tmp/listbase.$$`; do
		PATHNAME=""
		for DIR in `echo $LINE/ | awk -F/ '{ for( i = 1; i < NF; i++ ) print $i }'`; do
			PATHNAME="$PATHNAME/$DIR"
			echo $PATHNAME >> /tmp/path1.$$
		done
	done
	grep -v "^\/\." /tmp/path1.$$ | sort -u -o /tmp/path1.$$
	for i in `cat /tmp/path1.$$`; do
		DIR=`grep "^$i[ 	]*d--" $MASTER1 $MASTER2 | cut -d":" -f2`
		if [ "$DIR" = "" ]; then
			echo "\n\n${BELL}ERROR - missing directory entry '$i'."
			echo "Not found in either of the two master lists."
			exit 255
		else
			echo $DIR >> $INSDIR/$GRP.$CNT
		fi
	done
	CNT=`expr $CNT + 1`
done

echo
echo "Making proto files..."
CNT=1
while [ $CNT -le $FLOPCNT ]
do
> $INSDIR/proto.${GRP}${CNT}
echo "/dev/null
${blocks} ${inodes}
d--755 0 3" >> ${INSDIR}/proto.${GRP}${CNT}
sort ${INSDIR}/${GRP}.${CNT} | \
awk '$1 != prev { print; prev = $1 }' | \
awk '{ print $1"	"$2,$3,$4"	"$5 }' | \
sed '/	d[u-][g-]/s/\(.*\)	[^ 	]*$/\1/
{s;^/;	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
s;^\(\	*\)[^/	 ]*/;\1\	;
}'| \
awk '	#  figure out where the $ that closes a directory goes.
BEGIN	{
		FS = "\t"
	}
/./	{
		putdollar = 0
		for( i = 1; i < NF;  i++ ) {
			if( $i ~ /./ )
				break
		}
		curdepth = i
		for( j = maxdepth;  j > curdepth;  j-- ) {
			for( i = j-1;  --i; )
				printf "\t"
			print "$"
		}
		maxdepth = curdepth
	}
$0 ~ /	d--/	{
		for( i = 1; i < NF;  i++ ) {
			if( $i ~ /./ )
				break
		}
		maxdepth = i + 1;
	}
	{
		print
	}
END	{
		for( j = maxdepth;  j > 1;  j-- ) {
			for( i = j;  --i;  )
				printf "\t"
			print "$"
		}
		print "$"
	}
'| \
sed 's/,\([0-9]*\)$/\ \1/' >> $INSDIR/proto.$GRP$CNT
CNT=`expr $CNT + 1`
done

rm -f /tmp/list*$$

#----------------------- Create INSTALL script --------------------------
# install name file in /usr/options
if [ ! -f $CWD/setup/$GRP.name ]; then
	echo "Warning - sysadm name file missing: $CWD/setup/$GRP.name"
	#echo "Creating null file."
	#> $CWD/setup/$GRP.name
else
	GRP_NAME="`cat $CWD/setup/$GRP.name`"
	echo "$GRP_NAME$REV" > /usr/options/$GRP.name
fi

# install unsetup file in /usr/options
if [ ! -f $CWD/setup/$GRP.unsetup ]; then
	echo "Warning - sysadm unsetup file missing: $CWD/setup/$GRP.unsetup"
	#echo "Creating null file."
	#> $CWD/setup/$GRP.unsetup
else
	cp $CWD/setup/$GRP.unsetup /usr/options/$GRP.unsetup
fi

# set up prepackage installation checks driven by $CWD/prepkg.list
PRECHK=""
PKGNAME="$GRP_NAME$REV"
LIST=`grep "^${GRP}[	 ]" prepkg.list`
IFS='
'
for TMP in $LIST
do
if [ ! -z "$TMP" ]; then
	IFS=" 	
"
	set $TMP
	PREPKG=$2
	PRENAME=`line < /usr/options/$PREPKG.name`
	PCHK="
	eflag=0
	if
		test ! -s /usr/options/$PREPKG.name
	then
		echo \"**ERROR**  The $PKGNAME cannot be installed --\"
		echo \"It requires the $PRENAME to be installed first.\"
		eflag=1
	fi
	
	if
		[ \$eflag -eq 1 ]
	then
		exit
	fi"
else
	PCHK=""
fi
PRECHK="$PRECHK
$PCHK"
done

echo "
#
# Save mount point of floppy (INSPATH), package name (PKGNAME) and
# information for the mount & umount commands (mntdev & mntname).
#

INSPATH=/install
GROUP=$GRP
PKGNAME=\"$GRP_NAME$REV\"
LAST=$FLOPCNT
if
	test -b \"\$1\"
then
	mntdev=\$1
elif
	test -b /dev/diskette
then
	mntdev=/dev/diskette
elif
	test -b /dev/install
then
	mntdev=/dev/install
else
	echo \"**ERROR**  Can't find mount device\"
	exit
fi
mntname=\${2:-/install}

echo \"Installing the \${PKGNAME}.\"

#
# Check that the necessary packages have already been installed.
#
$PRECHK

# Do any pre-installation work.
if [ -f /install/install/\$GROUP.preins ]; then
	/install/install/\$GROUP.preins
	if [ \$? -ne 0 ]; then
		exit 255
	fi
fi


#
# Is enough space available to install the package?
#

ROOTneeds=\`expr \"\\\`du -s \${INSPATH}\\\`\" : \"\([0-9]*\).*\"\`

#
# How much space is available in the necessary filesystems?
#

ROOTspace=\`expr \"\\\`df /dev/dsk/c1d0s0 / 2>/dev/null\\\`\" : '.*: *\([0-9]*\)'\`

#
# Now see if the package fits!
# Inform the user and exit immediately, if not.
#

if
	[ \"\$ROOTspace\" -lt \"\$ROOTneeds\" ]  
then
	echo \"**ERROR**  \${PKGNAME} cannot be installed --\"
	echo \"Not enough space on the hard disk.\"
	echo \"There are \$ROOTspace blocks available\"
	echo \"on the / (root) file system --\"
	echo \"\$ROOTneeds blocks are needed.\"
	exit
fi


#
# Begin loop for multiple floppies
#

expect=1
while
	[ \$expect -le \$LAST ]
do
	if
		[ \$expect -gt 1 ]
	then
		cd /
		/etc/umount \$mntdev 2>/dev/null
		echo \"Remove floppy and insert floppy number \$expect\"
		echo \"Type <return> when ready:  \0134c\"
		read answer
		/etc/mount \$mntdev \$mntname -r 2>/dev/null
	fi

	#
	# Check to make sure this is the correct floppy of the set.
	#

	answer=\"\"
	while
		[ \`cat /install/install/ORDER\` != \$expect -a \"\$answer\" != y ]
	do
		echo \"**WARNING**   Floppy out of sequence\"
		echo \"Expecting floppy diskette number \$expect\"
		while
			[ \"\$answer\" != y -a \"\$answer\" != n ]
		do
			echo \"Type 'y' to continue or 'n' to try another floppy:  \0134c\"
			echo \"\"
			read answer
		done
		case \$answer in
			n)	cd /
				/etc/umount \$mntdev 2>/dev/null
				echo \"Remove floppy and insert correct floppy\"
				echo \"Type <return> when ready:  \0134c\"
				read answer

				#
				# Reset answer to \"\"
				# check to make sure that this
				# is now the correct floppy
				#

				answer=\"\"
				/etc/mount \$mntdev \$mntname -r 2>/dev/null
				;;

			y)	expect=\`cat /install/install/ORDER\`
				;;
		esac
	done

	echo \"The following files are being installed:\"
	# Do not cpio in 032 files on 332 release & vice versa.
	# After 032/332 specific files are installed, eliminate the
	#  file extension.
	if [ -f /bin/ns32332 ]; then
		/bin/ns32332
		NS332=\$?
	else
		NS332=1
	fi
	if [ -f /bin/ns32000 ]; then
		/bin/ns32000
		NS032=\$?
	else
		NS032=1
	fi
	if [ \$NS332 -eq 0 ]; then
		cd \${INSPATH}
		if [ -x /bin/cpio.sav ]; then
			find . -print | sed \"/^\.\/install/d
				/\.032$/d\" | tee /tmp/list | cpio.sav -pdumv /
		else
			find . -print | sed \"/^\.\/install/d
				/\.032$/d\" | tee /tmp/list | cpio -pdumv /
		fi
		LIST=\`grep '.332$' /tmp/list\`
		for i in \$LIST
		do
			j=\`echo \$i | sed 's/\.332$//'\`
			mv /\$i /\$j
		done
		rm -f /tmp/list
	elif [ \$NS032 -eq 0 ]; then
		cd \${INSPATH}
		if [ -x /bin/cpio.sav ]; then
			find . -print | sed \"/^\.\/install/d
					/\.332$/d\" | tee /tmp/list | cpio.sav -pdumv /
		else
			find . -print | sed \"/^\.\/install/d
					/\.332$/d\" | tee /tmp/list | cpio -pdumv /
		fi
		LIST=\`grep '.032$' /tmp/list\`
		for i in \$LIST
		do
			j=\`echo \$i | sed 's/\.032$//'\`
			mv /\$i /\$j
		done
		rm -f /tmp/list
	else
		cd \${INSPATH}
		if [ -x /bin/cpio.sav ]; then
			find . -print | sed \"/^\.\/install/d\" | cpio.sav -pdumv /
		else
			find . -print | sed \"/^\.\/install/d\" | cpio -pdumv /
		fi
	fi

	#
	# Increment number for next expected floppy.
	#

	echo \"Floppy diskette number \$expect is complete\"
	expect=\`expr \$expect + 1\`
done

#
# Invoke setup file
#

if [ -f /install/install/\$GROUP.setup ]; then
	sh /install/install/\$GROUP.setup
fi

#
# Give user closing message
#

echo \"Installation of the \${PKGNAME} is complete.\"
exit
" > $INSDIR/INSTALL

#----------------------- Create UNINSTALL script --------------------------
echo "
#
#	This command will remove portions of previously installed command set.
#

INSPATH=/install
RPATH=/install/install
GROUP=$GRP
PKGNAME=\"$GRP_NAME$REV\"

if [ ! -f /usr/options/$GRP.name ]; then
	echo \"The $GRP package is not installed.\"
	exit
fi

echo \"Removing the \${PKGNAME}.\"

if [ -f /install/usr/options/\$GRP.unsetup ]; then
	/install/usr/options/\$GRP.unsetup
fi

echo \"The following files are being removed:\n\"

for i in \`cat \${INSPATH}/usr/options/\${GROUP}.$GRP.Rlist\`
do
	echo \$i
	rm -f \$i
done

echo \"The \${PKGNAME} has been removed.\"
" > ${INSDIR}/UNINSTALL

> /usr/options/$GRP.Rlist
CNT=1
while [ $CNT -le $FLOPCNT ]; do
	# change proto source name for ORDER
	sed "s;/ORDER$;/ORDER${CNT};" \
		$INSDIR/proto.${GRP}${CNT} > /tmp/proto$$
	mv /tmp/proto$$ $INSDIR/proto.${GRP}${CNT}
	# Add extra dollars at end of proto to be safe.
	echo "$
$
$
$" >> $INSDIR/proto.${GRP}${CNT}

	# Make Remove list - only files, change .032/.332 entries to
	# just one entry with no extension
	cat $INSDIR/${GRP}.${CNT} | \
	awk '{ if ( $2 !~ /^d/ && $2 !~ /^c/ && $2 !~ /^b/ ) print $1 }' | \
	sed '/^\/install\//d
	/\.332$/d
	s/\.032//' | sort -u >> /usr/options/$GRP.Rlist

	if [ "`echo $GRP | grep NFS`" ]; then
		sed '/etc\/setmnt$/d
/etc\/mount$/d
/etc\/umount/d' /usr/options/$GRP.Rlist | sort -o -u /usr/options/$GRP.Rlist
	fi
	# Make ORDER files for floppy sequencing.
	echo $CNT > $INSDIR/ORDER$CNT
	echo $CNT > $INSDIR/LAST
	CNT=`expr $CNT + 1`
done

# Place proper setup script(s) in $INSDIR
echo "Making setup & unsetup files..."
> $INSDIR/$GRP.setup
> $INSDIR/$GRP.preins
> /usr/options/$GRP.unsetup
IFS='?'; export IFS
	for LINE in `awk '{ printf("%s?",$0) }' setup.list`
	do
		IFS=" 	
"
		set $LINE
		FILE=$1
		grep "^${FILE}[ 	]" $INSDIR/$GRP.* > /dev/null 2>&1
		RTN=$?
		if [ $RTN -eq 0 ]; then
			EXT=$2
			if [ -f $CWD/setup/$EXT.setup ]; then
				echo "echo \"\\\nSetting up the $EXT files...\\\\n\"" >> $INSDIR/$GRP.setup
				cat $CWD/setup/$EXT.setup >> $INSDIR/$GRP.setup 
			else
				echo "WARNING: setup script $CWD/setup/$EXT.setup not found."
			fi
			if [ -f $CWD/setup/$EXT.preins ]; then
				cat $CWD/setup/$EXT.preins >> $INSDIR/$GRP.preins
			else
				echo "# Dummy $EXT preinstall file." >> $INSDIR/$GRP.preins
			fi
			if [ -f $CWD/setup/$EXT.unsetup ]; then
				cat $CWD/setup/$EXT.unsetup >> /usr/options/$GRP.unsetup 
			else
				echo "WARNING: unsetup script $CWD/setup/$EXT.unsetup not found."
			fi
		fi
	done
	echo "exit 0" >> $INSDIR/$GRP.preins
IFS='
'
if [ $GRP = "TI" ]; then
	echo "\nCopying /usr/src/lib/terminfo/*.ti files.\n"
	cd /usr/src/lib/terminfo
	for i in `ls *.ti`
	do
		cp $i /install/TI/$i
	done
fi

if [ $GRP = "CFG" ]; then
	echo
	echo "REMINDER:"
	echo "	if pm300 - Add the GHC pakage to the precheck list."
	echo "	if pm100 or pm200 - check for SD.name[E2] || sd.name[E0]."
	echo
	echo "	Also, delete tput & include files from Rlist."
	echo
fi

if [ $GRP = ROOT ]; then
	echo
	echo "	Changing the proto file system size to $ROOT_HIBLKS blocks."
	echo
	sed "s/^$HIBLKS $HINODES/$ROOT_HIBLKS $ROOT_HINODES/" $INSDIR/proto.ROOT1 > /tmp/x$$
	mv /tmp/x$$ $INSDIR/proto.ROOT1
	echo
	echo "REMINDER - Don't forget to run DOS sorta a: on the BOOT floppy."
fi
echo "DONE"
exit 0
