#!/bin/sh

LIBRARY_PATH_SUPPORT=no
COMPILER=$1
VERBOSE=$2
WORKDIR=$3

done=no
cd $3
unset LIBRARY_PATH

# (1) make the library
if "$COMPILER" -dynamiclib -o library_path_lib.dylib library_path_lib.c 2>/dev/null 1>&2; then
    # (2) do the test, without LIBRARY_PATH; should fail
    if "$COMPILER" -nostdinc -I. -o library_path_test library_path_test.c -lrary_path_lib 2>/dev/null 1>&2; then
	echo "Compile seems to work even with LIBRARY_PATH not set; assuming LIBRARY_PATH does not work"
	done=0
    else
	# (3) do the test, with LIBRARY_PATH set; should pass
	export LIBRARY_PATH=.
	if "$COMPILER" -nostdinc -I. -o library_path_test library_path_test.c -lrary_path_lib 2>/dev/null 1>&2; then
	    LIBRARY_PATH_SUPPORT=yes
	fi
    fi
    rm -f library_path_test.o library_path_test
else
    echo "Unable to compile library; please fix this issue"
    done=0
fi
rm -f library_path_lib.dylib library_path_lib.o

# if done above, assume it works and exit
[ "$done" != "no" ] && exit $done

if [ "$LIBRARY_PATH_SUPPORT" != "yes" ]; then
    [ "$VERBOSE" = "yes" ] && echo "LIBRARY_PATH support not detected"
    exit 0
else
    [ "$VERBOSE" = "yes" ] && echo "LIBRARY_PATH support detected"
    exit 1
fi
