#!/usr/bin/env bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 <SAGE_VERSION> <SAGE_ROOT>"
    echo "Build Sage source code package."
    exit 2
fi

export SAGE_VERSION=$1
export SAGE_ROOT=$2
export PATH="$SAGE_ROOT/local/bin/:$PATH"

rm -rf tmp
mkdir tmp tmp/doc

cp -p MANIFEST.in README.txt *.py install sage-push pull \
    spkg-delauto spkg-dist spkg-install bundle export tmp/
cp -pR .hg* c_lib sage tmp/

# Copy the doc/ directory, except doc/output
cp -pR doc/common doc/[a-z][a-z] tmp/doc/

# Prepare the distribution in tmp
cd tmp/

# Delete all the autogenerated files under sage/, since they will get
# created again when Sage is built or upgraded.
./spkg-delauto sage

# Create the sdist using Python's distutils.
python setup.py sdist

# Recompress it in bz2 format (which is better) and write the file to
# the correct place.
cd ..
mkdir dist 2>/dev/null
gzip -dc tmp/dist/sage-$SAGE_VERSION.tar.gz |bzip2 >dist/sage-$SAGE_VERSION.spkg

# Delete the tmp directory.
rm -rf tmp
