#!/bin/sh

# Not used during the build because it requires network access.
# Changes should be rare and not crucial for security.

# Stdout should be ready to copy/paste into
# debian/patches/add-targets-for-gnat-s_osconf.diff.

# We do not remove wget messages from stderr, in case they report a
# real problem.

set -C -e -f -u

url='https://wiki.debian.org/Multiarch/Tuples#Why_not_use_GNU_triplets.3F'

wget "$url" --output-document=- | sed -n '
# Search 4 consecutive lines matching this pattern.
N;N;N
\%^<tr>  <td><span class="anchor" id="line-[0-9]\+"></span><p class="line[0-9]\+">\s*\([0-9a-z-]\+\)\s*</td>\n  <td><p class="line[0-9]\+">[^<]\+</td>\n  <td><p class="line[0-9]\+">\s*\([0-9a-zA-Z_]\+\)-\([0-9a-zA-Z_-]\+\)\s*</td>\n</tr>$%{

  # Replace the text.
  s%%+  <!-- Debian \1 -->\n+  <targetset canonical="\2-\3">\n+    <target>\2-\3</target>\n+    <target>\2-.*-\3</target>\n+  </targetset>%

  # Special case, i386 in Debian multiarch <-> i686 in GNU triplets.
  s%<target>i386\([^<]*\)-gnu</target>%<target>i686\1-gnu</target>%g

  # Print the buffer.
  p
}'
