#!/usr/bin/env bash
# Vendor the pre-trained RNNoise model into the upstream tarball so the Debian
# build can run offline.
#
# Upstream's autogen.sh calls download_model.sh, which fetches
#   https://media.xiph.org/rnnoise/models/rnnoise_data-<hash>.tar.gz
# (the model is too large for git) and extracts src/rnnoise_data.{c,h}, which
# are required to compile librnnoise. Debian builds have no network access, so
# uscan repacks the tarball (+ds) and this script -- invoked via
# `Update-Script:` in debian/watch as
#
#     debian/repack.sh --upstream-version <version>+ds
#
# downloads and bakes the model sources into the orig tarball in place.
# See debian/README.source and debian/copyright.
set -ex

NEW_VERSION=
while [ $# -gt 0 ]; do
    case "$1" in
    --upstream-version)
        NEW_VERSION="$2"
        shift 2
        ;;
    *) shift ;;
    esac
done

if [ -z "$NEW_VERSION" ]; then
    echo "Usage: $0 --upstream-version <version>" >&2
    exit 1
fi

SOURCE=$(dpkg-parsechangelog -SSource)
PARENT=$(realpath ..)

# Upper bound for file mtimes in the repacked tarball. We keep each file's own
# (genuine) timestamp via tar --clamp-mtime below; this only caps anything newer
# than the changelog date, so the repack stays byte-reproducible without
# flattening real timestamps to @0 (which trips package-contains-ancient-file
# on shipped upstream files such as README and the headers).
: "${SOURCE_DATE_EPOCH:=$(dpkg-parsechangelog -STimestamp)}"

# uscan/mk-origtargz drops the (already-repacked) orig tarball next to the
# package dir. Pick whichever compression extension is actually there.
TARBALL=
TAR_EXT=
for ext in xz gz bz2 zst; do
    cand="$PARENT/${SOURCE}_${NEW_VERSION}.orig.tar.$ext"
    if [ -e "$cand" ]; then
        TARBALL="$cand"
        TAR_EXT="$ext"
        break
    fi
done
[ -n "$TARBALL" ] || {
    echo "Could not find ${SOURCE}_${NEW_VERSION}.orig.tar.* in $PARENT" >&2
    exit 1
}

case "$TAR_EXT" in
gz) TAR_COMPRESS_FLAG="-z" ;;
xz) TAR_COMPRESS_FLAG="-J" ;;
bz2) TAR_COMPRESS_FLAG="-j" ;;
zst) TAR_COMPRESS_FLAG="--zstd" ;;
esac

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

tar -xf "$TARBALL" -C "$WORKDIR"
SRCDIR=$(find "$WORKDIR" -maxdepth 1 -mindepth 1 -type d | head -1)
cd "$SRCDIR"

# --- Vendor the pre-trained model -------------------------------------------
MODEL_VERSION=$(cat model_version)
MODEL_TARBALL="rnnoise_data-${MODEL_VERSION}.tar.gz"
MODEL_URL="https://media.xiph.org/rnnoise/models/${MODEL_TARBALL}"

if [ ! -f src/rnnoise_data.c ]; then
    echo "Fetching model ${MODEL_TARBALL} ..."
    if command -v curl >/dev/null 2>&1; then
        curl -fSL "$MODEL_URL" -o "$MODEL_TARBALL"
    else
        wget -O "$MODEL_TARBALL" "$MODEL_URL"
    fi
    # No -m: keep the model tarball's own mtimes (deterministic) rather than
    # stamping the extracted files with a build-time "now".
    tar -xvof "$MODEL_TARBALL"
    rm -f "$MODEL_TARBALL"
fi
# ----------------------------------------------------------------------------

cd "$WORKDIR"
rm -f "$TARBALL" # uscan may have left a symlink to the pristine download
tar --sort=name --clamp-mtime --mtime="@${SOURCE_DATE_EPOCH}" \
    --owner=0 --group=0 --numeric-owner \
    -cf "$TARBALL" $TAR_COMPRESS_FLAG "$(basename "$SRCDIR")"
echo "Done: $TARBALL"
