#!/bin/sh

#######################################
# Install SAGE libraries
#######################################

CUR=`pwd`

if [ $? -ne 0 ]; then
   exit 1
fi

DEVEL=$SAGE_ROOT/devel

if [ ! -d "$DEVEL" ]; then
    mkdir "$DEVEL"
fi

if [ $? -ne 0 ]; then
   exit 1
fi

D=`pwd`
E=`basename "$D"`

if [ ! -d "$DEVEL/old" ]; then
    mkdir "$DEVEL/old"
fi

if [ -d "$DEVEL/sage-main" ]; then
    cd "$DEVEL/sage-main"
    #hg pull "$SAGE_SERVER"/hg/sage-main
    # Pull in changes from the archive just downloaded.
    hg pull "$CUR"
    hg merge tip
    hg ci -m "merge"
    # Make the pulled in changes take effect.
    hg update

    # Since we are doing an upgrade, it is very important that the
    # build always completely works with no dependency issues. It is
    # OK if the build takes a little longer, since this is a full
    # upgrade.  So we touch all pyrex code.

    touch */*.pyx */*/*.pyx */*/*/*.pyx */*/*/*/*.pyx */*/*/*/*/*.pyx */*/*/*/*/*.pyx  */*/*/*/*/*/*.pyx  2> /dev/null

    "$SAGE_ROOT/sage" -b main
    if [ $? -ne 0 ]; then
        echo "Error building new version of SAGE."
    fi
    exit $?
fi

mkdir "$DEVEL/sage-main"
cp -pr * .hg* "$DEVEL/sage-main/"

cd "$DEVEL/sage-main"

# for backward compatibility
# (going from sage-1.4 to sage-1.5)
rm -f */*/interrupt.h */*/*/interrupt.h */*/*/*/interrupt.h

# touch and build everything
sage -b main

# repeat -- this copies over all the pyx files, so they appear when
# you use inspect.
sage -b main

if [ $? -ne 0 ]; then
    echo "ERROR installing SAGE"
    exit 1
fi
