#!/bin/bash

set -e

VER="$2"

ORIG_TAR="$(dpkg-parsechangelog -S Source)_$VER.orig.tar"
PARENT_DIR=$(tar -tvf "../$ORIG_TAR.xz" | head -n1 | rev | cut -d " " -f 1 | rev)

TMPDIR="$(mktemp -d /tmp/html5spec.XXX)"

mkdir -p "$TMPDIR/$PARENT_DIR/src/nu/validator/localentities/files/"

curl https://html.spec.whatwg.org/commit-snapshots/dd759db6b6cc2e029539c0673ceb2152117dca35/ -R -o "$TMPDIR/$PARENT_DIR/src/nu/validator/localentities/files/html5spec"

unxz "../$ORIG_TAR.xz"

tar --append -vf "../$ORIG_TAR" -C "$TMPDIR" \
    --owner=0 --group=0 --numeric-owner \
    --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
    "$(realpath "$TMPDIR/$PARENT_DIR/src/nu/validator/localentities/files/html5spec" --relative-to "$TMPDIR")"

xz -f "../$ORIG_TAR"

rm -r "$TMPDIR"
