#!/bin/sh
# Make the repacked (+ds) orig tarball from upstream's Fossil repository,
# for use while uscan cannot download upstream tarballs (see debian/watch).
#
# Usage, from the top of the source tree:
#     debian/get-orig-source [VERSION]
# VERSION defaults to the newest upstream release found by uscan.
# The tarball is written to the parent directory.
set -eu

url=https://fossil.wanderinghorse.net/r/libfossil
version=${1:-$(uscan --report --dehs | sed -n 's:.*<upstream-version>\(.*\)</upstream-version>.*:\1:p')}
[ -n "$version" ] || { echo "cannot determine upstream version" >&2; exit 1; }

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

fossil clone --no-open "$url" "$tmp/libfossil.fossil"
fossil tarball "$version" "$tmp/libfossil-$version.tar.gz" \
	-R "$tmp/libfossil.fossil" --name "libfossil-$version"
mk-origtargz --package libfossil --version "$version" \
	--repack --repack-suffix +ds --compression gzip \
	--copyright-file debian/copyright --directory .. \
	"$tmp/libfossil-$version.tar.gz"
