#!/bin/sh
# $Id: ld-config.sh,v 1.3 2006/06/05 22:41:31 jimb Exp $
# Author: Jim Brooks  http://www.jimbrooks.org
# Desc:   Output ld flags that are platform-specific.
# Args:   -syslibs
#         Echo linker args for system libs.
#-------------------------------------------------------------------------------

# Detect which OS from environment variable.
( echo $OSTYPE | grep -q -i linux  ) && LINUX=1
( echo $OSTYPE | grep -q -i darwin ) && DARWIN=1
( echo $OSTYPE | grep -q -i cygwin ) && CYGWIN=1

#===============================================================================
# Parse args.
#===============================================================================

while [ $# -gt 0 ]
do
    # -lmcheck on Linux Nvidia proprietary driver causes a core dump on program exit,
	# so enable it only if DEBUG=2.
    if [ "$1" = "DEBUG2" ]; then
        # Add -lmcheck only if this is a glibc system.
        shift
        if ( grep -q __GLIBC__ /usr/include/features.h 2>/dev/null );
        then
            echo "-lmcheck "
        fi
    elif [ "$1" = "-syslibs" ]; then
        DETECT_SYSLIBS=1
    else
        # None of the above.
        :
    fi

    shift
done

#===============================================================================
# Determine system libs.
#===============================================================================

if [ $DETECT_SYSLIBS ]; then
    if [ $DARWIN ]; then
        echo '-L"/System/Library/Frameworks/OpenGL.framework/Libraries" -lGL -lGLU -framework OpenGL -framework GLUT -framework foundation'
    elif [ $CYGWIN ]; then
        # "-lglut32 -lglu32 -lopengl32" links with the hw-accelerated libs.
        # The Linux linker args, if misused on cygwin, cause linking
        # with Mesa which, on cygwin, does software-rendering.
        echo "-lglut32 -lglu32 -lopengl32"
    else # GNU/Linux
        # x86-64 /lib64 should precede.
        [ -d /usr/X11/lib64 ]   && echo -n "-L/usr/X11/lib64 "
        [ -d /usr/X11R6/lib64 ] && echo -n "-L/usr/X11R6/lib64 "
        [ -d /usr/lib64/X11 ]   && echo -n "-L/usr/lib64/X11 "

        [ -d /usr/X11/lib ]     && echo -n "-L/usr/X11/lib "
        [ -d /usr/X11R6/lib ]   && echo -n "-L/usr/X11R6/lib "
        [ -d /usr/lib/X11 ]     && echo -n "-L/usr/lib/X11 "

        [ -d /usr/local/lib ]   && echo -n "-L/usr/local/lib "

        echo "-lglut -lGL -lGLU -lX11 -lXi"
    fi
fi

exit 0
