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

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

if [ "x$SAGE_DEBUG" = "xyes" ]; then
    # This used to disable omalloc but that is not really supported
    # by upstream
    SINGULAR_CONFIGURE="$SINGULAR_CONFIGURE --enable-debug --disable-optimizationflags"

    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


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.
    sdh_configure --exec-prefix="$SAGE_LOCAL" \
                  --bindir="$SAGE_LOCAL/bin" \
                  $SAGE_CONFIGURE_GMP \
                  $SAGE_CONFIGURE_NTL \
                  $SAGE_CONFIGURE_FLINT \
                  --enable-gfanlib \
                  --enable-Singular \
                  --enable-factory \
                  --disable-doc \
                  --disable-polymake \
                  --with-python=sage-python23 \
                  $SINGULAR_CONFIGURE
}


build_singular()
{
    sdh_make
    sdh_make_install
}


# 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
