#!/bin/sh

# Helper script to generate a tarball from a directory, with
# particular attention to giving reproducible output.
#
# Note: if output_file is not an absolute path, it will be assumed
# relative to the current working directory, not source_directory.

set -e

SOURCE_DIR="$1"
OUTPUT_FILE="$2"

if [ -z "$SOURCE_DIR" ] || [ -z "$OUTPUT_FILE" ]; then
	echo "usage: $0 source_directory output_file"
	exit 1
fi

# Ensure tarball doesn't depend on the user's umask(2).
find "$SOURCE_DIR" -type d -print0 | xargs -0r chmod 755
find "$SOURCE_DIR" -not -type d -print0 | xargs -0r chmod u+rw,g+r-w,o+r-w

# Clamp timestamps to be no later than SOURCE_DATE_EPOCH, if defined in
# the environment.  Typically it is the time of the most recent entry in
# debian/changelog
if [ -n "$SOURCE_DATE_EPOCH" ]; then
	# See https://wiki.debian.org/ReproducibleBuilds/TimestampsInTarball
	find "$SOURCE_DIR" -newermt "@$SOURCE_DATE_EPOCH" -print0 | xargs -0r touch --no-dereference --date="@$SOURCE_DATE_EPOCH"
fi

# Create tarball with files sorted in a stable order, see
# https://wiki.debian.org/ReproducibleBuilds/FileOrderInTarballs
# and without timestamp in the gzip header, see
# https://wiki.debian.org/ReproducibleBuilds/TimestampsInGzipHeaders
(
	gzip="pigz -9nm"
	cd "$SOURCE_DIR" && find . -print0 | LC_ALL=C sort -z | tar --owner root:0 --group root:0 --no-recursion --null -T - -I "$gzip" -cvf -
) > "$OUTPUT_FILE"
