#!/usr/bin/env bash

###########################################
## 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 "

# Compile for 64-bit if SAGE64 is set to 'yes'
if [ "x$SAGE64" = xyes ] ; then
   echo "Building a 64-bit version of rubiks"
   CFLAGS="$CFLAGS -m64 "
   CXXFLAGS="$CXXFLAGS -m64 "
   FCFLAGS="$FCFLAGS -m64 "
   F77FLAGS="$F77FLAGS -m64 "
   # Some packages may need LDFLAGS and/or ABI set here.
   LDFLAGS="$LDFLAGS -m64 "

   # If an environment variable CFLAG64 is set, this will be assumed to
   # be the compiler flag needed for a 64-bit build. If not, -m64 will
   # be assumed.

   if [ -z "$CFLAG64" ] ; then
      CFLAG64=-m64
   fi
   export CFLAG64

   # The makefile for dikcube and size222 originally set CFLAGS
   # internally, and did not import CFLAGS from the environment
   # so it is necessary that these makefiles are modified to enable
   # a 64-bit build.
   # It should be noted there is no advantage to having
   # rubiks build as a 64-bit application, but having two thirds of rubiks build
   # 64-bit and one third build 64-bit is very unprofessional, and
   # could lead to confusion by later developers. It is far better
   # if all of Sage builds 64-bit, as then it will be easier to find
   # any components which are failing to do so. While it may not
   # matter for some components, for others it will. Having all of
   # Sage 32-bit or all of Sage 64-bit makes more sense.

   # Use 'sed' to add ${CFLAG64} to the list of compiler flags (CFLAGS).
   # The original line is
   # CFLAGS = -O -DLARGE_MEM -DVERBOSE
   # This will be changed to:
   # CFLAGS = ${CFLAG64} -O -DLARGE_MEM -DVERBOSE
   sed 's/CFLAGS = -O -DLARGE_MEM -DVERBOSE/CFLAGS = ${CFLAG64} -O -DLARGE_MEM -DVERBOSE/' src/dik/makefile > makefile.full-64-bit-build
   mv makefile.full-64-bit-build src/dik/makefile
fi



# 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

# Add appropriate flag(s) to show all warnings.
# This test of a compiler is not perfect by any means, but
# is better than nothing.
if "$CC" -flags > /dev/null 2>&1 ; then
   SUN_COMPILER=1
   # The Sun compilers are fussy, and adding extra
   # warnings will just show too many.
else
   # Assume gcc if not the Sun C compiler.
   # Add -Wall to show all warnings.
   CFLAGS="$CFLAGS -Wall "
   CXXFLAGS="$CXXFLAGS -Wall "
   FCFLAGS="$FCFLAGS -Wall "
   F77FLAGS="$F77FLAGS -Wall "
   GNU_COMPILER=1
fi

# Determine if the C++ compiler is the Sun or GNU compiler
# Just to check we are not mising GNU and non-GNU.
if "$CXX" -flags > /dev/null 2>&1 ; then
   SUN_COMPILER=1
else
   GNU_COMPILER=1
fi


# Determine if the Fortran compiler is the Sun or GNU compiler
if [ -z "$SAGE_FORTRAN" ] ; then
   echo "No Fortran compiler has been defined. This is not normally a problem."
else
   if "$SAGE_FORTRAN" -flags > /dev/null 2>&1 ;  then
      SUN_COMPILER=1
   else
      GNU_COMPILER=1
   fi
fi

# Check if SAGE_FORTRAN_LIB is defined, that the file actually exists.
# SAGE_FORTRAN_LIB does not always need to be defined, but if it is defined, then
# the file should exist.

if [ -n "$SAGE_FORTRAN_LIB" ] &&  [ ! -e "$SAGE_FORTRAN_LIB" ]; then
   echo "SAGE_FORTRAN_LIB is defined as $SAGE_FORTRAN_LIB, but does not exist"
   exit 1
fi

# Checks that the user is not mixing the Sun and GNU compilers. This problem
# has been seen on code built with the aid of SCons, but in general could
# happen with any code if the user has specified a C compiler but not a C++ one.
# This problem is even more likely to occur with the Fortran compiler - I've done
# it myself when building Sage!

if [ "x$SUN_COMPILER" = "x1" ] && [ "x$GNU_COMPILER" = "x1" ] ; then
   echo "You are mixing the Sun and GNU C/C++/Fortran compilers"
   echo "Such a combination will lead to problems."
   echo "Check CC, CXX & SAGE_FORTRAN carefully."
   echo "Exiting ..."
   exit 1
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"

# Used by Sage in connection with Fortran
echo "Using SAGE_FORTRAN=$SAGE_FORTRAN"
echo "Using SAGE_FORTRAN_LIB=$SAGE_FORTRAN_LIB"

# 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)

# Copy some sensible Makefiles, which don't have a load of hard-coded
# junk in them. The original makefiles were a mess. I doubt they
# would have created 64-bit binaries, but that would have been
# the least of their problems. They had the compiler g++ hardcoded
# which made building with Sun Studio impossible.

cp patches/dietz-mcube-Makefile src/dietz/mcube/Makefile
cp patches/dietz-solver-Makefile src/dietz/solver/Makefile
cp patches/dietz-cu2-Makefile src/dietz/cu2/Makefile
cp patches/reid-Makefile src/reid/Makefile

if [ $UNAME = "CYGWIN" ]; then
    cp patches/makefile.dik.cygwin src/dik/makefile
fi

if [ $UNAME = "SunOS" ]; then
    INSTALL=cp; export INSTALL
else
    INSTALL=`which 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"

