:
# 
# $Header: mergelib.sh.pp 20-oct-99.13:43:13 jboyce Exp $ mergelib.sh 
# 
#
#	Usage: mergelib [-ddirectory ] [-oarchive] library ...
#
#	This script will build a library by extracting object modules
#	from a list of libraries and adding them to a new library.
#	The script gets complicated since it must use a modest amount
#	of $WORKDIR space (approximately the size of the largest library
#	in the list) and has to go through some fancy steps to
#	guarantee that the list of modules does not exceed command
#	line size.
#
#	Working  directory

if [ -z "$TMPDIR" ] ; 
then

  TMPDIR=/var/tmp

fi
 
case "$WORKDIR" in
    "") WORKDIR="$TMPDIR"
        ;;
esac

#
#	'sed' argument

SEDARG="/__.SYMDEF/d"

#
#	archive flag

ARFLAG_R="r"
ARFLAG_T="t"
ARFLAG_X="x"

case $ORACLE_TRACE in
    T)	set -x ;;
esac
trap 'cd $WORKDIR; rm -rf libdir$$; exit' 1 2 3 15
CURDIR=`pwd`
cd $WORKDIR

FILE=file
SPLIT=split

AR=ar

#
#	Insure that there is no other directory of this name in $WORKDIR
#	Make the directory and cd into it.
#
rm -rf libdir$$
mkdir libdir$$
cd libdir$$
#
#	The default directory into which the merged archive is
#	created is the current directory.
#	The default liblist is null and the default archive name is
#	libn8.a
#
dirname=$CURDIR
liblist=""
archive="libn8.a"
if [ "$#" = "0" ]
then
	echo "Usage: $0 [-ddirectory ] [-oarchive] library ..."
	exit 1
fi
#
#	Process the argument list
#
for i in $*
do
	case $i in
	-d*)
		dirname=`expr $i : '^..\(.*\)'`
		;;
	-o*)
		archive=`expr $i : '^..\(.*\)'`
		;;
	*)
		liblist="$liblist $i"
		;;
	esac
done
#
#	For each of the libraries in the list, retrieve all of the
#	object modules in batches of 20 and install them in the
#	archive.  Remove the object modules from the $WORKDIR directory 
#	when done.
#
#
for i in $liblist
do
        echo Working on $i
	case $i in
	    /*)	;;
	    *)	i=$CURDIR/$i ;;
	esac
	if $FILE $i | grep archive > /dev/null
	then
		$AR $ARFLAG_T $i | sed $SEDARG | $SPLIT -20
		for j in x*
		do
			$AR $ARFLAG_X $i `cat $j`
			$AR $ARFLAG_R $dirname/$archive `cat $j`
			rm -f `cat $j`
		done
		rm -f x*

	elif $FILE $i | egrep "executable|relocatable" > /dev/null

	then
		$AR $ARFLAG_R $dirname/$archive $i
	else
		echo Cannot recognize $i, skipping
	fi
done
#
#	Return to the $WORKDIR directory and remove the working directory.
#
cd $WORKDIR
rm -rf libdir$$
