#!/bin/sh
#
#ident	"@(#)mk_bld_space.sh	1.9	95/04/13 SMI"
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved.
#
USAGE='usage:	mk_bld_space [-m MACH] [-r] [-w CODEMGR_WS] <bld_space>

	-m	machine instruction set, e.g., i386, ppc, sparc
		the default is what is returned by "uname -p"
	-r	release-style build space: uses only s-dot files in "parent"
		so "parent" can be missing g-files
	-w	workspace "parent" to build space, as absolute path
		(-p for "parent" also works)

	<bld_space> is the directory in which to create or extend a source
	tree of sccs g-files for doing a build.
'

# define functions

# ( for -r option )
# function called at each SCCS node of the tree in the growing
# build space to create that node and to do "sccs get" using a
# temporary symlink named SCCS.  The symlink points to the
# corresponding node in CODEMGR_WS.
#
do_gets () {
	# grow the tree
	mkdir -p $TREE_ROOT/$1 2> /dev/null
	# insert temporary symlink to SCCS
	ln -s $CODEMGR_WS/$1/SCCS $TREE_ROOT/$1/SCCS
	# do gets
	(cd $TREE_ROOT/$1; sccs get -s SCCS)
	# remove the symlink
	rm $TREE_ROOT/$1/SCCS
}

# ( for -r option )
# recursive function to traverse the directory tree, finding
# SCCS directories and calling do_gets for each such node.
# Skips instruction-set architecture(s) named in SKIPMACH
#
bld_tree () {
	echo ...working with $1
	for OBJ in .* *
	do
	  # eval allows interpretation of the value of SKIPMACH
	  eval 'case $OBJ in
		  . | .. | \* )	         :;;# special cases, skip these
		  .del-* )	         :;;# skip deleted files
		  SCCS )	         do_gets $1;;
		  '$SKIPMACH' )	         :;;
		  Codemgr_wsdata )       :;;# skip to avoid backup s-dot files
		  old | deleted_files )  :;;
		  * )	    if [ -d $OBJ ]
				    then
					# skip any links  to directories
					   if [ ! -h $OBJ ]
					   then
						(cd $OBJ; bld_tree $1/$OBJ)
						fi ;
				   fi;;
		esac'
	done
}

# ( with no -r option )
# recursive function to traverse the directory tree, finding
# and echoing the names of s-dot files under SCCS.
# Skips instruction-set architecture(s) named in SKIPMACH
#
find_s_dots () {
	for OBJ in .* *
	do
	  # eval allows interpretation of the value of SKIPMACH
	  eval 'case $OBJ in
		  . | .. | \* )	         :;;# special cases, skip these
		  s..del-* )	         :;;# skip deleted files
		  s.* )		         echo $1/$OBJ;;
		  '$SKIPMACH' )	         :;;
		  Codemgr_wsdata )       :;;# skip to avoid backup s-dot files
		  old | deleted_files )  :;;
		  * )		   if [ -d $OBJ ]
				   then
					# skip any links  to directories
					if [ ! -h $OBJ ]
					then
						(cd $OBJ; find_s_dots $1/$OBJ)
					fi;
				   fi;;
		esac'
	done
}

# begin script

# initialize option flags
m_FLAG=n
r_FLAG=n
# w_FLAG initialization not necessary

while getopts m:p:rw: FLAG
do
	case $FLAG in
	  m )	  m_FLAG=y
		  MACH=$OPTARG;;
	  r )	  r_FLAG=y;;
	  w | p ) CODEMGR_WS="$OPTARG";;
	 \? )	  echo "$USAGE"
		  exit 1 ;;
	esac
done

# correct argument count after options
shift `expr $OPTIND - 1`

# test that the build space location was given
if [ $# -ne 1 ]
then
	echo "$USAGE"
	exit 1
fi

if [ "x$CODEMGR_WS" = "x" ]
then
	echo
	echo CODEMGR_WS must be set in the shell environment,
	echo as an absolute path, or specified via -w
	echo
	exit 1
fi

# The first arg is the root of the tree which will be the build space.
# Make sure it exists as a directory and is an absolute path.
#
[ -d $1 ] || mkdir $1
cd $1 || exit 1
TREE_ROOT=`pwd`; export TREE_ROOT

cd $CODEMGR_WS

if [ "$m_FLAG" = "n" ]
then
	MACH=`uname -p`
fi
case $MACH in
  sparc )  SKIPMACH="i386 | i86 | i86pc | ppc | prep";;
  i386 )   SKIPMACH="sparc| sun4| sun4c| sun4d| sun4e| sun4u| sun4m| sunmmu| srmmu| sfmmu| ppc| prep";;
  ppc )    SKIPMACH="sparc| sun4| sun4c| sun4d| sun4e| sun4u| sun4m| sunmmu| srmmu| sfmmu| i386| i86| i86pc";;
  * )	   SKIPMACH=_skip_nothing;;
esac
export SKIPMACH

echo ...creating or extending build space $1
echo and populating it with g-files from $CODEMGR_WS
echo
if [ "$r_FLAG" = "y" ]
then
	bld_tree .
else
	find_s_dots . | \
	  sed -e 's,SCCS/s.,,' | \
	  cpio -pdv $TREE_ROOT
fi

# conditionally create a Codemgr_wsdata directory in the build
# space with just enough data to form a pseudo workspace, tricking
# the OS-Net "ws" script.  Skip this step if protodefs already
# exists in the build space.
#
if [ -d Codemgr_wsdata/sunos ]
then
	if [ ! -f $TREE_ROOT/Codemgr_wsdata/sunos/protodefs ]
	then
		echo ...making $1 into pseudo workspace for the '"ws"' script
		mkdir -p $TREE_ROOT/Codemgr_wsdata/sunos 2> /dev/null
		echo 'PROTO1=$CODEMGR_WS/proto' > \
		  $TREE_ROOT/Codemgr_wsdata/sunos/protodefs
	fi
fi
echo
echo ...done.
echo
exit 0
