#! /bin/sh

# This script helps compiling a main program that will use dynamic
# loading.  It's needed on AIX since the linker has to know the export
# list of the main program when compiling the shared libs that the
# main program will load (with dlopen ()).  It creates a file
# "lib.exp" (or whatever you tell it to name it).  This file contains
# the export list for the main program (i.e. ".").
#
# For this stage we automatically add "-bE:lib.exp".  For the shared
# libs you'll have to add "-bI:lib.exp" (maybe with a path).
#
# This is only tested for AIX 4.2.

#set -x

usage="Usage: `basename $0` [-o <expfile>] ld <args for ld ...>"

if [ ${1-"-h"} = "-h" ]; then
	echo 1>&2 "$usage"
	exit 1
fi

# Check for name of export file
case $1 in
    -o)
	mainexp=${2?$usage}
	shift 2
	;;
    -o*)
	mainexp=$1
	mainexp=${mainexp#-o}
	shift
	;;
    *)
	mainexp="lib.exp"
	;;
esac

# Check for object or archive files
ofiles=""
for arg; do
    case $arg in *.o) ofiles="$ofiles $arg";; esac
    case $arg in *.a) ofiles="$ofiles $arg";; esac
done

# Call nm so that it prints only global sympols (unsorted, w/o header)
nm=nm
nmopts="-p -g -h"

# Replace ".funname T adr1 adr2" with "funname"
regex1='s,^\.\([a-zA-Z][a-zA-Z0-9_]*\)  *T .*$,\1,p'
# Replace "varname B adr1 adr2" with "varname bss"
regex2='s,^\([a-zA-Z][a-zA-Z0-9_]*\)  *B .*$,\1 bss,p'

echo "#! ." > $mainexp
$nm $nmopts $ofiles | sed -n -e "$regex1" -e "$regex2" | \
    sort | uniq >> $mainexp

exit $?
