# $NetBSD: libtool-fix-la,v 1.3 2004/03/13 03:41:13 uebayasi Exp $
#
# For *.la files, in the "relink_command" line, we make the following
# replacements:
#
#       "libfoo.la" -> "-L./.libs libfoo.la"
#	"dirpath/libfoo.la" -> "-Ldirpath/.libs libfoo.la"
#
# This allows the libraries to be found within ${WRKSRC} during relinking.
# We rely on the proper rpath settings to be set by libtool.
#
# For the *.lai files, in the "dependency_libs" line, we remove redundant
# -Ldir and -llib options.  We also make sure that we don't ever reference
# other .la files, only referring to other libraries via the usual
# "-L/path -llib" and making sure that /path is always somewhere under the
# default view.  This makes wildcard dependencies work for "overwrite"
# packages by letting libtool find libraries in the default view.

labase=`@BASENAME@ $lafile .la`
ladir=`@DIRNAME@ $lafile`
latimestamp=${ladir}/.${labase}.la.timestamp
laifile=${ladir}/.libs/${labase}.lai
if $test -f $lafile; then
	. ${ladir}/${labase}.la

	deps=
	for i in ${dependency_libs}; do
		case "$i" in
		-R*)
		# Skip all -R* options... rpath info shouldn't go into the
		# dependency_libs line.
			;;
		# Catch -L<dir> and remove redundant ones.
		-L*)
			case "$deps" in
			*"$i "*) ;;
			*"$i") ;;
			*) deps="${deps} $i" ;;
			esac
			;;
		# Convert "dirpath/libfoo.la" into "-Ldirpath/.libs libfoo.la"
		# if it's an uninstalled libtool archive.  This allows
		# proper relinking by libtool during the install stage
		# by allowing it to find the uninstalled shared library
		# in the .libs directory.
		#
		*.la)
			fixla=0
			ltlibsdir=`@DIRNAME@ $i`/.libs
			case "$ltlibsdir" in
			/*)
				# fix if an absolute path in ${WRKSRC}
				case "$ltlibsdir" in
				@WRKSRC@/*)	fixla=1 ;;
				esac
				;;
			*)
				# fix all relative paths
				fixla=1
				;;
			esac
			if $test $fixla -gt 0; then
				case "$deps" in
				*"-L$ltlibsdir "*) ;;
				*"-L$ltlibsdir") ;;
				*) deps="$deps -L$ltlibsdir" ;;
				esac
			fi
			deps="${deps} $i"
			;;
		# Everything else Just Belongs in the dependency_libs line.
		*)
			deps="${deps} $i"
			;;
		esac
	done

	relink=
	for i in ${relink_command}; do
		case "$i" in
		# -I* and -D* are useless for linking.
		-I*|-D*)
			;;
		# Catch -L<dir> and remove redundant ones.
		-L*)
			case "$relink" in
			*"$i "*) ;;
			*"$i") ;;
			*) relink="${relink} $i" ;;
			esac
			;;
		# This is the .la file we're relinking... don't touch it!
		$lafile)
			relink="${relink} $i"
			;;
		# Convert "dirpath/libfoo.la" into "-Ldirpath/.libs libfoo.la"
		# if it's an uninstalled libtool archive.  This allows
		# proper relinking by libtool during the install stage
		# by allowing it to find the uninstalled shared library
		# in the .libs directory.
		#
		*.la)
			fixla=0
			ltlibsdir=`@DIRNAME@ $i`/.libs
			case "$ltlibsdir" in
			/*)
				# fix if an absolute path in ${WRKSRC}
				case "$ltlibsdir" in
				@WRKSRC@/*)	fixla=1 ;;
				esac
				;;
			*)
				# fix all relative paths
				fixla=1
				;;
			esac
			if $test $fixla -gt 0; then
				case "$relink" in
				*"-L$ltlibsdir "*) ;;
				*"-L$ltlibsdir") ;;
				*) relink="$relink -L$ltlibsdir" ;;
				esac
			fi
			relink="${relink} $i"
			;;
		# Everything else Just Belongs in the relink_command line.
		*)
			relink="${relink} $i"
			;;
		esac
	done
	if $test -z "$deps" && $test -z "$relink" || \
	   @EGREP@ "^#.*modified by buildlink3" $lafile >/dev/null 2>&1; then
		:
	else
		# Save the orignal .la file produced by libtool as
		# .la.blsav, and create the new one with a signature.
		#
		@MV@ -f $lafile ${lafile}.blsav
		if $test -n "$deps"; then
			(
			$cat ${lafile}.blsav | @SED@ -e '/^dependency_libs=/,$d'
			$echo "dependency_libs='$deps'"
			$cat ${lafile}.blsav | @SED@ -e '1,/^dependency_libs=/d'
			) > ${lafile}.tmp
		else
			$cat ${lafile}.blsav > ${lafile}.tmp
		fi
		if $test -n "$relink"; then
			(
			$cat ${lafile}.tmp | @SED@ -e '/^relink_command=/,$d'
			$echo "relink_command='$relink'"
			$cat ${lafile}.tmp | @SED@ -e '1,/^relink_command=/d'
			) > $lafile
		else
			$cat ${lafile}.tmp > $lafile
		fi
		$echo >> $lafile
		$echo "# This file has been modified by buildlink3." >> $lafile
		@RM@ ${lafile}.tmp
		$echo "==> Fixed $lafile" >> $wrapperlog
	fi
	#
	# Fix up any .lai files (libtool archives that will be installed).
	#
	if $test -z "$laifile" || $test ! -f "$laifile" || \
	   @EGREP@ "^#.*modified by buildlink3" $laifile >/dev/null 2>&1; then
		:
	else
		@SED@ @_BLNK_WRAP_LT_UNTRANSFORM_SED@ \
			$laifile > ${laifile}.tmp
		. ${laifile}.tmp
		L=; l=; lexist=; prev=
		for i in ${dependency_libs}; do
			case "$i" in
			#
			# Don't allowed repeated arguments, so "-lm -lm"
			# becomes just "-lm".
			#
			$prev)
				;;
			# Skip all -R* options... rpath info shouldn't go
			# into the dependency_libs line.
			#
			-R*)
				;;
			# Skip directories that should never appear in
			# the -L<dir> flags.  Also modify directories in
			# ${DEPOTBASE} to point to somewhere in the
			# default view ${LOCALBASE}, since we want "overwrite"
			# packages to think the libraries really do just
			# exist through the default view.
			#
			-L*)
				case "$i" in
				-L${BUILDLINK_DIR}/*)
					;;
				-L@WRKSRC@|-L@WRKSRC@/*)
					;;
				-L@DEPOTBASE@/*)
					i=`$echo "X$i" | $Xsed -e "s,-L@DEPOTBASE@/[^/]*/,-L@LOCALBASE@/,"`
					case "$L" in
					*"$i "*) ;;
					*"$i") ;;
					*) L="$L $i" ;;
					esac
					;;
				*)
					case "$L" in
					*"$i "*) ;;
					*"$i") ;;
					*) L="$L $i" ;;
					esac
					;;
				esac
				;;
			# Libraries really do exist, so we want to keep
			# any -L<dir> flags we've seen in the generated
			# dependency_libs line.
			#
			-l*)
				lexist=1
				l="$l $i"
				;;
			# Libtool archives should be changed from
			# "/path/libfoo.la" to "-L/path -lfoo", where /path
			# is appropriately modified so that the depot
			# directory is changed to the views directory.  This
			# allows the .la files to be used by either "overwrite"
			# or "pkgviews" packages and works correctly with
			# wildcard dependencies.
			#
			*.la)
				dirbase=`@DIRNAME@ $i`
				dir=`$echo "X$dirbase" | $Xsed -e "s,^@DEPOTBASE@/[^/]*/,@LOCALBASE@/,"`
				case "$L" in
				*"-L$dir "*) ;;
				*"-L$dir") ;;
				*) L="$L -L$dir" ;;
				esac
				libbase=`@BASENAME@ $i .la`
				lib=`$echo "X$libbase" | $Xsed -e "s/^lib//"`
				lexist=1
				l="$l -l$lib"
				;;
			# Everything else Just Belongs.
			*)
				l="$l $i"
				;;
			esac
			prev="$i"
		done
		if $test -z "$lexist"; then
			L=
		fi
		deps="$L$l"
		deps=`$echo "X$deps" | $Xsed -e "s/^[ 	]*//"`
		@MV@ -f $laifile ${laifile}.blsav
		(
			$cat ${laifile}.tmp | @SED@ -e '/^dependency_libs=/,$d'
			$echo "dependency_libs='$deps'"
			$cat ${laifile}.tmp | @SED@ -e '1,/^dependency_libs=/d'
			$echo
			$echo "# This file has been modified by buildlink3."
		) > ${laifile}
		@RM@ -f ${laifile}.tmp
		$echo "==> Fixed $laifile" >> $wrapperlog
		if $test "$libdir" != "${BUILDLINK_DIR}/.tmp/inst" && \
		   @EGREP@ "${BUILDLINK_DIR}" $laifile >/dev/null 2>&1; then
			$echo "==> [buildlink3] Error: buildlink3 leakage into $laifile" >> $wrapperlog
			$echo "==> [buildlink3] Error: buildlink3 leakage into $laifile" 1>&2
			wrapper_result=1
		fi
	fi
fi
