#!/bin/sh
#
# Builds a shared library, incorporating the random portability work that we
# have to do.  Gets the basic information from Autoconf and knows how to find
# the appropriate system-specific map or version file and set library SONAMEs.
#
# This is not libtool.  If it appears to be in danger of evolving into
# libtool, please shoot it and start over, possibly by rewriting AFS in Ada.

# We take the following regular arguments: -d <srcdir>, -f <filename>, -l
# <library>, -M <major>, and -m <minor>.  If -f is given, it overrides -l and
# specifies the complete filename of the shared library to build.  We then
# expect a -- option indicating the end of our arguments and the rest of the
# arguments are passed along verbatim to the linker.

set -e

linker="@SHLIB_LINKER@"
suffix="@SHLIB_SUFFIX@"
sysname="@AFS_SYSNAME@"

library=
major=
minor=
unused=
srcdir=.
plain=
done=
while [ -z "$done" ] && [ $# -gt 0 ] ; do
    case "$1" in
    -d)
        shift
        srcdir="$1"
        shift
        ;;
    -f)
        shift
        filename="$1"
        shift
        ;;
    -l)
        shift
        library="$1"
        shift
        ;;
    -M)
        shift
        major="$1"
        shift
        ;;
    -m)
        shift
        minor="$1"
        shift
        ;;
    -i)
	ignore=yes
	shift
	;;
    -p)
	plain=yes
	shift
	;;
    --)
        shift
        done=yes
        ;;
    *)
        echo 'Usage: shlib-build (-p -f <file> | [-i] -l <lib> -M <major> -m <minor>) -- ...' >&2
        exit 1
        ;;
    esac
done
if [ -z "$library" ] && [ -z "$plain" ] || \
   [ -z "$filename" ] && [ "x$plain" != "x" ] ; then
    echo 'Usage: shlib-build (-p -f <file> | [-i] -l <lib> -M <major> -m <minor>) -- ...' >&2
    exit 1
fi

# Print out what we're doing while we do it for debugging.
export=
if [ -z "$filename" ] ; then
    if [ -z "$major" ] ; then
        filename="$library.$suffix"
        soname=
    else
        filename="$library.$suffix.$major.$minor"
        soname="$library.$suffix.$major"
    fi
fi
case $sysname in
rs_aix*)
    if [ -z "$plain" ] && [ -f "$srcdir/$library.map" ] ; then
	cat $srcdir/$library.map | \
	awk '/local:/ {inglobal=0};
	     inglobal { sub(/;/,""); print };
	     /global:/ { inglobal=1};' \
	> $library.exp
        export="-bE:$library.exp"
    fi
    echo "$linker $export -o $filename $*"
    $linker $export -o "$filename" "$@"
    ;;
sun*_5*)
    if [ -z "$plain" ] && [ -f "$srcdir/$library.map" ] ; then
        if [ -z "$ignore" ] ; then
            export="-Wl,-M$srcdir/$library.map"
        else
            # add "= EXTERN" to every symbol, so we don't get errors about
            # undefined symbols
            cat "$srcdir/$library.map" | \
                sed -e 's/\([a-zA-Z_]\);$/\1 = EXTERN;/' > "$library.real.map"
            export="-Wl,-M$library.real.map"
        fi
    fi
    if [ -z "$soname" ] ; then
        echo "$linker $export -o $filename $*"
        $linker $export -o "$filename" "$@"
    else
        echo "$linker $export -h $soname -o $filename $*"
        $linker $export -h "$soname" -o "$filename" "$@"
    fi
    ;;
*_linux*)
    if [ -z "$plain" ] && [ -f "$srcdir/$library.map" ] ; then
        export="-Wl,--version-script=$srcdir/$library.map"
    fi
    if [ -z "$soname" ] ; then
        echo "$linker $export -o $filename $*"
        $linker $export -o "$filename" "$@"
    else
        echo "$linker $export -Wl,-h,$soname -o $filename $*"
        $linker $export -Wl,-h,"$soname" -o "$filename" "$@"
    fi
    ;;
hp_ux*)
    if [ -z "$plain" ] && [ -f "$srcdir/$library.hp" ] ; then
        export="-c $srcdir/$library.hp"
    fi
    echo "$linker $export -o $filename $*"
    $linker $export -o "$filename" "$@"
    ;;
*darwin*)
    if [ -z "$plain" ] && [ -f "$srcdir/$library.map" ] ; then
	# For 10.4 and later, the Mac exports list is a list of symbols,
	# prefixed with an '_'
	cat $srcdir/$library.map | \
	awk '/local:/ {inglobal=0};
	     /^[\t ]+#/ {next};
	     inglobal { sub(/;/,""); sub(/[\t ]+/,"_"); print };
	     /global:/ { inglobal=1};' \
	> $library.exp
	export="-Wl,-exported_symbols_list,$library.exp"
	if [ ! -z "$ignore" ] ; then
	    export="$export,-flat_namespace,-undefined,suppress"
	fi
    fi
    echo "$linker $export -o $filename $*"
    $linker $export -o "$filename" "$@"
    ;;
*)
    echo "$linker -o $filename $*"
    $linker -o "$filename" "$@"
    ;;
esac
