###########################################
## rubiks
###########################################

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

set -e

# Add a sensible default optimisation flag. Change if necessary.
OPTIMIZATION_FLAGS="-O2"
# Work around a bug in gcc 4.6.0: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48702
if [ "`testcc.sh $CC`" = GCC ] ; then
    if $CC -dumpversion 2>/dev/null |grep >/dev/null '^4\.6\.[01]$' ; then
        echo "Warning: Working around bug in gcc 4.6.0"
        OPTIMIZATION_FLAGS="$OPTIMIZATION_FLAGS -fno-ivopts"
    fi
fi

# Most packages do not need all these set
# But it is better to do them all each time, rather than ommit
# a flag by mistake.

CFLAGS="$CFLAGS $OPTIMIZATION_FLAGS "
CXXFLAGS="$CXXFLAGS $OPTIMIZATION_FLAGS "
FCFLAGS="$FCFLAGS $OPTIMIZATION_FLAGS "
F77FLAGS="$F77FLAGS $OPTIMIZATION_FLAGS "

# If SAGE_DEBUG is set either unset (the default), or set to  'yes'
# Add debugging information.
# Since both the Sun and GNU compilers accept -g to give debugging information
# there is no need to do anything specific to one compiler or the other.

if [ "x$SAGE_DEBUG" = "x" ] || [ "x$SAGE_DEBUG" = "xyes" ] ; then
   echo "Code will be built with debugging information present. Set 'SAGE_DEBUG' to 'no' if you don't want that."
   # Actually anything other than 'yes' will cause
   # no debugging information to be added.
   CFLAGS="$CFLAGS -g "
   CXXFLAGS="$CXXFLAGS -g "
   FCFLAGS="$FCFLAGS -g "
   F77FLAGS="$F77FLAGS -g "
else
   echo "No debugging information will be used during the build of this package"
   echo "Unset SAGE_DEBUG if you want debugging information present (-g added)"
fi

# These are all used by GNU to specify compilers.
echo "Using CC=$CC"
echo "Using CXX=$CXX"
echo "Using FC=$FC"
echo "Using F77=$F77"

# Flags which may be set.
echo "The following environment variables will be exported"
echo "Using CFLAGS=$CFLAGS"
echo "Using CXXFLAGS=$CXXFLAGS"
echo "Using FCFLAGS=$FCFLAGS"
echo "Using F77FLAGS=$F77FLAGS"
echo "Using CPPFLAGS=$CPPFLAGS"
echo "Using LDFLAGS=$LDFLAGS"
echo "Using ABI=$ABI"
echo "configure scripts and/or makefiles might override these later"
echo " "

# export everything. Probably not necessary in most cases.
export CFLAGS
export CXXFLAGS
export FCFLAGS
export F77FLAGS
export CPPFLAGS
export LDFLAGS
export ABI
export CPPFLAGS

# End of pretty general spkg-install file.
# Now do the specific things needed for this package (rubiks)

if [ $UNAME = "SunOS" ]; then
    INSTALL=cp; export INSTALL
else
    INSTALL=install; export INSTALL
fi


# remove the old dik binary since the has a name collision
# with polynmake - see #2595
rm -f $SAGE_LOCAL/bin/cube

cd src

echo "Building Rubiks cube solvers"
$MAKE install PREFIX="$SAGE_LOCAL"

