
rtc()
{
	echo "\nPress RETURN to continue: \c" > /dev/tty
	line < /dev/tty
}

cleanup()
{
	rm -f /tmp/*$$
	sync
	exit 1
}

cpio_cleanup()
{
	echo "Restoring files from $PWD/saveit"
	cd $PWD/saveit
	find . -type f -print | cpio -pdumv /
	rm -fr $PWD/saveit
}

change_modes()
{
	IFS_SAVE="$IFS"
IFS=" 	
"
	set `echo $LINE` > /dev/null 2>&1
	NAME=$1
	if [ $vflag ]; then
		echo " $NAME: \c"
	fi
	UID=`echo $2 | cut -c2-3`
	PERM=`echo $2 | cut -c4-6`
	case $UID in
		--)
			UID=0;;
		u-)
			UID=4;;
		-g)
			UID=2;;
		ug)
			UID=6;;
		*)
			echo "Error in user/group id field for $NAME: $UID"
			exit 1;;
	esac
	chfast $NAME 0${UID}$PERM $3 $4
	if [ $? -ne 0 ]; then
		echo "ERROR: chfast failed for $NAME"
	elif [ $vflag ]; then
		echo "modes/permission changed"
	fi
	IFS="$IFS_SAVE"

}

mcs_file()
{
	IFS_SAVE="$IFS"
	IFS=" 	
"
	set `echo $LINE` > /dev/null 2>&1
	NAME=$1
	FILE=`file $NAME`
	if [ "`echo $FILE | grep executable`" ]; then
		if [ $vflag ]; then
			echo "$NAME: mcs'ing and stripping"
		fi
		mcs -d $NAME
		strip $NAME
	elif [ "`echo $FILE | grep 'ar archive'`" ]; then
		if [ $vflag ]; then
			echo " $NAME: mcs'ing"
		fi
		mcs -d $NAME
		ar ts $NAME > /dev/null
	fi
	IFS="$IFS_SAVE"
}

mkstream()
{
	IFS="
"
	# Get valid group name
	GROUP=`echo $GROUP | tr '[a-z]' '[A-Z]'`
	VALID_GROUP=`grep "\	$GROUP\	" $MASTER1`
	if [ "`grep "\	$GROUP\	" $MASTER1`" = "" ]; then
		echo "\n${BELL}The group '$GROUP' not found in $MASTER1"
		if [ "`grep "\	$GROUP\	" $MASTER2`" != "" ]; then
			echo "\nIt does exist in the secondary"
			echo "master list ($MASTER2)"
		fi
		exit 1
	fi
	
	# Extract all lines with package=$GROUP and cpu_type=$CPU.
	# Strip these fields out at this point along with any comment lines.
	if [ $vflag ]; then
		echo "\nExtracting $GROUP files..."
	fi
	awk "{ if ( \$6 ~ /^${GROUP}$/ && ( \$7 ~ /-/ || \$7 ~ /$CPU/ ) ) print \$1,\$2,\$3,\$4,\$5 }" $MASTER1 | egrep -v '^:|\.032|\.332' > /tmp/$GROUP$$
	
	# if source_name field is '-' then default it to the dest_name
	if [ $vflag ]; then
		echo "Switching dest and source filenames..."
	fi
	awk '{ if ( $5 ~ /-/ ) print $1,$2,$3,$4,$1; else print $1,$2,$3,$4,$5 }' /tmp/$GROUP$$ | sort -o /tmp/$GROUP$$
	
	makeregs()
	{
		# make a list of regular files.
		if [ $vflag ]; then
			echo "\nMaking a list of regular files..."
		fi
		awk '{ if ( $2 !~ /^d/ && $2 !~ /^c/ && $2 !~ /^b/ ) print $0 }' /tmp/$GROUP$$ | sort -o /tmp/regs$$
		
		# Setup files per group
		if [ $vflag ]; then
			echo "\nMaking a list of group $GROUP install files..."
		fi
		cp $SETUP/$GROUP.name $USR_OPT
		if [ -s $INSTALL/$GROUP.setup ]; then
			echo "/install/$GROUP.setup	---755	2 2	/install/$GROUP.setup" >> /tmp/regs$$
		fi
		if [ -s $USR_OPT/$GROUP.unsetup ]; then
			echo "/usr/options/$GROUP.unsetup	---755	2 2	/usr/options/$GROUP.unsetup" >> /tmp/regs$$
		fi
		sort -u /tmp/regs$$ | sort -o /tmp/regs$$

	}
	makeregs

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

	if [ $GROUP = TI ]; then
		if [ $vflag ]; then
			echo "\nCopying /usr/src/lib/terminfo/*.ti files."
		fi
		cd /usr/src/lib/terminfo
		cp *.ti /install
		chmod 444 /install/*.ti > /dev/null 2>&1
		chown bin /install/*.ti > /dev/null 2>&1
		chgrp bin /install/*.ti > /dev/null 2>&1
	fi
	
	awk '{ if ( $1 != $5 ) print $0 }' /tmp/regs$$ > /tmp/src$$
	if [ -s /tmp/src$$ ]; then
		if [ $vflag ]; then
			echo "\nMaking regular files where destination does not equal source..."
			echo "\nSaving root file system files..."
		fi
		trap "cpio_cleanup; cleanup" 0
		cat /tmp/src$$ | awk '{ print $1 }' | cpio -pdumv $PWD/saveit > /dev/null 2>&1
		COUNT=1
		if [ $vflag ]; then
			echo "Processing `grep -c '^' /tmp/src$$` entries:"
		fi
		for LINE in `cat /tmp/src$$ | grep -v "usr\/options"`; do
			if [ $vflag ]; then
				echo "\r$COUNT\c"
			fi
			IFS_SAVE="$IFS"
			IFS=" 	
"
			set $LINE
			DEST=$1
			SRC=$5
			IFS="$IFS_SAVE"
			if [ ! -f $DEST ]; then
				echo "WARNING: $DEST not found" | tee -a $LOGDIR/DEST.nf
			fi
			if [ ! -b $SRC -a ! -c $SRC -a ! -f $SRC ]; then
				echo "WARNING: $SRC not found" | tee -a $LOGDIR/SRC.nf
			else
				cp $SRC $DEST
				if [ $mflag ]; then
					mcs_file
					change_modes
				fi
			fi
			COUNT=`expr $COUNT + 1`
		done
		if [ $vflag ]; then
			echo "\rDONE"
		fi
		rm -f /tmp/src$$
	fi
	
	# Make lists for devs.
	awk '{ if ( $2 ~ /^c/ || $2 ~ /^b/ ) print $0 }' /tmp/$GROUP$$ | sort -o /tmp/listdev$$
	if [ -s /tmp/listdev$$ ]; then
		cp /tmp/listdev$$ $INSTALL/$GROUP.devs
		if [ $vflag ]; then
			echo "\nMaking lists for devs..."
		fi
		sort -u /tmp/listdev$$ /tmp/regs$$ -o /tmp/regs$$
		if [ $vflag ]; then
			echo "\nMaking sure that all device major/minor numbers are correct..."
		fi
		COUNT=1
		if [ $vflag ]; then
			echo "\nProcessing `grep -c '^' /tmp/listdev$$` devices:"
		fi
		for LINE in `cat /tmp/listdev$$`; do
			IFS_SAVE="$IFS"
			IFS=" 	
"
			set $LINE
			DEV=$1
			IFS="$IFS_SAVE"
			if [ $vflag ]; then
				echo "\r$COUNT\c"
			fi
			if [ ! -c $DEV -a ! -b $DEV ]; then
				echo "WARNING: $DEV not found" | tee -a $LOGDIR/DEV.nf
			elif [ $mflag ]; then
				LSLINE=`ls -l $DEV`
				LISTCB=`echo $LINE | awk '{ print $2 }' | cut -c1`
				LSCB=`echo $LSLINE | cut -c1`
				LISTMAJ=`echo $LINE | awk '{ print $5 }' | cut -d"," -f1`
				LSMAJ=`echo $LSLINE | cut -d"," -f1 | awk '{ print $NF }'`
				LISTMIN=`echo $LINE | awk '{ print $5 }' | cut -d"," -f2`
				LSMIN=`echo $LSLINE | cut -d"," -f2 | awk '{ print $1 }'`
				if [ $LISTCB != $LSCB ]; then
					echo "$DEV (c/b): Filesystem is $LSCB, Master list is $LISTCB"
				fi
				if [ $LISTMAJ != $LSMAJ ]; then
					echo "$DEV (maj): Filesystem is $LSMAJ, Master list is $LISTMAJ"
				fi
				if [ $LISTMIN != $LSMIN ]; then
					echo "$DEV (min): Filesystem is $LSMIN, Master list is $LISTMIN"
				fi
				change_modes
			fi
			COUNT=`expr $COUNT + 1`
		done
		if [ $vflag ]; then
			echo "\rDONE"
		fi
	fi

	# Make Rlist
	echo "/usr/options/$GRP.name	 ---644 0 3	/usr/options/$GRP.name" >> /tmp/regs$$
	if [ $GROUP = TI ]; then
		awk '{ print $1 }' /tmp/regs$$ | grep -v "^/dev/" | sort -o $INSTALL/$GROUP.Rlist
	else
		awk '{ print $1 }' /tmp/regs$$ | egrep -v "^/install/|^/dev/" | sort -o $INSTALL/$GROUP.Rlist
	fi
	# Add directories that are in this list but not specified as this
	# group in master list.
	echo "\nProcessing directories..."
	if [ "$mflag" -a "$vflag" ]; then
		echo "\nChanging directory permissions and owners..."
	fi
	> /tmp/listdir$$
	> /tmp/path1$$
	PATHNAME=""
	if [ $vflag ]; then
		echo "\nBuilding a list of directories:"
		echo "Processing `grep -c '^' /tmp/regs$$` filenames..."
	fi
	COUNT=1
	IFS="
"
	for file in `{ cat /tmp/regs$$; awk '{ if ( $2 ~ /^d/ ) print $0 }' /tmp/$GROUP$$; }  | awk '{ print $1 }' | sort -u`; do
		IFS=" 	
"
		if [ $vflag ]; then
			echo "\r$COUNT\c"
		fi
		OLDPATH=$PATHNAME
		PATHNAME=`expr "${file:-.}/" : '\(/\)/*[^/]*//*$'  \| \
		"${file:-.}/" : '\(.*[^/]\)//*[^/][^/]*//*$' \| .`
		if [ "$PATHNAME" = "/" -a -d $file ]; then
			echo "$file" >> /tmp/listdir$$
		elif [ "$OLDPATH" != "$PATHNAME" ]; then
			echo $PATHNAME >> /tmp/listdir$$
		fi
		COUNT=`expr $COUNT + 1`
	done
	if [ $vflag ]; then
		echo "\rDONE"
		echo "\nCreating a list of parent directories..."
	fi
	sort -u /tmp/listdir$$ -o /tmp/listdir$$
	if [ $vflag ]; then
		echo "Processing `grep -c '^' /tmp/listdir$$` directories:"
	fi
	COUNT=1
	for LINE in `cat /tmp/listdir$$`; do
		if [ $vflag ]; then
			echo "\r$COUNT\c"
		fi
		PATHNAME=""
		for DIR in `echo $LINE/ | awk -F/ '{ for( i = 1; i < NF; i++ ) print $i }'`; do
			OLDPATH=$PATHNAME
			PATHNAME="$PATHNAME/$DIR"
			if [ "$OLDPATH" != "$PATHNAME" ]; then
				echo $PATHNAME >> /tmp/path1$$
			fi
		done
		COUNT=`expr $COUNT + 1`
	done
	if [ $vflag ]; then
		echo "\rDONE"
	fi
	sort -u /tmp/path1$$ -o /tmp/path1$$
	if [ $vflag ]; then
		echo "\nChecking for existence in the master.list..."
		echo "Processing `grep -c '^' /tmp/path1$$` files:"
	fi
	COUNT=1
	for DIRNAME in `cat /tmp/path1$$`; do
		if [ $vflag ]; then
			echo "\r$COUNT\c"
		fi
		LINE=`grep "^$DIRNAME[ 	]*d--" $MASTER1 $MASTER2 | cut -d":" -f2`
		if [ ! "$LINE" ]; then
			echo "\n\n${BELL}ERROR - missing directory entry '$DIRNAME'"
			echo "Not found in either of the two master lists."
			exit 255
		elif [ $mflag ]; then
			change_modes
		fi
		COUNT=`expr $COUNT + 1`
	done
	cp /tmp/path1$$ $INSTALL/$GROUP.dirs
	awk '{ if ( $2 ~ /^d/ ) print $0 }' /tmp/$GROUP$$  | awk '{ print $1 }' >> $INSTALL/$GROUP.dirs
	sort -u $INSTALL/$GROUP.dirs -o $INSTALL/$GROUP.dirs
	if [ $vflag ]; then
		echo "\rDONE"
	fi
	
	if [ "$vflag" -a "$mflag" ]; then
		echo "\nChanging modes on empty directories..."
	fi
	
	IFS="
"
	if [ $tflag ]; then
		for LINE in `grep "d--.*$GROUP" $MASTER1 | grep -v "^:"`; do
			IFS_SAVE="$IFS"
			IFS=" 	
"
			set $LINE
			NAME=$1
			IFS="$IFS_SAVE"
			TEMP=`grep "^${NAME}/.*${GROUP}" $MASTER1 | line`
			if [ "$TEMP" = "" ]; then
				echo $LINE >> /tmp/$GROUP.e$$
			fi
		done
		if [ -s /tmp/$GROUP.e$$ ]; then
			sort -u /tmp/$GROUP.e$$ -o $INSTALL/$GROUP.empdir
		fi
	fi

	if [ -s $INSTALL/$GROUP.empdir -a "$mflag" ]; then
		if [ $vflag ]; then
			echo "Processing `grep -c '^' $INSTALL/$GROUP.empdir` files:"
		fi
		COUNT=1
		for LINE in `cat /tmp/$GROUP.empdir`; do
			if [ $vflag ]; then
				echo "\r$COUNT\c"
			fi
			change_modes
			COUNT=`expr $COUNT + 1`
		done
		if [ $vflag ]; then
			echo "\rDONE"
		fi
	fi

	awk '{ if ( $1 == $5 ) print $0 }' /tmp/regs$$ /tmp/listdev$$ | sort -u -o /tmp/src$$
	if [ $mflag ]; then
		if [ $vflag ]; then
			echo "\nDoing file maintenence on root file system $GROUP files..."
			echo "\nThere are `grep -c '^' /tmp/src$$` files:"
		fi
		COUNT=1
		for LINE in `cat /tmp/src$$`; do
			IFS_SAVE="$IFS"
			IFS=" 	
	"
			set $LINE
			NAME=$1
			IFS="$IFS_SAVE"
			if [ $vflag ]; then
				echo "\r$COUNT\c"
			fi
			if [ ! -f $NAME ]; then
				echo "WARNING: $NAME not found" | tee -a $LOGDIR/DEST.nf
			elif [ $mflag ]; then
				mcs_file
				change_modes
			fi
			COUNT=`expr $COUNT + 1`
		done
		if [ $vflag ]; then
			echo "\rDONE"
		fi
	fi
}

prepkg()
{
	IFS=" 	
"
	if [ $vflag ]; then
		echo "Checking the prepkg list for $GROUP..."
	fi
	for i in $MASTER_GROUPS; do
		eval ${i}_PRE=0
	done
	PREPKG=$RELDIR/prepkg.list
	BASE=`grep "^${GROUP}[	].*" $PREPKG | awk '{ print $1 }'`
	NEEDED=`grep "^${GROUP}[	].*" $PREPKG | awk '{ print $2 }'`
	while [ "$BASE" ]; do
		for i in $NEEDED; do
			eval ${i}_PRE=1
			BASE=`grep "^${i}[	].*" $PREPKG | awk '{ print $1 }'`
			NEEDED=`grep "^${i}[	].*" $PREPKG | awk '{ print $2 }'`
		done
	done
	LIST=""
	for SUB_GROUP in $MASTER_GROUPS; do
		if [ "`eval echo \\$\${SUB_GROUP}_PRE`" = 1 ]; then
			LIST="$LIST $SUB_GROUP"
		fi
	done
	if [ "$LIST" ]; then
		echo $LIST > $INSTALL/$GROUP.prepkg
	fi
}

setup()
{

	# Place proper setup script(s) in $INSTALL
	if [ $sflag ]; then
		echo "\nMaking group $GROUP setup & unsetup files..."
	fi
	> $INSTALL/$GROUP.setup
	> $USR_OPT/$GROUP.unsetup
	
	IFS='?'
	FIRST=1
	for LINE in `grep -v "^:" $RELDIR/setup.list | awk '{ printf("%s?",$0) }'`; do
		IFS_SAVE="$IFS"
		IFS=" 	
"
		set $LINE
		FILE=$1
		IFS="$IFS_SAVE"
		grep "^$FILE[ 	].*$GROUP" $MASTER1 > /dev/null 2>&1
		RTN=$?
		if [ $RTN -eq 0 ]; then
			EXT=`echo $LINE | awk '{ print  $2 }'`
			if [ -s $SETUP/$EXT.setup ]; then
				if [ $GROUP = CO -a "$FIRST" ]; then
					FIRST=""
					echo "echo \"\\\nSetting up the CORE files...\\\\n\"" >> $INSTALL/$GROUP.setup
				elif [ $FIRST ]; then
					FIRST=""
					echo "echo \"\\\nSetting up the $GROUP files...\\\\n\"" >> $INSTALL/$GROUP.setup
				fi
				if [ $GROUP = TI ]; then
					cat $SETUP/$EXT.setup.tape >> $INSTALL/$GROUP.setup 
				else
					cat $SETUP/$EXT.setup >> $INSTALL/$GROUP.setup 
				fi
			fi
			if [ -s $RELDIR/setup/$EXT.unsetup ]; then
				cat $RELDIR/setup/$EXT.unsetup >> $USR_OPT/$GROUP.unsetup
			fi
		fi
	done
	if [ ! -s $INSTALL/$GROUP.setup ]; then
		echo "   WARNING: setup script $GROUP.setup not found."
		rm -f $INSTALL/$GROUP.setup 
	fi
	if [ ! -s $USR_OPT/$GROUP.unsetup ]; then
		echo "   WARNING: unsetup script $GROUP.unsetup not found."
		rm -f $USR_OPT/$GROUP.unsetup 
	fi
	IFS=" 		
"
}

size()
{
	BLOCKS=0
	COUNT=1
	if [ $vflag ]; then
		echo "Find file sizes for group $GROUP..."
		echo "\nProcessing `grep -v '^/dev/' $INSTALL/$GROUP.Rlist | grep -c '^'` entries:"
	fi
	for SIZE in `cat $INSTALL/$GROUP.Rlist | grep -v "^/dev/" | xargs ls -s | awk '{ print $1 }'`; do
		if [ $vflag ]; then
			echo "\r$COUNT\c"
		fi
		BLOCKS=`expr $BLOCKS + $SIZE`
		COUNT=`expr $COUNT + 1`
	done
	if [ $vflag ]; then
		echo "\rDONE"
	fi
	echo $BLOCKS > $INSTALL/$GROUP.size
}

#
# MAIN()
#

REL=""
PWD=/usr/src/local/release/m88k
RELDIR=/usr/src/local/release
INSTALL=/install
LOGDIR=$PWD/log
USR_OPT=/usr/options
SETUP=$RELDIR/setup

usage() {
	echo "usage: $0 [-F1|-F2|-F3|F4|-X11|-MOT] [-mlist|-mrel] [-debug] [-nclean] [-ng]"
	echo "                  [-nm] [-ns] [-nv] [-pause] [-ntape] [-tapeonly]"
	echo "\n   -F1|-F2|-F3|F4|-X11|-MOT	choose a release (required)"
	echo "   -mlist|-mrel		choose a list file:\n			'master.list' or 'master.rel' (required)"
	echo "   -debug		do not clear screen and turn on set -x"
	echo "   -nclean		do not clean \$INSTALL and \$USR_OPT"
	echo "   -ng			do not generate list files (main body)"
	echo "   -nm			do not do file maintainence"
	echo "   -ns			do not make preins, setup, and unsetup files"
	echo "   -nv			turn off verbose mode"
	echo "   -ntape		do not write to tape"
	echo "   -pause		pause before writing to tape"
	echo "   -tapeonly		write to tape (skip everything else)"
	exit 1
}
	
if [ -z "$1" ]; then
	usage > /dev/tty
fi

if [ $1 = -h -o $1 = -help ]; then
	usage > /dev/tty
fi

cflag=1
gflag=1
mflag=1
pflag=""
sflag=1
tflag=1
toflag=""
vflag=1
while [ "$1" != "" ]; do
	case $1 in
		-F1)
			CPU=3
			REL=F1;;
		-F2)
			CPU=1
			REL=F2;;
		-F3)
			CPU=4
			REL=F3;;
		-[A-Z]4)
			CPU=4
			REL=`echo $1 | sed 's/-//'`;;
		-X11)
			CPU=4
			REL=X11;;
		-MOT)
			CPU=4
			REL=MOT;;
		-nclean)
			cflag="";;
		-debug|-DEBUG|-d|-D)
			CLEAR=""; export CLEAR
			set -x;;
		-ng)
			gflag="";;
		-nm)
			mflag="";;
		-ns)
			sflag="";;
		-nv)
			vflag="";;
		-ntape)
			tflag="";;
		-pause)
			pflag=1;;
		-tapeonly)
			toflag=1;;
		-mlist)
			mlflag=1
			;;
		-mrel)
			mrflag=1
			;;
		*)
			usage > /dev/tty;;
	esac
	shift
done

if [ $mlflag ]; then
	if [ ! -x $PWD/chfast ]; then
		echo "$0: cannot open chfast"
		exit 1
	fi
	MASTER1=$RELDIR/master.list
	MASTER2=$RELDIR/master.rel
elif [ $mrflag ]; then
	MASTER1=$RELDIR/master.rel
	MASTER2=$RELDIR/master.list
else
	usage > /dev/tty
fi

for DIR in $INSTALL $LOGDIR $RELDIR $SETUP $USR_OPT; do
	if [ ! -d $DIR ]; then
		mkdir $DIR > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "$0: Couldn't make the directory $DIR" > /dev/tty
		fi
		exit 1
	fi
done

for FILE in $MASTER1 $MASTER2; do
	if [ ! -f $FILE ]; then
		echo "$0: $FILE not found" > /dev/tty
		exit 1
	fi
done


if [ -z "$REL" -o \( -z "$mlflag" -a -z "$mrflag" \) ]; then
#if [ -z "$REL" ]; then
	usage > /dev/tty
fi

if [ $toflag ]; then
	cflag=""
	gflag=""
	mflag=""
	pflag=""
	sflag=""
	vflag=""
	tflag=1
fi

if [ ! -f  $SETUP/CONTENTS.$REL ]; then
	echo "$0: $SETUP/CONTENTS.$REL not found" | tee /dev/tty
	usage > /dev/tty
fi

if [ $cflag ]; then
	rm -f $USR_OPT/* > /dev/null 2>&1
	rm -f $INSTALL/* > /dev/null 2>&1
fi

> $LOGDIR/DEV.nf
> $LOGDIR/SRC.nf
> $LOGDIR/DEST.nf

MASTER_GROUPS=`cat $SETUP/CONTENTS.$REL`
cp $SETUP/CONTENTS.$REL $INSTALL/CONTENTS
echo $REL > $INSTALL/REL

trap cleanup 0
trap "sync; exit" 1 2 3 15

if [ $tflag ]; then
	while [ "`tape -f/dev/rmt/0m rew 2>&1`" = "" ]; do
		echo $CLEAR
		echo "\n\n\007Make sure that the tape is properly installed in drive 0."
		echo "Check that the write-protect wheel is disabled."
		rtc
	done
fi

IFS=" 	
"
if [ $sflag ]; then
	echo "Starting the setup section..."
	for GROUP in $MASTER_GROUPS; do
		echo "\n===== setup: $GROUP"
		setup
		prepkg
		cp $SETUP/$GROUP.info $INSTALL
		if [ -f $SETUP/$GROUP.preins ]; then
			cp $SETUP/$GROUP.preins $INSTALL
		fi
	done
fi
IFS=" 	
"
if [ $gflag ]; then
	echo "Generating file lists..."
	for GROUP in $MASTER_GROUPS; do
		echo "\n\n===== mkstream: $GROUP"
		mkstream
   		size
	done
fi
if [ "$tflag" -a ! "$gflag" ]; then
	IFS=" 	
"
	echo "Adding necessary directory and device lists..."
	for GROUP in $MASTER_GROUPS; do
		if [ ! -s $INSTALL/$GROUP.devs ]; then
			echo "\n===== $GROUP (devs)"
			awk "{ if ( \$6 ~ /^${GROUP}$/ && ( \$7 ~ /-/ || \$7 ~ /$CPU/ ) ) print \$1,\$2,\$3,\$4,\$5 }" $MASTER1 | egrep -v '^:|\.032|\.332' | awk '{ if ( $2 ~ /^c/ || $2 ~ /^b/ ) print $0 }' | sort -o $INSTALL/$GROUP.devs
		fi
		if [ ! -s $INSTALL/$GROUP.empdir ]; then
			echo "\n===== $GROUP (dirs)"
			IFS="
"
			for LINE in `grep "d--.*$GROUP" $MASTER1 | grep -v "^:"`; do
				IFS_SAVE="$IFS"
				IFS=" 	
"
			
				set $LINE
				NAME=$1
				IFS="$IFS_SAVE"
				TEMP=`grep "^${NAME}/.*${GROUP}" $MASTER1 | line`
				if [ ! "$TEMP" ]; then
					echo $LINE >> /tmp/$GROUP.e$$
				fi
			done
			if [ -s /tmp/$GROUP.e$$ ]; then
				sort -u /tmp/$GROUP.e$$ -o $INSTALL/$GROUP.empdir
			fi
		fi
	done
fi

IFS=" 	
"
cd /

if [ $tflag ]; then
	echo "\nChanging permissions on install files..."
	chmod 775 $USR_OPT $INSTALL
	find $INSTALL $USR_OPT -exec chown root {} \;
	find $INSTALL $USR_OPT -exec chgrp sys {} \;
	
	chmod 644 $INSTALL/* $USR_OPT/*.name
	chmod 444 $INSTALL/*.ti
	chown bin $INSTALL/*.ti
	chgrp bin $INSTALL/*.ti
	chmod 755 $INSTALL/*setup $INSTALL/*preins $USR_OPT/*unsetup
	
	echo "\nMaking a size file..."
	TOTAL=0
	for GROUP in $MASTER_GROUPS; do
		SIZE=`line < $INSTALL/$GROUP.size`
		SIZE=`expr $SIZE / 512 + 1`
		SIZE=`expr $SIZE \\* 512`
		TOTAL=`expr $TOTAL + $SIZE`
	done
	if [ $vflag ]; then
		echo "Total size (in blocks) for this tape is $TOTAL"
	fi
	echo "$TOTAL" > $INSTALL/tape.size
	chmod 644 $INSTALL/tape.size
	chown root $INSTALL/tape.size
	chgrp sys $INSTALL/tape.size
	
	if [ $pflag ]; then
		echo "\nReady to write the tape."
		echo "Press RETURN to continue :\c"
		line
	fi
	cd /
	echo "\nWriting the tape header..."
	ls install/CONTENTS install/REL install/*.Rlist install/*.info install/*.prepkg install/*.setup install/*.size install/*.preins 2>/dev/null | sort | cpio -ocvC256000 > /dev/rmt/0mn
	
	cd /
	for GROUP in $MASTER_GROUPS; do
		echo "\nWriting the files for the $GROUP group..."
		if [ -f $INSTALL/$GROUP.empdir ]; then
			if [ $GROUP = TI ]; then
				{ cat $INSTALL/$GROUP.Rlist $INSTALL/$GROUP.empdir $INSTALL/$GROUP.dirs $INSTALL/$GROUP.devs | awk '{ print $1 }'; ls $INSTALL/*.ti; } | sed 's/^\///' | sort -u |  cpio -ocvC512000 > /dev/rmt/0mn
			else
				cat $INSTALL/$GROUP.Rlist $INSTALL/$GROUP.empdir $INSTALL/$GROUP.dirs $INSTALL/$GROUP.devs | awk '{ print $1 }' | sed 's/^\///' | sort -u |  cpio -ocvC512000 > /dev/rmt/0mn
			fi
		else
			if [ $GROUP = TI ]; then
				{ cat $INSTALL/$GROUP.Rlist $INSTALL/$GROUP.dirs $INSTALL/$GROUP.devs | awk '{ print $1 }'; ls $INSTALL/*.ti; } | sed 's/^\///' | sort -u |  cpio -ocvC512000 > /dev/rmt/0mn
			else
				cat $INSTALL/$GROUP.Rlist $INSTALL/$GROUP.dirs $INSTALL/$GROUP.devs | awk '{ print $1 }' | sed 's/^\///' | sort -u |  cpio -ocvC512000 > /dev/rmt/0mn
			fi
		fi
	done
	tape rew > /dev/null 2>&1
fi

exit 0
