###########################################
## Singular
###########################################

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

SRC=`pwd`/src
cd "$SRC"

if [ "x$SAGE_DEBUG" = "xyes" ]; then
    SINGULAR_CONFIGURE="$SINGULAR_CONFIGURE --enable-debug --disable-optimizationflags"

    # --disable-omalloc is broken: linking fails because of missing flags
    #SINGULAR_CONFIGURE="$SINGULAR_CONFIGURE --disable-omalloc"

    # Replace omalloc by xalloc in places unaffected by --disable-omalloc
    # See xalloc/README, altough here we just replace the folder for simplicity
    rm -rf "$SRC/omalloc"
    mv "$SRC/xalloc" "$SRC/omalloc"

    CFLAGS="$CFLAGS -O0 -g"
    CXXFLAGS="$CXXFLAGS -O0 -g"
else
    # Singular 4.x (mostly) does not longer set defaults for CFLAGS and CXXFLAGS
    CFLAGS="-O2 -g $CFLAGS"
    CXXFLAGS="-O2 -g $CXXFLAGS"
fi

export CFLAGS CXXFLAGS

if [ "x$SAGE64" = xyes ]; then
    echo "Building a 64-bit version of Singular"
    CFLAGS="$CFLAGS -m64"
    CXXFLAGS="$CXXFLAGS -m64"
    CPPFLAGS="$CPPFLAGS -m64"; export CPPFLAGS
    LDFLAGS="$LDFLAGS -m64"; export LDFLAGS
    if [ "x`uname`" = xSunOS ] ; then
        export CC="$CC -m64"
        export CXX="$CXX -m64"
    fi
fi

remove_old_version()
{
    # the following is a little verbose but it ensures we leave no trace of 3.x
    # _or_ 4.x
    rm -f "$SAGE_LOCAL"/bin/*Singular*
    rm -f "$SAGE_LOCAL"/bin/*singular*
    rm -rf "$SAGE_LOCAL/include/singular" # 3.x and 4.x
    rm -rf "$SAGE_LOCAL/include/factory" # 3.x and 4.x
    rm -f "$SAGE_LOCAL/include/factor.h" # 3.x only
    rm -f "$SAGE_LOCAL/include/factoryconf.h" # 3.x only
    rm -rf "$SAGE_LOCAL/include/omalloc" #4.x only
    rm -f "$SAGE_LOCAL/include/omalloc.h" # 3.x only
    rm -f "$SAGE_LOCAL/include/omlimits.h" # 3.x only
    rm -rf "$SAGE_LOCAL/include/resources" #4.x only
    rm -rf "$SAGE_LOCAL/include/gfanlib" #4.x only

    # Clean up all Singular-related libraries
    libs=(
        singular  # 3.x with lower case
        singcf    # 3.x only additional archives
        singfac   # 3.x only additional archives
        Singular  # 4.x with upper case
        polys
        factory
        omalloc
        resources           # 3.x
        singular_resources  # 4.x and up
        gfan
    )
    if [ "$UNAME" = "CYGWIN" ]; then
        for name in ${libs[*]}; do
            rm -f "$SAGE_LOCAL"/bin/cyg${name}*.dll
            rm -f "$SAGE_LOCAL"/lib/lib${name}*.a
        done
    else
        for name in ${libs[*]}; do
            rm -f "$SAGE_LOCAL"/lib/lib${name}*
        done
    fi

    rm -f "$SAGE_LOCAL"/lib/p_Procs_Field* # 3.x only
    rm -rf "$SAGE_LOCAL/share/singular"
    rm -f "$SAGE_LOCAL"/share/info/singular*
}

config()
{
    # configure notes (dates from Singular 3.x, maybe outdated for 4.x):
    # 1) We really need to add --exec-prefix and --bindir as Singular
    #    uses some weird defaults.
    # 2) configure calls other configure scripts (for example
    #    omalloc/configure).  Not all of these configure scripts
    #    support all options given here, leading to warnings which
    #    may be ignored.
    ./configure --prefix="$SAGE_LOCAL" \
                --exec-prefix="$SAGE_LOCAL" \
                --bindir="$SAGE_LOCAL"/bin \
                --libdir="$SAGE_LOCAL"/lib \
                --with-gmp="$SAGE_LOCAL" \
                --with-ntl="$SAGE_LOCAL" \
                --with-flint="$SAGE_LOCAL" \
                --enable-gfanlib \
                --enable-Singular \
                --enable-factory \
                --disable-doc \
                --disable-polymake \
                $SINGULAR_CONFIGURE

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


build_singular()
{
    $MAKE
    if [ $? -ne 0 ]; then
        echo >&2 "Error building Singular."
        exit 1
    fi

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

}


# Actually run all the functions defined above
for i in remove_old_version config build_singular; do
    echo "############### Singular stage $i ###############"
    cd "$SRC" && $i
done
