#!/bin/sh
#*************************************************************************
#
#   $RCSfile: macosx-dylib-link-list,v $
#
#   $Revision: 1.2 $
#
#   last change: $Author: kz $ $Date: 2006/02/03 17:34:07 $
#
#   The Contents of this file are made available subject to
#   the terms of GNU Lesser General Public License Version 2.1.
#
#
#     GNU Lesser General Public License Version 2.1
#     =============================================
#     Copyright 2005 by Sun Microsystems, Inc.
#     901 San Antonio Road, Palo Alto, CA 94303, USA
#
#     This library is free software; you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public
#     License version 2.1, as published by the Free Software Foundation.
#
#     This library is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#     MA  02111-1307  USA
#
#*************************************************************************


# Code
# ----
finalLibraryList=""

# AddLibraryAndDependencies()
#
#   Adds a library to the global library list (if its not
# already on the list) and then grabs each dependent library
# and calls itself with that library.
# ---------------------------------------------------------------
AddLibraryAndDependencies ()
{
	# Arguments should be:
	#  1 - Global library directory
	#  2 - project library directory
	#  3 - library to scan and add

	local	globalLibDir;
	local	projLibDir;
	local	libraryName;
	local	actualLibDir;
	local	dependentLibs;
	local	depLibName;
	local	libloc1;
	local	libloc2;

	# Initial Assignments
	globalLibDir="$1"
	projLibDir="$2"
	libraryName="$3"

	actualLibDir="error..."
	# Find the directory library libraryName is actually in
	if [ -f "$globalLibDir/$libraryName" ]; then
		actualLibDir="$globalLibDir"
	fi
	if [ -f "$projLibDir/$libraryName" ]; then
		actualLibDir="$projLibDir"
	fi

	# Add library libraryName to the global library list if its not already there
	if ! printf "$finalLibraryList" | grep -q "$libraryName" ; then
          if [ -f "$actualLibDir/$libraryName" ]; then
               finalLibraryList="$finalLibraryList $libraryName"
          fi
	fi
	
	# Now get a list of library libraryName's dependecies and add each one of them and their dependencies
	if [ -f "$actualLibDir/$libraryName" ]; then
		dependentLibs=`otool -L "$actualLibDir/$libraryName" | grep -e '@' | grep -v "$libraryName" | sed 's/@executable_path\///' | sed 's/(compatibility version [0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*, current version [0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*)//g' | sed 's/ //g' | xargs`
		for depLibName in $dependentLibs; do
			# If the dependent is not in the global library list, then add it
			if ! printf "$finalLibraryList" | grep -q "$depLibName" ; then
				AddLibraryAndDependencies "$globalLibDir" "$projLibDir" "$depLibName"
			fi
		done
	fi
}


# Main program
#
#   Validates arguments, and calls the recursive library add
# function for each library on the command-line.
# ---------------------------------------------------------------
# If we're not given the requisite # of arguments, yell at the user
if [ $# = 0 ]; then
	printf "macosx-dylib-link-list\n" >&2
	printf " Error, incorrect number of arguments\n" >&2
	printf "  Usage:    macosx-dylib-link-list prjName solverLibDir prjLibDir [lib1] ... [libN]\n" >&2
	exit 1
fi

# Grab the project name
projectName="$1"
shift
# Grab the directory where the libraries are stored
solverLibDir="$1"
shift
# Grab the second directory where the libraries could stored
projLocalLibDir="$1"
shift

# Go through each library listed on the command line, and add
# it and the libraries it depends on to the global library list
while [ $# != 0 ]; do
	# Grab the library name and strip off the "-l"
	libraryName=`echo "$1" | sed 's/-l//g'`
	shift
	libraryName="lib$libraryName.dylib"
	
	AddLibraryAndDependencies "$solverLibDir" "$projLocalLibDir" "$libraryName"
done

# Finally print out the formatted library list suitable for linker input
for eachLib in $finalLibraryList; do
	# Since a library we are asked to link with might not be delivered
	# to solver/ yet, we have to find where that library is and specify
	# _that_ location rather than just solver
	if [ -f "$solverLibDir/$eachLib" ]; then
		printf " -dylib_file @executable_path/$eachLib:$solverLibDir/$eachLib "
	elif [ -f "$projLocalLibDir/$eachLib" ]; then
		printf " -dylib_file @executable_path/$eachLib:$projLocalLibDir/$eachLib "
	fi
done
