#!/usr/bin/env bash
#
# NOTE: This script is of little use after the git transition and
# will be deleted eventually.
#
# Create an rsyncable source distribution of Sage in dist/sage-rsync.tar.gz
# starting from a regular sdist tarball.
#
# This is mostly useful for regular automatic testing of Sage.
#
# All spkgs in spkg/standard are stored extracted: instead of a file
# spkg/standard/atlas-3.8.4.spkg, there is a directory
# spkg/standard/atlas/ (note the directory has no version number).
# In the tarball, there is no top-level directory like "sage-5.0",
# files like "Makefile" are stored directly at the top level.
#
# Running this script requires:
#  * GNU tar
#  * gzip with --rsyncable patch
#
# However, there are no special requirements for *building* from an
# rsyncable distribution.
#
#
# To build from an rsyncable tarball, do the following:
#   mkdir sage-VERSION
#   cd sage-VERSION
#   tar xzf /path/to/sage-rsync.tar.gz
#   ./rsyncpack.sh     # to repack the directories into spkgs
#   make               # as usual
#
#
# AUTHOR: Jeroen Demeyer (2011-12-10): Trac ticket #12106
#
#*****************************************************************************
#       Copyright (C) 2011 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
#  Distributed under the terms of the GNU General Public License (GPL)
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************


# Exit on error
set -e


# Check whether gzip supports --rsyncable, otherwise bail out immediately
if ! gzip --rsyncable </dev/null >/dev/null 2>/dev/null; then
    echo >&2 "It seems your version of gzip does not support the --rsyncable option."
    echo >&2 "In order to run sage --rsyncdist, you need a patched gzip."
    echo >&2 "For more information about the patch, see"
    echo >&2 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=118118"
    exit 1
fi


# If $1 starts with "sage-", remove this prefix
SAGE_VERSION=`echo "$1" | sed 's/^sage-//'`
if [ -z "$SAGE_VERSION" ]; then
    echo >&2 "Usage: $0 <SAGE_VERSION>"
    echo >&2 "Create an rsyncable source distribution of Sage"
    exit 2
fi

# Run this script from SAGE_ROOT
[ -z "$SAGE_ROOT" ] || cd "$SAGE_ROOT"

if [ ! -r "dist/sage-$SAGE_VERSION.tar" ]; then
    echo >&2 "The sdist tarball dist/sage-$SAGE_VERSION.tar has not been created yet."
    echo >&2 "You should call this script after running ./sage --sdist
    exit 1
fi

# Extract existing sdist tarball
mkdir -p "dist/sage-rsync"
cd "dist/sage-rsync"
echo "Extracting sdist tarball sage-$SAGE_VERSION.tar"
tar -x --strip-components 1 -f "../sage-$SAGE_VERSION.tar"

# Create a shell script to repack the spkgs.  The repacked spkgs will
# not be compressed, but that's not a problem.  It just means one
# should not make an sdist from an rsyncable Sage distribution.
exec 5>rsyncpack.sh
chmod 755 rsyncpack.sh
cat >&5 <<EOF
#!/usr/bin/env bash
set -e

cd spkg/standard
EOF

# Extract all spkgs and rename them to their base name
# (atlas-3.8.4.spkg would be extracted to a directory atlas/)
cd spkg/standard
for spkg in *.spkg; do
    # Determine bare spkg name, without version number
    spkgnamever=`echo "$spkg" | sed 's|\.spkg$||'`
    spkgname=`echo "$spkgnamever" | sed -n 's|^\([^-]*[^0-9]*\)-[0-9].*$|\1|p'`

    if [ "$spkgname" = "" ]; then
        echo >&2 "Cannot determine base package name for $spkg"
        exit 1
    fi

    echo "Extracting $spkg to directory $spkgname"
    ( bzip2 -cd $spkg || gzip -cd $spkg || cat $spkg ) 2>/dev/null |\
        tar -x --delay-directory-restore
    rm $spkg
    mv "$spkgnamever" "$spkgname"

    echo "mv '$spkgname' '$spkgnamever' && tar c '$spkgnamever' >'$spkg' && rm -rf '$spkgnamever'" >&5
done
exec 5<&-
cd ../..

# Put files in the tar file in *alphabetical* order, which is much
# better for rsync.  Print directories with trailing slash for
# better sorting and skip '.'
echo "Packing tarball sage-rsync.tar.gz"
find . '!' -name . '(' -type d -printf '%P/\n' -or -printf '%P\n' ')' |\
sort |\
tar -c --no-recursion -T /dev/stdin |\
gzip --best --rsyncable >../sage-rsync.tar.gz
