#!/usr/bin/env bash

if [ -z "$SAGE_LOCAL" ]; then
    echo >&2 "Error: SAGE_LOCAL undefined - exiting..."
    echo >&2 "Maybe run 'sage -sh'?"
    exit 1
fi


NTL=ntl-9.8.1
SPKG_ROOT="$SAGE_ROOT/build/pkgs/ntl"

set -e

cd "$SPKG_ROOT"

# Remove old sources and download new
rm -rf src
mkdir src
cd src

tar xzf <( sage-download-file "http://www.shoup.net/ntl/$NTL.tar.gz" )

if [ ! -d "$NTL" ]; then
    echo "$NTL directory not in tarball, aborting"
    exit 1
fi
mv "$NTL" ntl


### libtool
#
# NTL requires libtool to build its shared library but doesn't use
# autoconf itself. So it asks for a global install of libtool, which
# is actually discouraged by autoconf.

mkdir libtool
cd libtool
cp "$SPKG_ROOT/patches/configure.ac" .
touch Makefile.am
autoreconf -fiv
rm -rf autom4te.cache

### Finished creating the src/ directory

# Make everything writable
cd "$SPKG_ROOT"
chmod -R u+w src
tar c src | bzip2 --best >"$SAGE_DISTFILES/$NTL.tar.bz2"
rm -rf src
