#!/bin/sh
# Produce the repacked upstream tarball: strips the documentation copied from
# MDN Web Docs (CC-BY-SA-2.5) out of lib/src/dom/*.dart.
#
#   debian/repack <upstream.tar.gz> <output.tar.gz>
set -eu

in=$1
out=$2
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT INT TERM

tar xzf "$in" -C "$work"

find "$work/lib/src/dom" -name '*.dart' | while IFS= read -r f; do
    sed -i \
        -e '/^\/\/$/{N;/\n\/\/ API docs from \[MDN Web Docs\]/d}' \
        -e '/^\/\/ API docs from \[MDN Web Docs\]/d' \
        -e '/^\/\/ Attributions and copyright licensing by Mozilla Contributors/d' \
        -e '/^\/\/ under \[CC-BY-SA 2\.5\]/d' \
        -e '/^[[:space:]]*\/\/\//d' \
        "$f"
done

tar czf "$out" -C "$work" .
