#! /bin/sh
#
#ident	"@(#)def.dir.flp	1.17	93/07/30 SMI"
#
# Copyright (c) 1991 by Sun Microsystems, Inc.
#
# Default directory file list generator (FLG), modified by the OS group.
# Decides which FLG to run.
#
# Look first for a shell environment variable named CODEMGR_DIR_FLG.
# If it is exists, assume it names an FLG and run it.
#
# Next look for a file named "dir.flg" in the current
# directory.  If that exists, run it.
#
# Finally, do the standard thing - look for files under SCCS.
#


#
# find_files(pat,dirs...)
#
# pat   = pattern to pass to find
# dirs  = space separated list of directories for find to visit
#
find_files() {

   	pat=$1
   	shift

	for dir in $*
	do
	   if [ -d $CODEMGR_WS_ROOT/$dir ]; then
	      find $CODEMGR_WS_ROOT/$dir -name "$pat" -print | grep "/SCCS/s."
	   fi
	done

} # find_files()


echo_file() {
	#
	# Check to make sure a file exists, if it does then
	# echo it out.
	#
   	if [ -f $CODEMGR_WS_ROOT/$1 ]; then
      		echo $CODEMGR_WS_ROOT/$1
   	fi
} # echo_file()


#
# exec_file(script)
#
# script = 
#	full path name to script to be executed
#			or
#	relative path name to script to be exucuted 
#	    (relative to root of workspace)
#
# exec_file will execute the script pointed to by 'script'.  It will
# do this by interpreting the #! notation at the top of the file
# to determine what 'interpreter' to run for the script.  
#
# Note: If the ((script == /bin/sh) || (script == /usr/bin/sh)) then it is 
# actually inlined with the
#        . ${script}
# notation.  This gives the additional functionality of being able to
# inherit functions defined by the def.dir.flg script(this file).
#
exec_file() {
	FILE=$1
	if [ `expr "$FILE" : '^/'` = "0" ]; then 
		#
		# If not absolute path, then prepend $CODEMGR_WS_ROOT to
		# path.
		#
		FILE=$CODEMGR_WS_ROOT/$FILE
	fi
	MAGIC=`head -1 $FILE`
	if [ `expr "$MAGIC" : '^#!'` = "0" ]; then
		#
		# No #! notation - assume this is a SHELL script
		#
		SHELL="/bin/sh"
	else
		#
		# Set SHELL to string after #!
		#
		SHELL=`echo $MAGIC | sed -e "s/^\#\![ \t]*//"`
	fi
	if [ "(" `expr $SHELL : '^/bin/sh'` = "0" ")" -a \
	     "(" `expr $SHELL : '^/usr/bin/sh'` = "0" ")" ]; then
	   	#
	   	# Not bourne shell so we execute the interpreter itself
	   	#
	   	$SHELL $FILE
	else
	   	#
	   	# This is a bourne script so we just inline it.  This lets
	   	# us take advantage for the find_file routine already in
	   	# this script.
	   	#
	   	. $FILE
	fi
	#
	# If we executed a script we want to be sure to bring it over
	# with us.
	#
	echo $FILE

} # exec_file

dodir() {

	cd $1
	for i in * .*
	do

	case $i in

	\* | \. | \.\.)
		;;

	inc.flg)
		exec_file $1/$i
		;;

	SCCS)
		sccs_list=`echo $i/s.*`
		if [ -d $i -a ! -h $i -a "$sccs_list" != "$i/"'s.*' ] ; then
			for j in $i/s.*
			do
				echo $1/$j
			done
		fi
		;;

	*)
		if [ -d $i -a ! -h $i ] ; then
			dodir $1/$i
			cd $1
		fi
		;;
	esac

	done
} #dodir

if [ -n "$CODEMGR_DIR_FLG" ]; then
	$CODEMGR_DIR_FLG
elif [ -f dir.flg ]; then
	./dir.flg
else
	PWD=`pwd`
	#
	# The following couple of lines are to translate a the
	# CODEMGR_WS_ROOT path from a /net mount to a local directory
	# if it is actually a local disk mount.
	#
	cd $CODEMGR_WS_ROOT
	CODEMGR_WS_ROOT=`/usr/bin/pwd`
	cd $PWD
	#
	# Find and execute all req.flg's above PWD
	#

	lcd=$PWD
	while [  `expr $lcd : \.\*$CODEMGR_WS_ROOT\$ ` = "0" ]
	do
	   if [ -f $lcd/req.flg ]; then
	      exec_file $lcd/req.flg
	   fi
	   lcd=`dirname $lcd`
	done #while
	if [ -f $lcd/req.flg ]; then
	   exec_file $lcd/req.flg
	fi


	#dodir $PWD | grep '/SCCS/s\.'
	dodir $PWD 

	#
	# If find doesn't find anything it will exit 1 and bringover
	# and putback will stop.  Always exit 0 here.
	#
	exit 0
fi

