#!/bin/sh
set -ex

PREFIX="`which swig | sed 's,/swig,,'`"
if test ! -d "$PREFIX"; then
  echo "Failed to find swig prefix" >&2
  exit 1
fi

LIBTOOL="`which glibtool`"
test -z "$LIBTOOL" && LIBTOOL="`which libtool`"

LIBTOOL="$LIBTOOL --tag=disable-static"

CFLAGS="`python -c 'from distutils import sysconfig
print \"%s -I%s\" % tuple(sysconfig.get_config_vars(\"CFLAGS\",
 \"INCLUDEPY\"))'`"
 
case `uname` in
CYGWIN*)
LIBS="`python -c 'from distutils import sysconfig; import os, re
libpl, ldlib = sysconfig.get_config_vars(\"LIBPL\", \"LDLIBRARY\")
lopt = re.sub(r\"^lib(.*)\\.dll\\.a$\", r"-l\\1", ldlib)
print "-L%s %s" % (libpl, lopt)
'`"
;;
Darwin)
# This may well crash-and-burn horribly with a non-system python,
# but I don't have access to an OS X box like that.
LIBS="-Wl,-framework,Python"
;;
*)
echo "This script does not know about your system" >&2
exit 1
;;
esac

TEMPDIR=tmp-libswigpy-build
mkdir "$TEMPDIR"
cd "$TEMPDIR"
swig -runtime -python swigrun.i
$LIBTOOL --mode=compile gcc -O3 -Wall swigrun_wrap.c -o swigpy.o -c $CFLAGS
$LIBTOOL --mode=link gcc -no-undefined -rpath "$PREFIX/lib" -o libswigpy.la \
swigpy.lo $LIBS
$LIBTOOL --mode=install cp libswigpy.la "$PREFIX/lib"
cd ..
rm -r "$TEMPDIR"



