#!/usr/bin/env bash

###############################################################################
#
#  NTL sage install script
#
#  Copyright (C) 2005 William Stein <wstein@ucsd.edu>
#  Distributed under the terms of the GNU General Public License (GPL)
#
#  AUTHORS: William Stein (original version)
#           David Kirkby (2005-12-13); <david.kirkby@onetel.net>
#           Jean-Pierre Flori (2012-08-07) <jean-pierre.flori@ssi.gouv.fr>
#
###############################################################################

if [ -z "$SAGE_LOCAL" ]; then
    echo >&2 "Error: SAGE_LOCAL undefined - exiting..."
    echo >&2 "Maybe run 'sage -sh'?"
    exit 1
fi

CUR=`pwd`

ntl_libtool()
{
    echo "Generating libtool."

    cd "$CUR/src/libtool"

    ./configure

    if [ $? -ne 0 ]; then
        echo >&2 "Error generating libtool."
        exit 1
    fi
}

ntl_patch()
{
    echo
    echo "Applying patches to NTL."

    cd "$CUR/src/ntl"

    # Apply all patches
    for patch in "$CUR"/patches/*.patch; do
        patch -p1 < "$patch"
        if [ $? -ne 0 ]; then
            echo >&2 "Error applying '$patch'."
            exit 1
        fi
    done
}

ntl_configure()
{
    echo
    echo "Configuring NTL."

    cd "$CUR/src/ntl/src"

    # Run the configure script, setting CC, CXX, CFLAGS etc as needed.
    # This ensures that they get written by DoConfig into 'makefile'.
    CFLAGS="-O2 -g $CFLAGS"
    CXXFLAGS="-O2 -g $CXXFLAGS"

    if [ "$SAGE64" = "yes" ]; then
        if [ -z "$CFLAG64" ]; then
            CFLAG64="-m64"
        fi
        CFLAGS="$CFLAG64 $CFLAGS"
        CXXFLAGS="$CFLAG64 $CXXFLAGS"
    fi

    case "$UNAME" in
      Darwin)
        echo "Setting SHAREDFLAGS to '-fno-common'"
        SHAREDFLAGS="-fno-common"
        ;;
      CYGWIN)
        LIBTOOL_LINK_FLAGS="-no-undefined"
        ;;
    esac

    ./configure DEF_PREFIX="$SAGE_LOCAL" SHARED=on \
        CC="$CC" CFLAGS="$CFLAGS $SHAREDFLAGS" \
        CXX="$CXX" CXXFLAGS="$CXXFLAGS $SHAREDFLAGS" \
        LDFLAGS="$LDFLAGS" LIBTOOL_LINK_FLAGS="$LIBTOOL_LINK_FLAGS" \
        NTL_GMP_LIP=on NTL_GF2X_LIB=on NTL_STD_CXX=on \
        LIBTOOL="$CUR/src/libtool/libtool"

    if [ $? -ne 0 ]; then
        echo >&2 "Error configuring NTL."
        exit 1
    fi
}

ntl_build()
{
    echo
    echo "Tuning and building NTL."

    cd "$CUR/src/ntl/src"

    $MAKE

    if [ $? -ne 0 ]; then
        echo >&2 "Error building/tuning NTL."
        exit 1
    fi
}

ntl_install()
{
    echo
    echo "Installing NTL."

    cd "$CUR/src/ntl/src"

    $MAKE install

    if [ $? -ne 0 ]; then
        echo >&2 "Error installing NTL."
        exit 1
    fi
}

ntl_libtool
ntl_patch
ntl_configure
ntl_build
ntl_install
