#!/bin/sh
#
# Build zotero-reader_<version>+dfsgN.orig.tar.xz.  Called by
# `debian/rules get-orig-source`; VERSION= overrides the version, which
# otherwise comes from the top entry of debian/changelog.
#
# zotero/reader has never been tagged, so the tree is the commit Zotero itself
# pins, together with the submodules that commit records.  Nothing is removed
# here: the whole tree is packed and handed to mk-origtargz, which applies
# Files-Excluded from debian/copyright.  This is a maintainer tool: it needs
# network access and is never run by a build.

set -eu

PKG=zotero-reader
ROOT=$(cd "$(dirname "$0")/.." && pwd)

# The reader commit zotero/zotero 433c109b7128aa80c01840310386bb90908ea8a7
# (11.0-beta.2) pins, and the submodules recorded by it.
COMMIT=0b16e731cf7d880e74faaf8808921678cac38aae
COMMIT_DATE="2026-10-14 15:55:37 UTC"
PDFJS=5f95f712bc629ac11159cb4859ed8091fdffa87b
EPUBJS=e68c8b9c868be6ccb5b9684213117e95e52ef880
SDT=7963c11faac4595be3bbe86b7574704a22c67bed
UPVERSION=0.0~git20261014
REPACK=+dfsg2

VERSION=${1:-${VERSION:-}}
if [ -z "$VERSION" ]; then
	VERSION=$(dpkg-parsechangelog -l"$ROOT/debian/changelog" -SVersion |
		sed -e 's/^[0-9]*://' -e 's/-[^-]*$//' -e "s/$REPACK\$//")
fi
if [ "$VERSION" != "$UPVERSION" ]; then
	printf 'get-orig-source: debian/changelog is %s, this script knows %s\n' \
		"$VERSION" "$UPVERSION" >&2
	exit 1
fi

UVERSION=$VERSION$REPACK
TARBALL=$ROOT/../${PKG}_${UVERSION}.orig.tar.xz

EPOCH=$(date -u -d "$COMMIT_DATE" +%s)

log() { printf 'get-orig-source: %s\n' "$*" >&2; }

# codeload drops large transfers now and then; resume instead of starting over.
fetch_url() {
	url=$1
	out=$2
	attempt=0
	while [ "$attempt" -lt 40 ]; do
		if tar tzf "$out" >/dev/null 2>&1; then
			return 0
		fi
		attempt=$((attempt + 1))
		curl -4 -fsSL --http1.1 -C - --retry 5 --retry-all-errors \
			--retry-delay 5 --max-time 3600 -o "$out" "$url" ||
			log "retrying $url (attempt $attempt)"
		sleep 3
	done
	tar tzf "$out" >/dev/null 2>&1
}

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
STAGE=$WORK/$PKG-$UVERSION
mkdir -p "$STAGE"

fetch() {
	repo=$1 sha=$2 dir=$3
	log "fetching $repo @ $sha -> $dir"
	[ "$dir" = . ] || rm -rf "$STAGE/$dir"
	mkdir -p "$STAGE/$dir"
	name=$(printf %s "$repo" | tr / _)-$sha.tgz
	tarball=$WORK/$name
	fetch_url "https://codeload.github.com/$repo/tar.gz/$sha" "$tarball"
	tar xzf "$tarball" -C "$STAGE/$dir" --strip-components=1
	rm -f "$tarball"
}

fetch zotero/reader "$COMMIT" .
fetch zotero/pdf.js "$PDFJS" pdfjs/pdf.js
fetch zotero/epub.js "$EPUBJS" epubjs/epub.js
fetch zotero/structured-document-text "$SDT" structured-document-text

log "packing the whole tree"
RAW=$WORK/${PKG}_${VERSION}.orig.tar.xz
tar --sort=name --owner=0 --group=0 --numeric-owner --mtime="@$EPOCH" \
	-cJf "$RAW" -C "$WORK" "$PKG-$UVERSION"

log "dropping what debian/copyright excludes"
rm -f "$TARBALL"
(cd "$ROOT" && mk-origtargz --version "$VERSION" --repack-suffix "$REPACK" \
	--compression xz --copy --force-repack \
	-C "$(dirname "$TARBALL")" "$RAW")
log "$(stat -c '%s bytes' "$TARBALL")"
