#!/bin/sh

cd src
CUR=`pwd`

# Build and install Python support files
cd python/
python setup.py build

if [ $? -ne 0 ]; then
    echo "Failure to build sage-mode Python support files"
    exit 1
fi

python setup.py install

if [ $? -ne 0 ]; then
    echo "Failure to install sage-mode Python support files"
    exit 1
fi

# Copy emacs lisp to SAGE_DATA, and print helpful instructions
cd "$CUR"
if [ "x$SAGE_DATA" = x ]; then
    INSTALL_DIR=$SAGE_LOCAL/share/emacs/site-lisp/sage-mode
    # Create intermediate directories
    mkdir -p $INSTALL_DIR
else
    INSTALL_DIR=$SAGE_DATA/emacs
fi
rm -rf $INSTALL_DIR
cp -r emacs $INSTALL_DIR

# Remove old elc files if any.
# Then, even if byte compiling fails, they won't taint the setup.
rm -f $INSTALL_DIR/*.elc

# Byte compile -- this can fail since we don't require emacs for Sage
EMACS=${EMACS-emacs}
$EMACS -batch 2> /dev/null > /dev/null
if [ $? -ne 0 ]; then
    echo
    echo WARNING: Could not find emacs at "'$EMACS'"
    echo "Set the EMACS environment variable or ignore this if you don't have emacs installed"

else

    echo Byte compiling sage-mode with "'$EMACS'"
    echo Set the EMACS environment variable to compile with a different emacs.
    $EMACS -batch -L $INSTALL_DIR/ -f batch-byte-compile $INSTALL_DIR/*.el
    echo Creating autoloads
    $EMACS --batch -Q -L $INSTALL_DIR/ --load $INSTALL_DIR/sage.el \
           --funcall 'sage-update-autoloads'
    rm $INSTALL_DIR/sage-load.el~

    # Trick the shell into expanding $INSTALL_DIR and $SAGE_ROOT
    export INSTALL_DIR
    sh -c "cat <<EOF
$(perl -ne 'print if (/Start .emacs/../End .emacs/)' ../SPKG.txt)
EOF"

fi
