#!/bin/sh
# Build the "vendor" orig component tarball,
# ../lightning-matrix_<upstream version>.orig-vendor.tar.xz.
#
# It holds only the crates the archive cannot provide, as listed in
# debian/vendored-crates.txt (produced by debian/update-vendored-crates), at the
# versions rust/Cargo.lock pins. Run it on an unpatched upstream tree; it needs
# cargo and network access, the package build itself needs neither.
set -eu

top=$(cd "$(dirname "$0")/.." && pwd)
version=${1:-$(dpkg-parsechangelog -l"$top/debian/changelog" -SVersion | sed 's/-[^-]*$//')}
out=${2:-"$top/../lightning-matrix_${version}.orig-vendor.tar.xz"}
# A fixed, reproducible mtime for every file in the tarball. Epoch 0 (what
# this used to pass to tar) makes lintian flag every installed file as
# package-contains-ancient-file; the changelog's own timestamp is the usual
# reproducible-builds stand-in for "now" and avoids that false positive.
mtime=$(dpkg-parsechangelog -l"$top/debian/changelog" -STimestamp)

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

CARGO_HOME="$work/cargo-home" cargo vendor --locked --versioned-dirs \
    --manifest-path "$top/rust/Cargo.toml" "$work/full" >/dev/null

mkdir "$work/vendor"
while read -r crate; do
    [ -d "$work/full/$crate" ] || { echo "$crate is not in rust/Cargo.lock" >&2; exit 1; }
    cp -a "$work/full/$crate" "$work/vendor/$crate"
    case $crate in
    libsqlite3-sys-*)
        # The build links the system libsqlite3 (patch
        # 0005-rust-use-system-sqlite.patch drops "bundled-sqlite"), so build.rs
        # takes its non-bundled path and never compiles the SQLite or SQLCipher
        # amalgamations shipped inside the crate (~9 MB each). Drop them rather
        # than ship unused third-party source. What build.rs still reads without
        # "bundled" stays: bindgen-bindings/ and sqlite3/bindgen_bundled_version*.rs.
        rm -rf "$work/vendor/$crate/sqlcipher"
        rm -f "$work/vendor/$crate/sqlite3/sqlite3.c" \
              "$work/vendor/$crate/sqlite3/sqlite3.h" \
              "$work/vendor/$crate/sqlite3/sqlite3ext.h" \
              "$work/vendor/$crate/sqlite3/wasm32-wasi-vfs.c"
        ;;
    aquamarine-*)
        # doc/js/ is a minified build of mermaid.js and the libraries inside it
        # (DOMPurify, js-yaml, dagre, dayjs, lodash, ...), 12 MB without source and
        # under several different licences. The crate embeds it with include_dir!
        # and uses it only when rustdoc renders a diagram; matrix-sdk applies the
        # macro under cfg(doc), so no build of this package ever reads it. Keep
        # the directory, which include_dir! needs to exist, and nothing else.
        rm -rf "$work/vendor/$crate/doc/js"
        mkdir -p "$work/vendor/$crate/doc/js"
        printf '%s\n' \
            'The mermaid.js bundle that aquamarine normally ships here was removed' \
            'from the Debian source package: it is minified JavaScript without' \
            'source, and it is only used when rustdoc renders diagrams.' \
            > "$work/vendor/$crate/doc/js/README.Debian"
        ;;
    esac
    # Source-package tooling may drop files such as Cargo.toml.orig, and cargo
    # fails the build if a file listed in the checksum is missing. Keep the
    # package hash and drop the per-file list.
    python3 - "$work/vendor/$crate/.cargo-checksum.json" <<'PY'
import json, sys
p = sys.argv[1]
d = json.load(open(p))
d["files"] = {}
json.dump(d, open(p, "w"))
PY
done <"$top/debian/vendored-crates.txt"

# Fixed ownership, order and mtime so the same list gives the same bytes.
tar --sort=name --owner=0 --group=0 --numeric-owner --mtime=@"$mtime" \
    -C "$work/vendor" -cJf "$out" .

printf 'Wrote %s (%s crates)\n' "$out" "$(wc -l <"$top/debian/vendored-crates.txt")"
