#!/bin/bash

# This script uses tar-fixup-stamp-comment.pl provided by tito
# to make tarball generated by setuptools reproducible.

set -e

# ensure that current working directory is OK
pushd "$(dirname "$(realpath "$0")")" > /dev/null

# get information about source
NAME="$(      python setup.py --name)"
VERSION="$(   python setup.py --version)"
TIMESTAMP="$( git log -n 1 --format='%ct')"

# use standard setup.py sdist to generate tar archive
python2 >&2 setup.py sdist --formats=tar

# fix stamps to make the tarball reproducible and save compressed
tar-fixup-stamp-comment.pl $TIMESTAMP < dist/${NAME}-${VERSION}.tar \
| gzip -n -c -                        > dist/${NAME}-${VERSION}.tar.gz

# remove original tar archive
rm dist/${NAME}-${VERSION}.tar

# provide useful output
echo "$PWD/dist/${NAME}-${VERSION}.tar.gz"

popd > /dev/null

