#!/usr/bin/env bash

set -e

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

cd "$SAGE_ROOT"

TARGET=autotools-`cat build/pkgs/autotools/package-version.txt`

rm -rf upstream/$TARGET
mkdir -p upstream/$TARGET
cd upstream/$TARGET


echo "Downloading m4 sources..."
sage-download-file http://ftp.gnu.org/gnu/m4/m4-1.4.17.tar.xz | tar xJf -

echo "Downloading help2man sources..."
sage-download-file http://ftp.gnu.org/gnu/help2man/help2man-1.46.4.tar.xz | tar xJf -

echo "Downloading texinfo sources..."
# texinfo 5.x breaks building old versions of autotools...
sage-download-file http://ftp.gnu.org/gnu/texinfo/texinfo-4.13.tar.lzma | tar xJf -

git clone git://git.sv.gnu.org/gnulib
git clone --no-checkout git://git.sv.gnu.org/autoconf
git clone --no-checkout git://git.sv.gnu.org/automake
git clone --no-checkout git://git.sv.gnu.org/libtool


cd "$SAGE_ROOT/upstream"
tar cjf $TARGET.tar.bz2 $TARGET
rm -rf $TARGET

echo "New autotools tarball is ready in $SAGE_ROOT/upstream/$TARGET.tar.bz2"
