#!/bin/sh

cleanup ()
{
    rm -f "$tempfile"
}

print-error ()
{
	if [ -n "$1" ]; then
		printf "`basename $0`: error: $1\n"
	fi
	printf "Usage: `basename $0` library-delivery-directory\n"
	exit 1;
}

# Check arguments
if [ $# != 1 ]; then
	print-error
fi

if [ ! -d "$1" ]; then
	print-error "argument is not a directory"
fi

# We assume that the most current library has the most static data member
# symbols
tempfile=/tmp/`basename $0`.$$.`id -u`
rm -f "$tempfile"
touch "$tempfile"
liblist=`ls -1 $1/lib*static*.dylib 2>/dev/null | grep -v libstatic`
for i in $liblist; do
	symbolcount=`/usr/bin/nm "$i" | grep -v ' U ' | grep -c '__Q.*\.'`
	printf "$symbolcount $i\n" >> "$tempfile"
done
largestlib=`cat "$tempfile" | sort -n | tail -n1`
rm -f "$tempfile"

largestlib=`basename "$largestlib"`
if [ -f "$1/$largestlib" ]; then
	linkname=`printf "$largestlib" | sed 's/lib.*static/libstatic/'`
	printf "`basename $0`: creating $linkname softlink to $1/$largestlib\n"
	ln -sf "$largestlib" "$1/$linkname"
fi

cleanup

exit 0
