#! /bin/sh
#
set -x
printenv
# This script builds a stand-alone DLL for the socket channel.
# It makes use of the configure scripts in the necessary components
#
# Determine the source and build directory
mypath=$0
mypath=`echo $mypath | sed -e 's/builddll//'`
mympich2=`(cd $mypath && cd ../../../../.. && pwd)`
mybuild=`(cd ../../../../../ && pwd)`
#
echo "Building channel dll for sock with $0"
# Get the prefix, libdir, and exec_prefix, which may be needed for
# setting installation directories.  These may be either passed on the
# command line or as environment variables
for arg do 
    option=`expr "x$arg" : 'x[^=]*=\(.*\)'`
    case $arg in 
        --prefix=*) prefix=$option
	;;
	--libdir=*) libdir=$option
	;;
	--exec-prefix=*) exec_prefix=$option
	;;
	--shlibtype=*) shlibtype=$option
	;;
	--localdefs=*) 
	if [ -s $option ] ; then 
	    . $option
	else
	    echo "File $option was not found"
	    exit 1
	fi
    esac
done
BUILD_DLLS=yes
export BUILD_DLLS
#
# We must have a valid ABIVERSION
if [ "X$ABIVERSION" = "X" ] ; then
    echo "No ABIVERSION available - required to build sock DLL"
    exit 1
fi
#
# Determine shared library type (only a few supported)
osname=`uname -s`
if [ -z "$shlibtype" ] ; then
    if [ -z "$CC" ] ; then
        shlibtype=gcc
    else
        shlibtype=$CC
    fi
    case $osname in
        *Darwin*|*darwin*)
         shlibtype="$shlibtype-osx"
        ;;
       *)
        ;;
    esac
fi

# Export the top_srcdir for MPICH2
master_top_srcdir=$mympich2
export master_top_srcdir
#
# Temp for experimentation
if [ -z "$CC_SHL" ] ; then
    case $shlibtype in
	gcc-osx|osx-gcc)
        CC_SHL="${CC} -fPIC"
        C_LINK_SHL='${CC} -dynamiclib -undefined suppress -single_module -flat_namespace'
        SHLIB_EXT=dylib
	;;
	*gcc*)
        CC_SHL="${CC} -fPIC"
        C_LINK_SHL='${CC} -shared'
        SHLIB_EXT=so
	;;
	*)
	echo "Unknown shared library type in builddll"
	exit 1
	;;
    esac
fi
export CC_SHL
export C_LINK_SHL
export SHLIB_EXT
#
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/ch3/include"
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/ch3/util/sock"
CPPFLAGS="$CPPFLAGS -I$mybuild/src/mpid/ch3/util/sock"

# The sock channel requires the sock utilities in mpid/common/sock
# This directory may not exist yet (no other configure may have 
# created it), so we build it if necessary
if [ ! -d $mybuild/src/mpid/common/sock ] ; then 
    mkdir $mybuild/src/mpid/common/sock 
fi
(cd $mybuild/src/mpid/common/sock && \
    $mympich2/src/mpid/common/sock/configure --enable-sharedlibs=$shlibtype \
    --prefix=$prefix --libdir=$libdir --exec-prefix=$exec_prefix )
if [ $? != 0 ] ; then
    echo "Aborting build because mpi/common/sock/configure failed"
    echo "See $mybuild/src/mpid/common/sock/config.log for more information"
    exit 1
fi

# Ensure that the sock channel includes are found before the dllchan includes
(\
CPPFLAGS="-I../../channels/sock/include -I$mympich2/src/mpid/ch3/channels/sock/include $CPPFLAGS";\
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/datatype" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/sock" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/sock/poll" ; \
CPPFLAGS="$CPPFLAGS -I../../../../common/sock" ; \
CPPFLAGS="$CPPFLAGS -I../../../../common/sock/poll" ; \
CPPFLAGS="$CPPFLAGS -I../include" ; \
CPPFLAGS="$CPPFLAGS -I../../../include" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/ch3/channels/sock/include" ; \
 $mypath/configure --enable-sharedlibs=$shlibtype \
    --prefix=$prefix --libdir=$libdir --exec-prefix=$exec_prefix )
#
# Make the channel library and update the channel dll
(cd src && make clean && rm -f libmpich2-ch3-sock.la && \
    make libmpich2-ch3-sock.la )
rc=$?
if [ $rc != 0 ] ; then
    echo "make step failed in ch3/channels/sock/src"
    exit $rc
fi
rm -f *.lo
ar x src/libmpich2-ch3-sock.la
ar cr libmpich2-ch3-sock.la *.lo
rm -f *.lo
#
# Build the socket support code and link it into the ch3-sock dll
(cd $mybuild/src/mpid/common/sock/poll && make clean && \
    rm -f libmpich2sock.la && make libmpich2sock.la)
rc=$?
if [ $rc != 0 ] ; then
    echo "make step failed in common/sock/poll"
    exit $rc
fi
mkdir .tmp
cd .tmp
rm -f *.lo
ar x $mybuild/src/mpid/common/sock/poll/libmpich2sock.la
ar cr ../libmpich2-ch3-sock.la *.lo
cd ..
rm -rf .tmp
#
# Build the ch3 socket support code and link it into the ch3-sock dll
if [ ! -d ../../util ] ; then
    mkdir ../../util
fi
if [ ! -d ../../util/sock ] ; then
    mkdir ../../util/sock
fi

(\
CPPFLAGS="-I../../channels/sock/include -I$mympich2/src/mpid/ch3/channels/sock/include $CPPFLAGS";\
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/sock" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/sock/poll" ; \
CPPFLAGS="$CPPFLAGS -I../../../common/sock" ; \
CPPFLAGS="$CPPFLAGS -I../../../common/sock/poll" ; \
CPPFLAGS="$CPPFLAGS -I../../channels/sock/include" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/ch3/channels/sock/include" ; \
CPPFLAGS="$CPPFLAGS -I../../include" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/ch3/include" ; \
CPPFLAGS="$CPPFLAGS -I$mympich2/src/mpid/common/datatype" ; \
cd ../../util/sock && \
    $mympich2/src/mpid/ch3/util/sock/configure --enable-sharedlibs=$shlibtype \
    --prefix=$prefix --libdir=$libdir --exec-prefix=$exec_prefix )

(cd ../../util/sock && make clean && rm -f libmpich-sockutil.la && \
    make libmpich-sockutil.la)
rc=$?
if [ $rc != 0 ] ; then
    echo "make step failed in util/sock"
    exit $rc
fi
mkdir .tmp
cd .tmp
rm -f *.lo
ar x ../../../util/sock/libmpich-sockutil.la
ar cr ../libmpich2-ch3-sock.la *.lo
cd ..
rm -rf .tmp
#
# Now, create the dynamically-loadable library
# Some of the routines now depend on routines in libmpl.
rm -f libmpich2-ch3-sock*.dylib libmpich2-ch3-sock*.so
$mybuild/src/util/createshlib -echo --mode=link -version-info "1:1" \
    -clink="$C_LINK_SHL" -cc=gcc -libtype=$shlibtype \
    -o libmpich2-ch3-sock.la -rpath `pwd` -L../../../../../lib -lmpl 
rc=$?
if [ $rc != 0 ] ; then 
    echo "Createshlib step failed for sock channel"
    exit $rc
fi
# Note that when this directory is installed, it needs to be rebuilt with 
# a different rpath.
