#!/bin/sh
#
# Build zotero-document-worker_<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.
#
# Neither document-worker nor its submodules have ever been tagged, so the tree
# is the commit Zotero pins.  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-document-worker
ROOT=$(cd "$(dirname "$0")/.." && pwd)

# The document-worker commit zotero/zotero
# 433c109b7128aa80c01840310386bb90908ea8a7 (11.0-beta.2) pins.
COMMIT=a8bab24c82a41435f125a17a4865951b74518117
COMMIT_DATE="2026-10-12 17:17:56 UTC"
UPVERSION=0.0~git20261012
REPACK=+dfsg3

# pdf.js is taken from the commit zotero-reader pins rather than from the one
# document-worker's .gitmodules records: the latter (f4d05cae) predates
# 2a28e53 ("Finish document-worker page tasks after failures"), and both
# components ship one physical copy of the CMap and standard font files, so
# they have to agree.  See debian/README.source.
PDFJS=5f95f712bc629ac11159cb4859ed8091fdffa87b
SDT=7963c11faac4595be3bbe86b7574704a22c67bed

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
	if [ -n "${ZOTERO_ORIG_CACHE:-}" ] && [ -f "$ZOTERO_ORIG_CACHE/$name" ]; then
		log "using the cached $name"
		cp "$ZOTERO_ORIG_CACHE/$name" "$tarball"
	else
		fetch_url "https://codeload.github.com/$repo/tar.gz/$sha" "$tarball"
		if [ -n "${ZOTERO_ORIG_CACHE:-}" ]; then
			mkdir -p "$ZOTERO_ORIG_CACHE"
			cp "$tarball" "$ZOTERO_ORIG_CACHE/$name"
		fi
	fi
	tar xzf "$tarball" -C "$STAGE/$dir" --strip-components=1
	rm -f "$tarball"
}

fetch zotero/document-worker "$COMMIT" .
fetch zotero/pdf.js "$PDFJS" pdf.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")"
