# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../libtool/parse-config
# Copyright (C) 2009 The OpenSDE Project
# Copyright (C) 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# Some packges have outdated libtool that e.g. does not handle lib64. The
# LIBTOOL-QUIRK flag allows automated updating of those files.

libtool_fixup()
{
	local pref= ltmain=

	if atstage native; then
		pref="$root"
	else
		pref="$base/build/$SDECFG_ID/TOOLCHAIN/tools.cross"
	fi

	for ltmain in "$pref/usr/share/libtool"/{,config/}ltmain.sh; do
		[ ! -s "$ltmain" ] || break
	done

	[ -f configure.in -o -f configure.ac ] && libtoolize -c -f

	find -name libtool -o -name ltmain.sh | while read fn; do
		case "$fn" in
			*libtool) cp -fv "$pref/usr/bin/libtool" "$fn" ;;
			*) cp -fv "$ltmain" "$fn" ;;
		esac
	done
}

# Many libtool and ltmain files contain an error which is hostile in
# our build environment:
# - on systems where the libdir is not */lib, libtool still searches
#   /lib and /usr/lib first
# - removal of temporary paths during library search does not work,
#   this concerns mainly dependency_libs in .la files where build system
#   paths and other things survive
#
# We try to correct any libtool and ltmain file now:
libtool_correct_files() {
	# do not do anything if we're outside builddir (like sysfiles is)
	[[ ${PWD//src.$pkg.$config.$id/} = $PWD ]] && return

	# find all libtool etal files, and sed them
        find -type f -name "libtool*" -o -name "ltmain*" -o -name "ltconfig*" -o -name "ltcf*" | while read f; do
		cp $f $f.ltcorrect
                sed -i \
		       -e 's,sys_lib_dlsearch_path_spec="/lib .*$lt_ld_extra.*,sys_lib_dlsearch_path_spec="$lt_ld_extra",' \
                       -e "s,'s% \$path % %g',\"s% \$path % %g\",g" -e "s,'s% -L\$path % %g',\"s% -L\$path % %g\",g" \
                       -e 's,echo "$\(lib_search_path\|deplibs\|dependency_libs\) ",echo " $\1 ",g' \
                        $f

		# previous sed does (per line):
		# 1. Dynamic library search path
		#   libtool.m4/ltmain.sh use the ld.so.conf list (lt_ld_extra) of path to search for dynamic libs,
		#   that variable is, however prefixed with "-L/lib -L/usr/lib" statically, the latter paths are
		#   removed by the sed expression
		# 2. Wrong quotes around sed expression
		#   A cleanup routine for temporary paths (grep for notinst_path) in libtool*/ltmain.* removes
		#   paths that should not appear after installation of the library. For this to work the "$path"
		#   variable must be evaluated (which is not possible with ' around it)
		# 3. Sed expects a leading whitespace
		#   To be sure that the cleaning process (mentioned in 2.) works also for the first argument,
		#   a " " is prepended before it is run through the sed expression (see 2.).

		diff $f.ltcorrect $f >/dev/null || echo_warning "Corrected $f"

		rm -f $f.ltcorrect
        done
}

libtool_final_check()
{
	local ld=${libdir##*/}
	# do not do anything if we're outside builddir (like sysfiles is)
	[[ ${PWD//src.$pkg.$config.$id/} = $PWD ]] && return
	if [ -f libtool -a -f $root/usr/bin/libtool ] &&
	   ! egrep -q "sys_lib_search_path_spec=.* /usr/$ld " libtool
	then
		echo_warning "Overwriting libtool - configure broken beyond believe."
		cp $root/usr/bin/libtool .
		echo -e "#!/bin/true" > missing
	fi
}

if ! atstage toolchain && hasflag LIBTOOL-QUIRK; then
        hook_add preconf 8 libtool_fixup
elif ! hasflag NO-LIBTOOL-FIX; then
	hook_add preconf 8 libtool_correct_files
fi

#hook_add premake 7 libtool_final_check