#!/usr/bin/env bash

if [ "$SAGE_LOCAL" = "" ]; then
   echo "SAGE_LOCAL undefined ... exiting";
   echo "Maybe run 'sage -sh'?"
   exit 1
fi

if [ "x$SAGE64" = xyes  ]; then
   echo "Building R 64 bit."
   CFLAGS="-O2 -g -m64 "; export CFLAGS
   LDFLAGS="-m64 "; export LDFLAGS
fi

if [ `uname` = "Darwin" ]; then
   echo "Copying fake java and javac compiler on OSX"
   cp patches/java "$SAGE_LOCAL"/bin
   cp patches/javac "$SAGE_LOCAL"/bin
fi

CUR=`pwd`

# set CPPFLAGS so that Sage's readline & iconv are picked up
CPPFLAGS="-I$SAGE_LOCAL/include $CPPFLAGS"; export CPPFLAGS

# Hack to get around dumb hardcoding.
cp patches/R.sh.in src/src/scripts/


# Note by David Kirkby, Feb 16th 2010. /usr/include/X11/Xwindows.h does
# not exist on Solaris, but R configures OK with X support. Hence I've added
# a more specific test on Solaris, by testing for a library. That library 
# exists both on Solaris 10 03/2005 (SPARC) and on OpenSolaris. 
if [ -f /usr/include/X11/Xwindows.h ]; then
    XSUPPORT=yes
else
   if [ "x`uname`" = xSunOS ] && [ -f /usr/X11/lib/libXv.so ] ; then 
       XSUPPORT=yes
   # Note by Karl-Dieter Crisman, April 12th 2010. X support would be nice
   # to have in OSX, but see 
   # http://CRAN.R-project.org/bin/macosx/RMacOSX-FAQ.html#X11-window-server-_0028optional_0029
   # for how differently this would have to be handled on different OSX
   # versions, none trivially.  In any case, aqua, which we enable, 
   # performs the same function on OSX.
   else
       XSUPPORT=no
   fi
fi

# we don't want to install R as a library framework on OSX
if [ `uname` = "Darwin" ]; then
    OSXFW="--enable-R-framework=no"; export OSXFW
else
    OSXFW=""; export OSXFW
fi

if [ "x`uname`" = xSunOS ]; then
    # Note by David Kirkby, 16th Feb 2010. Even after adding the iconv library
    # R would not build properly on Solaris 10, complaining of undefined symbols 
    # uiter_setUTF8 and  ucol_strcollIter
    # After an email to r-help@r-project.org, Ei-ji Nakama (rim.nakama@gmail.com)
    # emailed me and said the option --without-ICU might help, which it did. I don't see 
    # this option documented, but for now at least, it does allow R to build. 

    echo "Disabling ICU on Solaris, using an undocumented option --without-ICU"
    echo "since the ICU library is not included on Solaris."
    SUN_FLAGS="--without-ICU"; export SUN_FLAGS
fi

# let's remove old install, even the wrongly installed ones
rm -rf "$SAGE_LOCAL"/lib/r
rm -rf "$SAGE_LOCAL"/lib/R
rm -rf "$SAGE_LOCAL"/lib64/R
rm -rf "$SAGE_LOCAL"/lib64/r
# and let's also nuke some leftovers on SAGE_LOCAL/lib
rm -rf "$SAGE_LOCAL"/lib/libRblas.so "$SAGE_LOCAL"/lib/libRlapack.so "$SAGE_LOCAL"/lib/libR.so
rm -rf "$SAGE_LOCAL"/lib/libRblas.dylib "$SAGE_LOCAL"/lib/libRlapack.dylib "$SAGE_LOCAL"/lib/libR.dylib

cd src

# make sure the prefix is always lib, not lib64 on 64 bit linux
LIBnn="lib"; export LIBnn

FC=sage_fortran; export FC
F77=sage_fortran; export F77

# These flags are *critical* so that R will be built against Sage's 
# copy of readline and ATLAS.
CFLAGS="-I$SAGE_LOCAL/include -L$SAGE_LOCAL/lib/ $CFLAGS"; export CFLAGS
LDFLAGS="-L$SAGE_LOCAL/lib/ $LDFLAGS"; export LDFLAGS   

# FreeBSD needs the path to libiconv to be explicitly specified.  In theory, 
# --with-libiconv-prefix should work but configure script is broken and 
# ignores that path when looking for libiconv.  Hard-wire via xxFLAGS. 
# Note by David Kirkby, 16th Feb 2010. The option --with-libiconv-prefix
# is not documented in the latest R manual, neither is it an option if you 
# type 'configure --help'.  Since there is an iconv library going to be 
# integrated into Sage - see http://trac.sagemath.org/sage_trac/ticket/8191
# it is likely these FreeBSD specific lines can be removed, but I'll leave 
# them here for a FreeBSD developer to address.
if [ `uname` = "FreeBSD" ]; then 
    CPPFLAGS="$CPPFLAGS -I/usr/local/include" 
    CFLAGS="$CFLAGS -I/usr/local/include" 
    LDFLAGS="$LDFLAGS -L/usr/local/lib" 
fi 

if [ `uname` = "Darwin" ]; then
     echo "Configuring R for OSX"
    ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib --with-x=$XSUPPORT --with-readline="$SAGE_LOCAL" $OSXFW
else
     echo "Configuring R with ATLAS"
    ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib --with-x=$XSUPPORT --with-readline="$SAGE_LOCAL" --with-blas="-L$SAGE_LOCAL/lib -lf77blas -latlas" --with-lapack="-L$SAGE_LOCAL/lib -llapack -lcblas" $SUN_FLAGS
fi

if [ $? -ne 0 ]; then
     echo "Configuring R with fallback options"
    ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib --with-x=no --with-readline="$SAGE_LOCAL" $OSXFW $SUN_FLAGS
fi

if [ $? -ne 0 ]; then
    echo "Error configuring R."
    exit 1
fi

$MAKE R
if [ $? -ne 0 ]; then
    echo "Error building R."
    exit 1
fi

#parallel make install is broken
export MAKE=make
$MAKE vignettes #needed for help system
$MAKE install
if [ $? -ne 0 ]; then
    echo "Error installing R."
    exit 1
fi

#$MAKE install-libR
#if [ $? -ne 0 ]; then
#    echo "Error installing libR."
#    exit 1
#fi

# Fix hardcoding problems.
cd "$CUR"/patches
python fix_hardcode

echo "Now install rpy"

cd "$CUR"

RPY_VER=rpy2-2.0.8

sage -f "$RPY_VER".spkg 
if [ ! -f "$SAGE_ROOT"/spkg/installed/"$RPY_VER" ]; then
    echo "Error installing rpy."
    exit 1
fi

if [ `uname` = "Darwin" ]; then
    echo "Copying fake java and javac compiler on OSX"
    rm -f "$SAGE_LOCAL"/bin/java
    rm -f "$SAGE_LOCAL"/bin/javac
fi


