#!/bin/sh
#	buildtree --
#		Build a kernel tree from an RCS tree
# sas 850219
# bog 850813 munging.
#

version='$Header: buildtree,v 800.0 86/02/20 21:49:13 root Exp $'
echo
echo $version
echo

cruftdir=LEFTOVERS
rcslist=/tmp/rcs$$
targlist=/tmp/targ$$

case $# in
2)	;;
*)	
	echo "usage: buildtree <subdir> <destination dir>"
	exit 1
	;;
esac

echo ""
if [ -r $1/dirlist ]; then
	echo "using $1/dirlist as directory tree specification"
	# etc is a special case because there are subdirectories of it,
	# rather than toplevel files.
	dirlist=`grep -v '^etc$' $1/dirlist`
else
	echo "$1/dirlist does not exist."
	exit 2
fi

if [ -d $2 ]; then
	echo -n "$2 exists.  Clear $2? [yn] (y) "
	read x
	case $x in
		[nN]*)
			echo "$2 not cleared.  $2 will be updated."
			;;
		*)
			rm -rf $2
			mkdir $2
			echo "cleared"
			;;
	esac
else
	mkdir $2
fi

# deal specially with the etc subdirectory, if it exists -- create the
# blank directory if we need to, and add the etc subdirectories to dirlist
# so the stuff in them gets checked out correctly.
if grep '^etc$' $1/dirlist; then
	# they wanna check out the etc subdirectory? Oh GAWD...
	if [ ! -d $2/etc ]; then
		mkdir $2/etc
	fi
	extradirs=`ls -d etc/*`
fi

dirlist=`echo $dirlist; echo $extradirs`
echo "using $dirlist"

# if we blasted directory $2, then we don't need to bother with update
# comparisons.
case $x in
	[nN]*)
		# make lists of files we check out, and files in the 
		# target directory already. targlist and rcslist 
		# are those lists.
		# clear the rcslist;
		echo > $rcslist
		# put a list of the top-level garbage in the targlist.
		ls $2 | fgrep -v -f $1/dirlist | fgrep -v $cruftdir > $targlist

		for e in $dirlist
		do
			# put all files under RCS control on the rcslist.
			ls $e/RCS/*,v | sed -e 's-RCS/--' -e 's-,v--' >> $rcslist
			if [ \! -d $2/$e ]; then
				mkdir $2/$e
				for f in $e/RCS/*,v
				do
					co $f $2/$e/`basename $f ,v`
				done
			else
				# put all files in targ dir. on the targlist.
				ls $2/$e | sed -e "s-^-$e/-" >> $targlist
				for f in $e/RCS/*,v
				do
					targ=$2/$e/`basename $f ,v`
					if [ -s $targ ]; then
						# compare timestamps. 
						# If RCS file is more recent,
						# replace file in target dir 
						# with RCS one.
						rcstime=`mtime $f`
						targtime=`mtime $targ`
						if [ $rcstime -gt $targtime ]
						then
							rm -f $targ
							co $f $targ
						fi
					else
						co $f $targ
					fi
				done
			fi
		done
		# compare lists: what's in the sysRCS tree vs. what's in target.
		# move extra stuff in target to a cruft directory.
		# (leave ".o" files around.)
		sort $rcslist | grep -v '^[ 	]*$' > $rcslist.$$
		mv $rcslist.$$ $rcslist
		sort $targlist | grep -v '^[ 	]*$' > $targlist.$$
		mv $targlist.$$ $targlist
		if diff $rcslist $targlist ; then
			rm -f $rcslist $targlist
		else
			cruft=`comm -13 $rcslist $targlist | grep -v '\.o$'`
			if [ \! -d $2/$cruftdir ]; then
				mkdir $2/$cruftdir
			fi
			(cd $2; tar cf - $cruft) | (cd $2/$cruftdir; tar xf -)
			echo "rm -f $cruft $rcslist $targlist"
			# (cd $2; rm $cruft)
			echo " The following stuff was in $2 and not in RCS directory."
			echo $cruft
			echo "It has been placed in $2/$cruftdir."
		fi
		;;
	*)
		for e in $dirlist
		do
			mkdir $2/$e
			for f in $e/RCS/*,v
			do
				co $f $2/$e/`basename $f ,v`
			done
		done
		;;
esac

# link machine directory to s32 if it is there.  THIS IS A KLUDGE.
if [ -d $2/s32 ]; then
	echo "Linking s32 to machine..."
	cd $2
	rm machine
	ln -s s32 machine
	echo "done."
fi

# If there is already a compile directory there, mess around with it.
# (Might not be one, if ALL is specified, for instance...)
if [ -d $2/$1 ] ; then
	# Config up the makefile -- EVEN IF updating an old tree.
	# (conf can change)
	# If there is already a makefile there, save it for later comparison.
	if [ -s ../$1/makefile ] ; then
		mv ../$1/makefile ../$1/makefile.orig
		echo "Original makefile saved as $1/makefile.orig"
	fi
	cd $2/conf
	config $1
	# Do the make depend that config requires.
	cd ../$1
	echo make depend
	make depend
	# Remove "-O" from compilation of trap.c.
	ed makefile << EOF
/^trap.o:/
/CC/
s/-O//p
w
q
EOF
	echo "done."
	# If there was an old makefile, and if it was different, then we had 
	# better start the poor sap all over.
	if [ -s makefile.orig ] ; then
		echo
		echo "Comparing the old makefile with the new one..."
		echo
		if diff makefile.orig makefile ; then
			rm -f makefile.orig
		else
			echo
			echo "Original makefile is different from the one"
			echo "just created.  Do a make clean..."
			rm *.o
			echo
			echo "Done."
			echo
		fi
	fi
else
	echo
	echo $1 compile directory does not exist.  Remember to run "config",
	echo "make depend", and to remove the "-O" from compilation of trap.c,
	echo in whatever directories you are compiling.
	echo
fi

exit 0
