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

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

export PATH SAGE_VERSION SAGE_ROOT

if [ -e tmp ]; then
   rm -rf tmp
fi
mkdir tmp

if [ ! -d dist ]; then
   mkdir dist
fi

CUR=`pwd`
rm -rf */.doctest */*/.doctest */*/*/.doctest */*/*/*/.doctest */*/*/*/*/.doctest  */*/*/*/*/*/.doctest
rm -rf */.doctest* */*/.doctest* */*/*/.doctest* */*/*/*/.doctest* */*/*/*/*/.doctest*  */*/*/*/*/*/.doctest*

rm -rf c_lib/*.so c_lib/*.os c_lib/*/*.os c_lib/*/*/*.os
if [ $? -ne 0 ]; then
    echo "Error deleting c_lib temp files"
fi

cp -prv MANIFEST.in README* install sage sage-push pull setup.py spkg-* .hg .hgtags .hgignore bundle export mercurial-howto.txt setupnb.py spkg-delauto c_lib tmp/

cd tmp/

rm -f c_lib/*.so c_lib/*.os  # important -- do not distribute those; it wastes space and causes problems!

# Delete all the autogenerated files, since they will get created again
# when SAGE is built or upgraded.
cd sage; "$CUR"/spkg-delauto .; cd ..

python setup.py sdist

cd dist
gunzip sage-$SAGE_VERSION.tar.gz
bzip2 -f sage-$SAGE_VERSION.tar
mv sage-$SAGE_VERSION.tar.bz2 ../../dist/sage-$SAGE_VERSION.spkg

cd ../..
rm -rf tmp
