#!/bin/sh

# mklib for MacOSX-X11
# Contributor: Daniel Bunzli, daniel.bunzli at epfl dot ch
# 1/24/2004
#
# Usage:  mklib <archflags> <libname> <major> <minor> <file.o ... >
#
# <archflags> are arcitecture specific build flags
# <libname> is name of output library (LIBRARY)
# <major> is major version number (MAJOR)
# <minor> is minor version number (MINOR)
# <file.o ... > remaining arguments are object files (OBJECTS)


ARCHFLAGS=$1
shift 1

LIBRARY=$1
shift 1

MAJOR=$1
shift 1

MINOR=$1
shift 1

VERSION=$MAJOR.$MINOR

OBJECTS=$*


#
# macosx-X11 notes: we don't include -ljpeg for the librmi build because
# libjpeg doesn't ship with macosx (!!).
#

if [[ ${LIBRARY} = "librm" ]]; then
    DEPLIBS="-lGL -lGLU -L/usr/X11R6/lib -lX11 -lXmu"
elif [[ ${LIBRARY} = "librmi" ]]; then
    DEPLIBS="../lib/librm.dyld -lGL -lGLU -L/usr/X11R6/lib -lX11 -lXmu"
else
    DEPLIBS="../lib/librm.dyld -lGL -lGLU -L/usr/X11R6/lib -lX11 -lXmu"
fi

#--platform------------------------------------------------------------

# build library objects
echo "Building shared library $LIBRARY.$VERSION.dyld and the archive library $LIBRARY.a"
\rm -f ${LIBRARY}.a ${LIBRARY}.${VERSION}.dyld
ar qv ${LIBRARY}.a ${OBJECTS}
ranlib ${LIBRARY}.a

# macosx specific build
gcc -dynamiclib -current_version ${VERSION} \
    -o ${LIBRARY}.${VERSION}.dyld ${OBJECTS} ${DEPLIBS}
if [[ ${ARCHFLAGS##*-} != "debug" ]]; then
    strip -x ${LIBRARY}.${VERSION}.dyld
    echo "library has been stripped (non-debug build)."
fi 

# code tree
cp ${LIBRARY}.a ${LIBRARY}.${VERSION}.dyld ../lib
\rm -f ../lib/${LIBRARY}.dyld
ln -s ${LIBRARY}.${VERSION}.dyld ${LIBRARY}.dyld


# local install
if [[ -n ${RM_INSTALL} ]]; then
    echo "Installing ${LIBRARY} librairies locally in ${RM_INSTALL}/lib..."
    \cp -f ${LIBRARY}.a ${LIBRARY}.${VERSION}.dyld ${RM_INSTALL}/lib
    \rm -f ${RM_INSTALL}/lib/${LIBRARY}.dyld
    ln -s \
    ${RM_INSTALL}/lib/${LIBRARY}.${VERSION}.dyld \
    ${RM_INSTALL}/lib/${LIBRARY}.dyld
    echo "Local ${LIBRARY} librairies installed."
fi

#EOF
