# splitdist Sun Sep 19 18:13:28 1993 CDT

# Make egrep arg list from a list of file names.
# Dots are escaped.
# egrep_list file...

egrep_list () {
	(
		_G=""
		for F; do
			echo -n "${_G}^${F}\$"
			_G="|"
		done
		echo
	) | sed -e 's/\./\\./g'
	return 0
}

###### ######
if [ -z foo ]; then

# Split directory $1 into $TARGET/distrib1, $TARGET/distrib2
# $TARGET/distrib1 contains files needed on a boot disk,
# $TARGET/distrib2 contains the rest.
# Run as root from mkdist.

/bin/echo "splitdist $*"

if [ ! "$UAD" ] ; then
	/bin/echo "Need \$UAD"
	echo 'This script should only be invoked from mkdist!  Exiting....'
	exit 1
fi

if [ $# -eq 5 ] ; then
	FORMAT=$1
	DISTRIB=$2
	TARGET=$3
	UBLOCKS=$4
	KER_BLOCKS=$5
else
	/bin/echo \
"usage: splitdist {fha|fva} distrib_dir target_dir ublocks ker_blocks"
	exit 1
fi


echo -n "Splitting $DISTRIB to $TARGET/distrib[12] .... [hit enter]"
read x

fi
###### ######
TARGET=/tmp/fvatest
DISTRIB=/k/k00/coh.386
UAD=/k/install/adm
# XCLUDE=$(cat $UAD/Files.boot.$FORMAT)
XCLUDE=Xclude

egrep_list "." $(cat Xclude)
exit 1
# Make egrep arg list from list of boot files.
EGREP_FILE=$TARGET/Egrep

( echo -n '^.$'
	for F in $(cat $XCLUDE); do
		echo -n "|^${F}\$"
	done
	echo
) | sed -e 's/\./\\./g' > $EGREP_FILE

# Make master file list in $TARGET_FILES
TARGET_FILES=$TARGET/Files
(cd $DISTRIB; find . -print |
	sed -e "s/^..//" |
	egrep -v $(cat $EGREP_FILE) > $TARGET_FILES)

exit 1

# Delete files from the list if they belong on the boot floppy.
(cd $TARGET/distrib2; rm $(cat $UAD/Files.boot.$FORMAT))

# Make $TARGET/distrib1, $TARGET/distrib2 and their subdirectories.

rm -r $TARGET/distrib1 $TARGET/distrib2 2>/dev/null
/bin/mkdir $TARGET/distrib1 $TARGET/distrib2
/bin/chmog 755 root root $TARGET/distrib1 $TARGET/distrib2

# create dirs for boot disk
# cd $TARGET/distrib1
# /bin/mkdir `cat $UAD/Dirs.boot`
# $UAD/setperm `cat $UAD/Dirs.boot` 2>&1 | egrep -v link

# Copy distribution to $TARGET/distrib2

cpdir -ad $DISTRIB $TARGET/distrib2
/bin/echo "Completed the cpdir."

# cd $TARGET/distrib2
# for i in dev `cat $UAD/Files.boot.$FORMAT`
# do
# 	mv $TARGET/distrib2/$i $TARGET/distrib1/$i
# done

# Boot files go into distrib1, not distrib2.
cd $TARGET/distrib2

/bin/echo -n "Copying files to distrib1..."
( cd $TARGET/distrib2
	cpio -pduvm $TARGET/distrib1 < $UAD/Files.boot.$FORMAT
	find dev -print | cpio -pduvm $TARGET/distrib1
)

/bin/echo -n "Removing files from distrib2..."
(cd $TARGET/distrib2; rm -r dev $(cat $UAD/Files.boot.$FORMAT))

# See if the files in distrib1 will fit on a boot floppy.

set $(du -s $TARGET/distrib1)
FBLOCKS=$(expr $1 + $KER_BLOCKS + 100)

if [ "$FBLOCKS" -gt "$UBLOCKS" ]; then
	/bin/echo "\007WARNING: Only have room for $UBLOCKS blocks on boot floppy."
	/bin/echo "But there are $FBLOCKS blocks of data in distrib1."
	/bin/echo -n "Hit <Enter> "
	read JUNK
else
	/bin/echo "Boot floppy uses $FBLOCKS of $UBLOCKS available blocks."
fi

echo "Done with splitdist."
/bin/sync
