#!/bin/sh

INSTALL_DIR=`pwd`
SOFTWARE=swig
VERSION=1.3.25

function cleanupandexit {
  echo "Installation failed.  Look above for errors."
  cd $INSTALL_DIR
  rm -rf ${SOFTWARE}-${VERSION}
  exit 1
}

tar xzf ${SOFTWARE}-${VERSION}.tar.gz
cd $INSTALL_DIR/${SOFTWARE}-${VERSION}

./configure --with-perl=/usr/bin/perl --with-python=/usr/bin/python --with-java=/opt/java/sdk/current/bin/java --without-php4 || cleanupandexit

make || cleanupandexit
make install || cleanupandexit

cat > build-libswigpy.sh << "EOF"
#!/bin/sh
set -ex
PREFIX=/usr/local
LIBTOOL=libtool
CFLAGS="`python -c 'from distutils import sysconfig
print \"%s -I%s\" % tuple(sysconfig.get_config_vars(\"CFLAGS\",
 \"INCLUDEPY\"))'`"
LIBS="`python -c 'from distutils import sysconfig; import os
print apply(os.path.join, sysconfig.get_config_vars(\"LIBPL\",
\"LDLIBRARY\"))
'`"
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
EOF

sh ./build-libswigpy.sh

echo ""
echo "SWIG Installation Complete"

