#!/bin/sh
set -e
#
# Since samba links shared libs against internal static libs, we
# build libsmbclient separately for each architecture, install
# into temporary destinations and finally combine the .dylibs
# with lipo
#
if [ $# != 2 ] ; then
    echo "Usage: buildsmbclient <PREFIX> <DSTDIR>" >&2
    exit 1
fi
PFX="$1"
WDIR=`pwd`
TDIR="$WDIR/libsmbclient.$$"
cd "$2"
IDIR="`pwd`"
cd "$WDIR"

cleanup() {
    cd "$WDIR"
    rm -rf "$TDIR"
}
trap cleanup EXIT
mkdir -p "$TDIR"
cd "$TDIR"
TGZ=samba-latest.tar.gz
DLURL="ftp://ftp.samba.org/pub/samba/$TGZ"

test -f $TGZ || wget "$DLURL"
SRC=`tar tzf $TGZ|tail -1|cut -d/ -f1`
tar xzf $TGZ
cd $SRC/source3
CFLAGS="-arch i386" LDFLAGS="-arch i386" ./configure \
    --prefix=$PFX \
    --with-configdir=/etc \
    --disable-swat \
    --without-wbclient \
    --without-libnetapi \
    --with-static-libs=libtalloc,libtdb \
    --with-shared-libs=libsmbclient
make && mv bin bin-i386
make clean
sed -e s/i386/ppc/g < config.status | sh
make
mkdir -p "$IDIR"
for lib in bin/libsmbclient.dylib* ; do
    bn=`basename $lib`
    if [ -L $lib ] ; then
        cp $lib "$IDIR/$bn"
    else
        lipo -create bin-i386/$bn $lib -output "$IDIR/$bn"
    fi
done
