#!/usr/bin/env bash

########################################################################
# Set various environment variables
########################################################################

# Assume current directory is SAGE_ROOT/spkg
SAGE_ROOT=`cd .. && pwd -P`
SAGE_LOCAL="$SAGE_ROOT/local"
SAGE_SHARE="$SAGE_LOCAL/share"
SAGE_LOGS="$SAGE_ROOT/logs/pkgs"
SAGE_SPKG_INST="$SAGE_ROOT/spkg/installed"
PATH="$SAGE_ROOT/spkg/bin:$SAGE_LOCAL/bin:$PATH"
PYTHONPATH="$SAGE_LOCAL"
export SAGE_ROOT SAGE_LOCAL SAGE_LOGS SAGE_SPKG_INST PATH PYTHONPATH

# Storing the start time of the build process. The time is stored in
# seconds since 1970-01-01 in a hidden file called
# "SAGE_ROOT/.BUILDSTART". See ticket #6744.
echo `date -u "+%s"` > "$SAGE_ROOT/.BUILDSTART"

########################################################################
# Fixes for upgraded Sage versions
########################################################################
# The script 'pipestatus' used to be autogenerated by this file.  It
# should be present in any version of Sage starting with 4.5.  In
# #11073, the autogeneration was removed, so we just check to see if
# the file is missing.
if [ ! -f pipestatus ]; then
    cat >&2 <<EOF
Error: the script 'pipestatus' is missing.  If you are trying to
upgrade, your original version of Sage is too old, so upgrading to a
5.x version of Sage won't work.  Your options are:
 - Upgrading first to sage-4.8 and then upgrading again to sage-5.x.
 - Downloading a new binary version of Sage.
 - Building a new source version from scratch.

EOF
    exit 2
fi

# If spkg/bin/sage-env doesn't exist, we are surely upgrading (sage-env
# was moved to spkg/bin in sage-5.0).  Manually set SAGE_UPGRADING=yes,
# as old versions of sage-upgrade didn't do this.
if [ ! -f "$SAGE_ROOT/spkg/bin/sage-env" ]; then
    SAGE_UPGRADING=yes
fi

if [ "$SAGE_UPGRADING" = yes ]; then
    # We're doing an upgrade. Let spkg/Makefile call sage-spkg with
    # "-f" to force rebuilding dependent packages, too:
    export SAGE_SPKG_OPTS="-f"

    # Avoid RM being set to "rm" (older versions of Sage did this):
    #   http://trac.sagemath.org/sage_trac/ticket/3537
    if [ "$RM" = rm ]; then
        export RM="rm -f"
    fi

    # Unset AS and LD if they are set to their default values,
    # otherwise the AS and LD test from #14296 below will fail.
    if [ "$AS" = as ]; then
        unset AS
    fi
    if [ "$LD" = ld ]; then
        unset LD
    fi

    # If we have an old version of sage-location,
    # then manually fix the .la (libtool) files.
    if grep 'lib64' "$SAGE_LOCAL/bin/sage-location" &>/dev/null; then
        # sage-location does check lib64, so everything is fine.
        true
    else
        echo "Found an old version of sage-location, manually fixing some Libtool files."
        for d in "$SAGE_LOCAL/lib32" "$SAGE_LOCAL/lib64"; do
            for la_file in "$d"/*.la; do
                [ -w "$la_file" ] || continue
                sed -i "s ^libdir=.* libdir='$d' " "$la_file"
            done
        done
    fi

    # The old sage-make_relative script is troublesome (#13407 and #14027).
    # We simply delete it, its role is taken over by sage-location.
    rm -f "$SAGE_LOCAL/bin/sage-make_relative"
fi

###############################################################################
# Create basic directories needed for Sage
###############################################################################

mkdir -p "$SAGE_ROOT/tmp"
mkdir -p "$SAGE_LOGS"
mkdir -p "$SAGE_LOCAL/bin"
mkdir -p "$SAGE_LOCAL/etc"
mkdir -p "$SAGE_LOCAL/lib"
mkdir -p "$SAGE_SPKG_INST"
mkdir -p "$SAGE_SHARE"

# If we are upgrading from an old version of Sage where $SAGE_SHARE
# was stored at $SAGE_ROOT/data, move it to the right location.
# Check that $SAGE_ROOT/data is an existing directory and not a symlink.
if [ -d "$SAGE_ROOT/data" ] && [ ! -h "$SAGE_ROOT/data" ]; then
    mkdir -p "$SAGE_ROOT/devel"
    mv "$SAGE_ROOT"/data/extcode "$SAGE_ROOT/devel/ext-main"
    mv "$SAGE_ROOT"/data/* "$SAGE_SHARE"
    rm -rf "$SAGE_ROOT/data"
fi

# Symlink old SAGE_DATA to SAGE_SHARE for optional/experimental
# packages that haven't yet been updated.
rm -f "$SAGE_ROOT/data"
ln -s local/share "$SAGE_ROOT/data"

###############################################################################
# Determine whether to install GCC (gcc, g++, gfortran).
###############################################################################

# Give a deprecation message whenever SAGE_FORTRAN or SAGE_FORTRAN_LIB
# is set to something. See Trac #13349.
if [ -n "$SAGE_FORTRAN" ] || [ -n "$SAGE_FORTRAN_LIB" ]; then
cat >&2 <<EOF
WARNING: the environment variables SAGE_FORTRAN and SAGE_FORTRAN_LIB
are deprecated since sage-5.3.

The GNU Compiler Collection (GCC) is included in Sage and will be
installed if you do not have a Fortran compiler. If you do have a
Fortran compiler installed and you want to specify its path, you can
use the standard environment variable FC (instead of SAGE_FORTRAN) to
point to the Fortran compiler.

As replacement to SAGE_FORTRAN_LIB, you can either add the directory
containing the Fortran library to LIBRARY_PATH and/or LD_LIBRARY_PATH,
or copy/symlink the Fortran library into \$SAGE_ROOT/local/lib. In most
cases, none of this is needed as the compiler/linker should find the
Fortran library by itself.

Support for SAGE_FORTRAN and SAGE_FORTRAN_LIB might be removed from
future versions of Sage. Moreover, these variables are not tested
anymore and might not work as you expect.
EOF
sleep 5
fi

# Determine various compilers.  These variables should not be exported,
# they are only used in this spkg/install script to determine whether to
# install GCC.  The "real" $CC, $CXX,... variables for building Sage are
# set in sage-env.

if [ -z "$CC" ]; then
    CC=gcc
fi

if [ -z "$CXX" ]; then
    if command -v g++ >/dev/null 2>/dev/null; then
        CXX=g++
    fi
fi

if [ -z "$FC" ]; then
    if [ -n "$SAGE_FORTRAN" ]; then
        FC="$SAGE_FORTRAN"
    elif command -v gfortran >/dev/null 2>/dev/null; then
        FC=gfortran
    elif command -v g95 >/dev/null 2>/dev/null; then
        FC=g95
    elif command -v g77 >/dev/null 2>/dev/null; then
        FC=g77
    fi
fi

if [ -f "$SAGE_LOCAL/bin/gcc" ]; then
    # GCC is already installed. Normally we don't need to re-install
    # GCC, unless this is an upgrade. To disable unneeded rebuilding
    # of GCC, we touch the installed file for GCC, such that it will
    # really only be built if one of its dependencies has been upgraded.
    if [ "$SAGE_UPGRADING" = yes ]; then
        echo >&2 "We are upgrading Sage and GCC was installed before, it will get"
        echo >&2 "re-installed if needed."
        need_to_install_gcc=yes
        for f in "$SAGE_SPKG_INST"/gcc-*; do
            if [ -f "$f" ]; then
                touch "$f"
            fi
        done
    else
        need_to_install_gcc=no
    fi
elif [ -n "$SAGE_INSTALL_GCC" ]; then
    # Check the value of the environment variable SAGE_INSTALL_GCC
    case "$SAGE_INSTALL_GCC" in
        yes)
            echo >&2 "Installing GCC because SAGE_INSTALL_GCC is set to 'yes'."
            need_to_install_gcc=yes;;
        no)
            need_to_install_gcc=no;;
        *)
            echo >&2 "Error: SAGE_INSTALL_GCC should be set to 'yes' or 'no'."
            echo >&2 "You can also leave it unset to install GCC when needed."
            exit 2;;
    esac
else
    # SAGE_INSTALL_GCC is not set, install GCC when needed.
    need_to_install_gcc=no

    # Check whether $CC is some version of GCC.  If it's a different
    # compiler, install GCC.
    CCtype=`testcc.sh $CC`
    if [ "$CCtype" != GCC ]; then
        echo >&2 "Installing GCC because your '$CC' isn't GCC (GNU CC)."
        need_to_install_gcc=yes
    else
        # $CC points to some version of GCC, find out which version.
        GCCVERSION=`$CC -dumpversion`
        case $GCCVERSION in
            [0-3].*|4.[0-3]|4.[0-3].*)
                # Install our own GCC if the system-provided one is older than gcc-4.4.
                # * gcc-4.2.4 compiles a slow IML:
                # https://groups.google.com/forum/?fromgroups#!topic/sage-devel/Ux3t0dW2FSI
                # * gcc-4.3 might have trouble building ATLAS:
                # https://groups.google.com/forum/?fromgroups#!topic/sage-devel/KCeFqQ_w2FE
                echo >&2 "Installing GCC because you have $CC version $GCCVERSION, which is quite old."
                need_to_install_gcc=yes;;
            4.4*|4.5*)
                # GCC 4.4.x and GCC 4.5.x fail to compile PARI/GP on ia64:
                # * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46044
                if [ x`uname -m` = xia64 ]; then
                    echo >&2 "Installing GCC because you have $CC version $GCCVERSION on ia64."
                    echo >&2 "gcc <= 4.5 fails to compile PARI/GP on ia64."
                    need_to_install_gcc=yes
                fi;;
            4.6.[01])
                # Also install GCC if we have version 4.6.0 or 4.6.1, which is
                # known to give trouble within Sage:
                # * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48702
                # * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48774
                echo >&2 "Installing GCC because you have $CC version $GCCVERSION."
                echo >&2 "gcc-4.6.0 and gcc-4.6.1 have known bugs affecting Sage."
                need_to_install_gcc=yes;;
            4.6*)
                # GCC 4.6.x doesn't compile ECL on Cygwin:
                # * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52061
                if uname | grep CYGWIN >/dev/null; then
                    echo >&2 "Installing GCC because you have $CC version $GCCVERSION on Cygwin."
                    echo >&2 "gcc-4.6.x fails to compile ECL on Cygwin."
                    need_to_install_gcc=yes
                fi;;
            4.7.0)
                # GCC 4.7.0 is very broken on ia64, see
                # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48496
                # This is fixed in GCC 4.7.1.
                if [ x`uname -m` = xia64 ]; then
                    echo >&2 "Installing GCC because you have $CC version $GCCVERSION on ia64."
                    echo >&2 "gcc-4.7.0 has a serious bug on ia64."
                    need_to_install_gcc=yes
                fi;;
        esac
    fi

    # Check C++ and Fortran compilers.
    if [ -z "$CXX" ]; then
        echo >&2 "Installing GCC because a C++ compiler is missing."
        need_to_install_gcc=yes
    fi

    if [ -z "$FC" ]; then
        echo >&2 "Installing GCC because a Fortran compiler is missing."
        need_to_install_gcc=yes
    fi
fi

# If we are not installing GCC: check that the assembler and linker
# used by $CC match $AS and $LD.
# See http://trac.sagemath.org/sage_trac/ticket/14296
if [ $need_to_install_gcc != yes ]; then
    if [ "$AS" != "" ]; then
        CC_as=`$CC -print-file-name=as 2>/dev/null`
        CC_as=`command -v $CC_as 2>/dev/null`
        cmd_AS=`command -v $AS`

        if [ "$CC_as" != "" -a "$CC_as" != "$cmd_AS" ]; then
            echo >&2 "Error: Mismatch of assemblers between"
            echo >&2 " * $CC using $CC_as"
            echo >&2 " * \$AS equal to $AS"
            if [ "$SAGE_PORT" = "" ]; then
                echo >&2 "Aborting, either change or unset AS or set SAGE_PORT=yes or set"
                echo >&2 "SAGE_INSTALL_GCC=yes (this GCC would use assembler $AS)"
                exit 1
            else
                echo >&2 "Continuing since SAGE_PORT is set."
            fi
        fi
    fi
    if [ "$LD" != "" ]; then
        CC_ld=`$CC -print-file-name=ld 2>/dev/null`
        CC_ld=`command -v $CC_ld 2>/dev/null`
        cmd_LD=`command -v $LD`

        if [ "$CC_ld" != "" -a "$CC_ld" != "$cmd_LD" ]; then
            echo >&2 "Error: Mismatch of linkers between"
            echo >&2 " * $CC using $CC_ld"
            echo >&2 " * \$LD equal to $LD"
            if [ "$SAGE_PORT" = "" ]; then
                echo >&2 "Aborting, either change or unset LD or set SAGE_PORT=yes or set"
                echo >&2 "SAGE_INSTALL_GCC=yes (this GCC would use linker $LD)"
                exit 1
            else
                echo >&2 "Continuing since SAGE_PORT is set."
            fi
        fi
    fi
fi

###############################################################################
# Create the sage_fortran script.
###############################################################################

# Don't use FC and SAGE_FORTRAN_LIB if we are installing GCC or have
# installed gfortran, as that can cause compatibility problems.
# We want to use the Fortran compiler that we build as part of GCC.
if [ "$need_to_install_gcc" = yes ] || [ -x "$SAGE_LOCAL/bin/gfortran" ]; then
    unset FC
    unset SAGE_FORTRAN_LIB
fi

# Write sage_fortran script.
cat >"$SAGE_LOCAL/bin/sage_fortran" <<EOF
#!/bin/sh

cat >&2 <<ENDOFWARNING
WARNING: The sage_fortran script is deprecated since Sage 5.11 and
might not be supported in future versions of Sage. You should update
your package to use the standard environment variable \\\$FC instead.

ENDOFWARNING

if [ "x\$FC" = x ]; then
    # Default value determined in spkg/install
    myFC='${FC:-gfortran}'
else
    myFC="\$FC"
fi

# unset FC to avoid an infinite loop in the case that FC=sage_fortran.
unset FC

if [ "\$SAGE64" = yes ]; then
    exec \$myFC -m64 -fPIC "\$@"
else
    exec \$myFC -fPIC "\$@"
fi
EOF
chmod +x "$SAGE_LOCAL/bin/sage_fortran"

# Make the fortran library symlink if requested
if [ -f "$SAGE_FORTRAN_LIB" ]; then
    ( cd "$SAGE_LOCAL/lib" && ln -sf "$SAGE_FORTRAN_LIB" . )
fi

###############################################################################
# Create $SAGE_ROOT/spkg/Makefile starting from spkg/standard/deps
###############################################################################

exec 3>Makefile

cat >&3 <<EOF
#==============================================================================
# This file has been automatically generated by
#   $SAGE_ROOT/spkg/install
# You should not edit it by hand
#==============================================================================

EOF

# If the user (or the Makefile) has set SAGE_PARALLEL_SPKG_BUILD=no,
# then turn off parallel building: disable just building multiple
# packages at the same time.   Individual packages can still be built
# in parallel by specifying '-j' in $MAKE.
if [ "${SAGE_PARALLEL_SPKG_BUILD:-yes}" = no ]; then
    echo ".NOTPARALLEL:" >&3
    echo "" >&3
fi

# Usage: newest_version_base $pkg
# Print version number of latest (according to modification time)
# base package $pkg
newest_version_base() {
    PKG=$1
    # As a fallback, we also look at the latest installed package.
    for FILE in `{ ls -1t base/$PKG-*-install; ls -1t installed/$PKG-*; } 2>/dev/null`
    do
        ANS=`echo "$FILE" | sed 's|.*/||; s|-install||'`
        if [ -n "$ANS" ]; then
            echo "$ANS"
            return 0
        fi
    done

    echo >&2 "Cannot determine latest version of $PKG."
    echo "$PKG"
    return 1
}

# Usage: newest_version $pkg
# Print version number of latest (according to modification time)
# standard or optional package $pkg
newest_version() {
    PKG=$1
    # First find the most recent spkg.  We also look for *optional*
    # packages since downloaded packages arrive in spkg/optional.
    # As a fallback, we also look at the latest installed package.
    for FILE in `{ ls -1t standard/$PKG-*.spkg optional/$PKG-*.spkg; ls -1t "$SAGE_SPKG_INST"/$PKG-*; } 2>/dev/null`
    do
        ANS=`echo "$FILE" | sed 's|.*/||; s|\.spkg||'`
        if [ -n "$ANS" ]; then
            echo "$ANS"
            return 0
        fi
    done

    echo >&2 "Cannot determine latest version of $PKG."
    echo "$PKG"
    return 1
}

cat >&3 <<EOF
# Base packages
PREREQ=`newest_version_base prereq`

# Standard packages
ATLAS=`newest_version atlas`
BOEHM_GC=`newest_version boehm_gc`
BOOST_CROPPED=`newest_version boost_cropped`
BZIP2=`newest_version bzip2`
CDDLIB=`newest_version cddlib`
CEPHES=`newest_version cephes`
CLIQUER=`newest_version cliquer`
CONWAY=`newest_version conway_polynomials`
CVXOPT=`newest_version cvxopt`
CYTHON=`newest_version cython`
DOCUTILS=`newest_version docutils`
ECL=`newest_version ecl`
ECLIB=`newest_version eclib`
ECM=`newest_version ecm`
ELLIPTIC_CURVES=`newest_version elliptic_curves`
EXTCODE=`newest_version extcode`
FFLASFFPACK=`newest_version fflas_ffpack`
FLINT=`newest_version flint`
FLINTQS=`newest_version flintqs`
FPLLL=`newest_version libfplll`
FREETYPE=`newest_version freetype`
GAP=`newest_version gap`
GCC=`newest_version gcc`
GD=`newest_version gd`
GDMODULE=`newest_version gdmodule`
GENUS2REDUCTION=`newest_version genus2reduction`
GFAN=`newest_version gfan`
GF2X=`newest_version gf2x`
GIVARO=`newest_version givaro`
GLPK=`newest_version glpk`
GRAPHS=`newest_version graphs`
GSL=`newest_version gsl`
ICONV=`newest_version iconv`
IML=`newest_version iml`
IPYTHON=`newest_version ipython`
JINJA2=`newest_version jinja2`
JMOL=`newest_version jmol`
LCALC=`newest_version lcalc`
LRCALC=`newest_version lrcalc`
LIBGAP=`newest_version libgap`
LIBPNG=`newest_version libpng`
LINBOX=`newest_version linbox`
M4RI=`newest_version libm4ri`
M4RIE=`newest_version libm4rie`
MATPLOTLIB=`newest_version matplotlib`
MAXIMA=`newest_version maxima`
MERCURIAL=`newest_version mercurial`
MPC=`newest_version mpc`
MPFI=`newest_version mpfi`
MPFR=`newest_version mpfr`
MPIR=`newest_version mpir`
MPMATH=`newest_version mpmath`
NETWORKX=`newest_version networkx`
NTL=`newest_version ntl`
NUMPY=`newest_version numpy`
PALP=`newest_version palp`
PARI=`newest_version pari`
PATCH=`newest_version patch`
PEXPECT=`newest_version pexpect`
PIL=`newest_version pil`
POLYBORI=`newest_version polybori`
POLYTOPES_DB=`newest_version polytopes_db`
PPL=`newest_version ppl`
PYCRYPTO=`newest_version pycrypto`
PYGMENTS=`newest_version pygments`
PYNAC=`newest_version pynac`
PYTHON=`newest_version python`
R=`newest_version r`
RPY=`newest_version rpy2`
RATPOINTS=`newest_version ratpoints`
READLINE=`newest_version readline`
RUBIKS=`newest_version rubiks`
SAGE=`newest_version sage`
SAGENB=`newest_version sagenb`
SAGETEX=`newest_version sagetex`
SAGE_ROOT_REPO=`newest_version sage_root`
SAGE_SCRIPTS=`newest_version sage_scripts`
SCIPY=`newest_version scipy`
SCONS=`newest_version scons`
SETUPTOOLS=`newest_version setuptools`
SINGULAR=`newest_version singular`
SPHINX=`newest_version sphinx`
SQLALCHEMY=`newest_version sqlalchemy`
SQLITE=`newest_version sqlite`
SYMMETRICA=`newest_version symmetrica`
SYMPOW=`newest_version sympow`
SYMPY=`newest_version sympy`
TACHYON=`newest_version tachyon`
NCURSES=`newest_version ncurses`
ZLIB=`newest_version zlib`
ZNPOLY=`newest_version zn_poly`

# Directory to keep track of which packages are installed
INST=`echo "$SAGE_SPKG_INST" | sed 's/ /\\\\ /g'`

EOF

# $(TOOLCHAIN) variable containing prerequisites for the build
echo >&3 -n 'TOOLCHAIN ='
if [ "$SAGE_INSTALL_CCACHE" == yes ]; then
    echo >&3 -n ' $(INST)/ccache'
fi
if [ "$need_to_install_gcc" == yes ]; then
    echo >&3 -n ' $(INST)/$(GCC)'
    # Use this option for the prereq configure script, such that it
    # will skip all compiler checks.
    export PREREQ_OPTIONS="--disable-compiler-checks $PREREQ_OPTIONS"
fi
echo >&3

# Copy spkg/standard/deps
cat >&3 <<EOF

#==============================================================================
# What follows now is a copy of
#   $SAGE_ROOT/spkg/standard/deps
#==============================================================================

EOF

cat standard/deps >&3

# Close the Makefile
exec 3>&-

###############################################################################
# Skip the rest if nothing to do (i.e., to [re]build).
###############################################################################

# Set MAKE to "make" if unset
if [ -z "$MAKE" ]; then
    MAKE=make
fi

# * If "make" doesn't understand the -q option (although we require
#   GNU make, which supports it), it should exit with a non-zero status
#   which is not a problem.
# * Only do this check if spkg/bin/sage-spkg exists, as that means we
#   are running sage-5.x and sage-spkg understands MAKEFLAGS.
#   If we are upgrading, we might have a pre-4.8 version of sage-spkg
#   which doesn't check MAKEFLAGS.
#   See Trac #12248 and also #12016.
if [ -f "$SAGE_ROOT/spkg/bin/sage-spkg" ]; then
    if $MAKE -q "$@" >/dev/null 2>/dev/null; then
        echo "Nothing to (re)build / all up-to-date."
        exit 0
    fi
fi

# Dump environment for debugging purposes:
echo "*** ALL ENVIRONMENT VARIABLES BEFORE BUILD: ***"
env | sort
echo "***********************************************"

###############################################################################
# NOW do the actual build:
###############################################################################
time $MAKE "$@"
if [ $? -ne 0 ]; then
    cat >&2 <<EOF
***************************************************************
Error building Sage.

The following package(s) may have failed to build:
EOF

    for f in "$SAGE_LOGS"/*.log; do
        # Look for recent error message in log file.
        # Note that "tail -n 20 ..." doesn't work on Solaris.
        if tail -20 "$f" | grep "^Error" &>/dev/null; then
            base_f=`basename $f .log`
            cat >&2 <<EOF

package: $base_f
log file: $f
build directory: ${SAGE_BUILD_DIR:-$SAGE_ROOT/spkg/build}/$base_f
EOF
        fi
    done
    cat >&2 <<EOF

The build directory may contain configuration files and other potentially
helpful information. WARNING: if you now run 'make' again, the build
directory will, by default, be deleted. Set the environment variable
SAGE_KEEP_BUILT_SPKGS to 'yes' to prevent this.

EOF
    exit 1
fi

# Build succeeded.
echo "Sage build/upgrade complete!"

if [ "$1" = "all" ]; then
    echo
    echo "To install small scripts to directly run Sage's versions of GAP,"
    echo "the PARI/GP interpreter, Maxima, or Singular etc. (by typing e.g."
    echo "just 'gap' or 'gp') into a standard 'bin' directory, start Sage"
    echo "by typing 'sage' (or './sage') and enter something like"
    echo
    echo "    install_scripts('/usr/local/bin')"
    echo
    echo "at the Sage command prompt ('sage:')."
    echo
    echo "If you issued 'make', 'make all', or a similar command, then the"
    echo "HTML version of the documentation will be built right now."
    echo "Otherwise, if you want to (re)build the HTML documentation,"
    echo "run 'make doc'.  To build the PDF version, run 'make doc-pdf'."
    echo
fi
