#!/bin/bash

OS=`uname`
ARCH=`uname -m`

VERSION="dev"

# standardise the OS and ARCH names
if [ "$OS" = "Darwin" ] ; then
	OS="apple"
elif [ "$OS" = "Linux" ] ; then
	OS="linux"
elif [ "$OS" = "SunOS" ] ; then
	OS="sun"
else
	echo "OS not supported"
	exit 1
fi

if [ "$ARCH" = "x86" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "i586" ] \
	|| [ "$ARCH" = "i486" ] || [ "$ARCH" = "i386" ] ; then
	ARCH="x86"
elif [ "$ARCH" = "Power Macintosh" ] ; then
	ARCH="ppc"
elif [ "$ARCH" = "amd64" ] || [ "$ARCH" = "x86_64" ] ; then
	ARCH="x86_64"
elif [ "$ARCH" = "sun4u" ] ; then
	ARCH="sparc"
else
	echo "ARCH not supported"
	exit 1
fi

if [ "$OS" = "apple" ] ; then
	CC="gcc -Wall"
	CPPFLAGS="$CPPFLAGS -I/System/Library/Frameworks/JavaVM.framework/Home/include"
    CFLAGS="-fPIC -fno-common $CFLAGS"
	LD="gcc -dynamiclib"
	LDFLAGS="$LDFLAGS -framework JavaVM"
	LIBPREPEND="lib"
	LIBAPPEND="-apple-"${ARCH}".jnilib"
	FC="g95"
	FFLAGS="$CFLAGS"
	LAPACK_INCLUDES="-I/System/Library/Frameworks/vecLib.framework/Headers"
	# unfortunately OS X cannot build default multi-module libraries with fortran files
	FORTRAN_LIBS="-lg95 -Wl,-single_module"
	BLAS_LIBS="-framework veclib"
	LAPACK_LIBS=""
elif [ "$OS" = "linux" ] ; then
	CC="gcc -Wall"
	CPPFLAGS="$CPPFLAGS -I$JAVA_HOME/include -I$JAVA_HOME/include/linux"
	CFLAGS="-fPIC $CFLAGS"
	LD="gcc -shared"
	LDFLAGS="$LDFLAGS -L$JAVA_HOME/lib"
	LIBPREPEND="lib"
	LIBAPPEND="-linux-"${ARCH}".so"
	FC="gfortran"
	FFLAGS="$CFLAGS"
	LAPACK_INCLUDES=""
	FORTRAN_LIBS="-lgfortran"
	BLAS_LIBS="-lblas"
	LAPACK_LIBS="-llapack"
else
	echo "OS not supported"
	exit 1
fi

echo "Creating config for $OS $ARCH"

echo "CC=$CC" > Makefile.incl
echo "CPPFLAGS=$CPPFLAGS" >> Makefile.incl
echo "CFLAGS=$CFLAGS" >> Makefile.incl
echo "LD=$LD" >> Makefile.incl
echo "LDFLAGS=$LDFLAGS" >> Makefile.incl
echo "LIBPREPEND=$LIBPREPEND" >> Makefile.incl
echo "LIBAPPEND=$LIBAPPEND" >> Makefile.incl
echo "LAPACK_INCLUDES=$LAPACK_INCLUDES" >> Makefile.incl
echo "BLAS_LIBS=$BLAS_LIBS" >> Makefile.incl
echo "FORTRAN_LIBS=$FORTRAN_LIBS" >> Makefile.incl
echo "LAPACK_LIBS=$LAPACK_LIBS" >> Makefile.incl
echo "FC=$FC" >> Makefile.incl
echo "FFLAGS=$FFLAGS" >> Makefile.incl

echo "If you don't have a machine optimised BLAS/LAPACK, please see the comments in this file to compile your own, or install the atlas-base-dev package"

# check if g95 is installed
if [ ! -x "`which $FC 2>/dev/null`" ] ; then
	echo "Fortran compiler required. Please install gfortran (sometimes called g95)."
fi

# if we are on a dev machine with a bin directory, use it, otherwise point to a jar
if [ -d "../bin" ] ; then
	echo "JLAPACK_JNI_CP=../bin" >> Makefile.incl
else 
	echo "JLAPACK_JNI_CP=../netlib-java-$VERSION.jar:../lib/f2j/arpack-combined.jar" >> Makefile.incl
fi

# wget http://netlib.org/lapack/lapack-3.1.1.tgz
# tar xfz lapack-3.1.1.tgz
# cd lapack-3.1.1
# cp make.inc.example make.inc
# make FORTRAN="gfortran -fPIC" LOADER="gfortran -fPIC" OPTS="-fPIC -funroll-all-loops -O3" NOOPTS="-fPIC" lib
# cd ..
# 
# wget http://belnet.dl.sourceforge.net/sourceforge/math-atlas/atlas3.7.30.tar.bz2
# tar xfj atlas3.7.30.tar.bz2
# cd ATLAS
# mkdir mybuild
# cd mybuild
# ../configure -Fa alg -fPIC --with-netlib-lapack=$PWD/../../lapack-3.1.1/lapack_LINUX.a
# make
# cd lib
# make ptshared
# cd ..
# sudo make install
# sudo install -m 755 lib/*.so /usr/local/atlas/lib/
# 
# sudo ln -sf /usr/local/atlas/lib/libcblas.a /etc/alternatives/libblas-3.a
# sudo ln -sf /usr/local/atlas/lib/libcblas.so /etc/alternatives/libblas-3.so
# sudo ln -sf /usr/local/atlas/lib/liblapack.a /etc/alternatives/liblapack-3.a
# sudo ln -sf /usr/local/atlas/lib/liblapack.so /etc/alternatives/liblapack-3.so
# sudo ldconfig

echo "You must download the files"
echo "  http://www.caam.rice.edu/software/ARPACK/SRC/arpack96.tar.gz"
echo "  http://www.caam.rice.edu/software/ARPACK/SRC/patch.tar.gz"
echo "and extract them here if you want to have ARPACK support"

if [ ! -f arpack96.tar.gz ] ; then
    wget http://www.caam.rice.edu/software/ARPACK/SRC/arpack96.tar.gz
fi
if [ ! -f patch.tar.gz ] ; then
    wget http://www.caam.rice.edu/software/ARPACK/SRC/patch.tar.gz
fi
tar xfz arpack96.tar.gz
tar xfz patch.tar.gz
